Auto scroll to bottom with a textbox - forms

I have an mdb file made by ms access. It got a form inside and inside the form there are one large textbox.
The intention of making this textbox is to show the progress of some work by adding messages inside the textbox:
txtStatus.value = txtStatus.value & "Doing something..." & vbCrLf
txtStatus.value = txtStatus.value & "Done." & vbCrLf
But the problem is, when the height of the text > height of the textbox, the new message is not displayed automatically. The textbox has a scroll bar, but I have to scroll it manually. I would like to auto scroll to the bottom whenever new text pop up.
I tried to add this code(copied from internet) in the On Change property, but the code failed, it does nothing:
Private Sub txtStatus_Change()
txtStatus.SelStart = Len(txt) - 1
End Sub
I wish there would be some simple and beautiful way to achieve this. I don't want to add some code which only work on some computers due to its dependence on the windows platform's kernel/etc.

You can do it via a call to a sub;
AppendText "Bla de bla bla."
.
.
sub AppendText(strText As String)
with txtStatus
.setfocus '//required
.value = .value & strText & vbNewLine
.selstart = len(.Value)
end with
end sub

There is a workaround to the design flaw mentioned by steve lewy in his comment on the original post. It is possible to have a text box that appears to do both of the following:
When the contents are too large for the box, and the box does not
have the focus, the box displays the last part of its contents,
rather than the first part of it.
When the box has the focus, it can scroll to any part of the text,
but it initially shows only the last part of the text, with the
cursor at the end of the text.
This is accomplished by actually having two identically-sized, overlaid text boxes, where one is visible only when the focus is elsewhere, while the other is visible only when it has the focus.
Here’s an example of how to do it in Access 2010.
Create a new Access database, and create a memo field named LongNote in its only table. Fill LongNote with some examples of long text. Create a form for editing that table.
Create a text box called BackBox with the desired size and font, too small to completely show a typical value of its data source, LongNote. (Instead of creating this box, you can rename the default text box created on the form.)
Make an exact copy of that box called FrontBox. Set the data source of FrontBox to be either the entire contents of BackBox or the last part of the contents, as shown below. The size of the last part, measured in characters, depends on the size of the box and its font, as well as on the kind of text to be displayed. It needs to be chosen by trial and error to reliably allow that many characters to be displayed in the box. For instance, here’s the formula for a box that can reasonably hold only 250 characters:
=iif(Len([BackBox])>=250,"... " & Right([BackBox],246),[BackBox])
If the whole value is too large to be shown, three dots precede the part that is shown to indicate that it’s incomplete.
Create another text box called OtherBox, just to have somewhere you can click besides the two boxes already mentioned, so neither of them has the focus. Also create a tiny (0.0097 x 0.0097) text box called FocusTrap, which is used to avoid selecting the entire contents of whatever text box gets the focus when the form is displayed (because text selected that way is hard to read).
Enter the following event-handling VBA code:
' Prevent all text boxes from being focused when a new record becomes
' current, because the focus will select the whole text and make it ugly
Private Sub Form_Current()
FocusTrap.SetFocus
End Sub
Private Sub Form_Open(Cancel As Integer)
FocusTrap.SetFocus
End Sub
' When FrontBox receives focus, switch the focus to BackBox,
' which can display the entire text
Private Sub FrontBox_GotFocus()
BackBox.SetFocus
FrontBox.Visible = False
End Sub
' When BackBox receives the focus, set the selection to
' the end of the text
Private Sub BackBox_GotFocus()
BackBox.SelStart = Len([LongNote])
BackBox.SelLength = 0
End Sub
' When BackBox loses focus, re-display FrontBox – if the text in
' BackBox has changed, then FrontBox will follow the change
Private Sub BackBox_LostFocus()
FrontBox.Visible = True
End Sub
Test the form. When you click on FrontBox, it should disappear, letting you work on BackBox. When you click in OtherBox to remove the focus from BackBox, FrontBox should reappear. Any edits made in BackBox should be reflected in FrontBox.
Back in design mode, move FrontBox so it exactly covers BackBox, and click Position | Bring to Front to ensure that it covers BackBox. Now test the form again. It should appear that a single text box switches between display-the-last-few-lines mode and edit-the-entire-contents mode.

Simply put the following code after linefeed or on Change event txtStatus
txtStatus.SelStart = Len(txtStatus) - 1

Related

How do I access the highlighted text in a Matlab GUI edit box

I would like to have a text box and a button in my GUI. When the button is pressed, a history window will come up, and if the user selects a previous entry the text that they have highlighted in the edit box will be overwritten.
It should work like copy-pasting, whatever is selected in the history window should be pasted over what is selected, or the new text should be added wherever the cursor is.
Is there any way in Matlab to do this? Is it possible to access what is highlighted in an edit box?
With vanilla Matlab this isn't possible. It appear that Mathworks is in the process of expanding what they support with GUIs (survey 1, survey 2), but as of yet they don't allow this.
One possible workaround is using findjobj.m, by Yair Altman. He discusses edit boxes in this post
You can trace findjobj.m for your text box to find 1 or 2 lines of code that are needed so you don't have to carry around all 3,400 lines of it.
Then all you really need to do is get the selected indices and work from there.
javaHandle = findjobj(editBoxHandle);
startSelect = get(javaHandle,'SelectionStart');
endSelect = get(javaHandle,'SelectionEnd');
Once you have the indexes of what text is selected, it becomes almost trivial to replace that text with the new text.
text = editBoxHandle.String;
editBoxHandle.String = [text(1:startSelect) newText text(endSelect:end)];
One thing to note, when the user clicks the button the text box will lose focus, and it will no longer be clear what text is selected. You can remedy this by giving focus back to the text box, and re-selecting what was selected in the button's callback.
uicontrol(editBoxHandle); %Give focus to the edit box, selecting the entire text
javaHandle.select(startSelect,endSelect); %select/highlight the correct stuff
This will highlight the text that will be replaced with the users selection

Word 2010: expand a control to fill its table cell

I have a table cell of fixed large size, and in it is nothing but a Plain Text Content Control, whose default placeholder text "Click here to enter text" takes up only a small portion of that table cell.
The problem is that if the user clicks anywhere in the cell but outside of the control's placeholder text, then starts typing, the entered text will not be part of the control and will not be subject to the control's style or any other control properties.
So - other than adding a lot of dummy characters to the placeholder text, which doesn't seem to be predictable in its word-wrap behavior, is there a way to make the control's placeholder text (or, in general, its click-boundary) fill the entire table cell?
UPDATE actually there were some carriage returns after the control that I was not aware of; after deleting those, clicking anywhere in the cell as long as the x coordinate if the click is greater than or equal to the leftmost x coordinate of the control will edit the control text value as desired. If you click leftward of the control, you will end up editing whatever fixed text exists to the left of the control, i.e. a fixed text label. Still strange. The workaround here was to split cells for all multi line text entry areas, such that the label is on its own cell, and the control is now at the leftmost edge of its cell.
Content controls do not support something like expanding to the surrounding container. They will always use only the space that is required to render their content.
If you want to prevent users from clicking and typing text outside of the control, you could use the following approach:
Put a rich text content control around your table/table row/cell
Put the plain text content control as a nested content control in the table cell (the plain text content control must be non-vanishing for this to work)
Make the outer content control read-only
Now the only thing the user is allowed to edit is the inner plain text content control. The downside of this approach is that your document now contains areas that are locked. This has an impact on usability, first, because, it may not be obvious to the user why certain areas cannot be modified, and second, because a lot of standard actions do no longer work if part of the selection is locked (e.g. Select All > Update ToC).

How do I get OpenOffice Writer Combo boxes to display multi-line text?

I am developing an OpenOffice Writer template that can be used to fill in reports for a child-care centre.
There are some standard outcomes, comprising long sentences, and I want the user to be able to select the appropriate sentence from a combo box. I have entered the sentences into a table in Openoffice Base database, which is then connected to a series of combo boxes in a Writer template. However, when the user choose an option that contains a very long sentence, only the text up to the length of the combo box is visible.
What I want to do is have the selected value of the combo-box wrap over several lines when selected so that all the (very long) text appears in the selected box when the user chooses a long sentence from the combo.
I have been looking through the properties of the combo box control, but have yet to identify one that will allow the selected value in the combo box to word-wrap (so that I could make the combo-box several lines in height such that the entire sentence would fit into the box).
Any pointers on how I could do this would be much appreciated.
thanks,
David.
Thanks Jim K, that was helpful. In the end, what I wound up doing was creating a textbox which I named "selectedOutcomeATextBox" immediately below my combo box which was named "OutcomeCombo".
I then attached the following macro code to the textModified event associated with the "selectedOutcomeATextBox":
Sub UpdateOutcomeA
Dim Doc As Object
Dim Form As Object
Dim Ctl As Object
Dim newCtl as Object
Doc = ThisComponent
Form = Doc.DrawPage.Forms.GetByIndex(0)
Ctl = Form.getByName("OutcomeCombo")
newCtl = Form.getByName("selectedOutcomeATextBox")
newCtl.Text = Ctl.Text
End Sub
I also set the "Printable" property of the "OutcomeCombo" to "No", so that when the document prints, the combo box itself does not appear on the printed page, but the "selectedOutcomeATextBox" textbox which has had its value set by the macro when I choose a value from the combo box does appear with the desired text. I also set the "TextType" property of the selectedOutcomeATextBox" text box to "Multi-Line", so that extra long text will wrap to the next line, thereby showing the very long strings that are stored there.
Thanks heaps Jim K.
cheers,
David Buddrige
Apparently combo boxes do not have the MultiLine attribute. The question was asked a few years ago here but was not solved.
One alternative that requires some macro programming is to use a single multi-line text field and then make a scroll bar button that changes the choice. Instead of a scroll bar, two buttons could be used to change the choice (Previous / Next), or even a list box control. Using a list box control in this way would have the advantage that they could see all the choices at once, like a combo box.
Another approach is to break up each sentence and display the parts across several lines of a list box. Then when one line is clicked, all the lines of a sentence are selected at once, using an event listener for the list box. This could be shown in addition to an ordinary editable multi-line text box, in case none of the answers in the list are wanted.
One more idea: Radio buttons can have multiple lines, so dynamically show radio buttons, one for each sentence. A dialog window could be displayed to hold the radio buttons. The result of the dialog would be used to fill the multi-line text field.
Or you could just live with the truncated sentences. Maybe it would help to make the control a little wider, or abbreviate the sentences.

Deselect text programmatically

I am using a Text widget.
I have over-ridden the right click to display a popup menu in my Perl/Tk GUI. But whenever I right click at any position, the text from the earlier cursor location till the location where I have right clicked gets highlighted.
I don't know what is causing this, so I simply want to programmatically deselect this highlighted text.
How do I go about doing this?
Thanks!
EDIT:
I have made a bind for right-click and this is the subroutine that is called:
sub rightClickMenu {
my ($self, $x, $y) = #_;
$txt->tagRemove('sel', '1.0', 'end');
$rightMenu -> post($x, $y);
$txt->tagRemove('sel', '1.0', 'end');
}
I have removed the sel tag twice (just to be sure). $rightMenu is the menu that is popped up. It shows perfectly fine when right-clicked.
The selection in the text widget is handled by setting the tag sel for the selected range of characters. This tag can be removed like this:
.t tag remove sel 1.0 end
assuming the pathname of your text widget is .t. This specifies that for all characters from the first (1.0) to the character position after the last character (end) the tag sel is to be removed.
Note: normally when removing a tag one has to deal with the possibility that it has been assigned to multiple ranges in the text. The tag removal invocation above clears the tag from the whole text, and that's fine for the selection tag since you're (usually) only supposed to have one selected range anyway. If there are multiple ranges that have the tag foo and you want to clear just one of them, you first need to find the starting and ending indices of that range and clear (by calling tag remove) the tag only between those.
Note 2: All this is assuming that the visible effect is actually caused by the sel tag getting set. In Tk, it's not a standard binding for button 2 to set this tag: it could be that some non-standard binding in Perl-Tk sets some other tag that is displayed visually in the same way as the sel tag is. For further investigation, this command may be useful:
.t tag names $placeWhereIRightClicked
(again assuming the pathname of your text widget is .t, and that placeWhereIRightClicked holds the index of the place where the right clicking occurred) will tell you all tags that are active at that index.
(The command
.t tag names
will list tags for the whole text.)
TkDocs has an article about the text widget where the tag remove command is mentioned, but how to do it in Perl-Tk isn't showed.
The CPAN documentation for the text widget says that the syntax for the command is
$text->tagRemove(tagName, index1, ?index2, index1, index2, ...?)
so I suppose
$text->tagRemove('sel', '1.0', 'end')
or something like that is the way to do it (no Perl, can't test).
(Note: the 'Hoodiecrow' mentioned in the comments is me, I used that nick earlier.)

How to handle variable width FieldObjects in Crystal Reports

I have a Crystal Report which is viewed via a CrystalReportViewer control on an .aspx page (using VS2008).
The report has two data-driven FieldObjects (which can contain a variable number of chars) which I would like to display on the same line beside each other.
Problem is when the text in the first FieldObject is too long it overlaps the text in the second FieldObject.
I have tried setting the 'CanGrow=True' and 'MaxNumberOfLines=1' on the first FieldObject to 'push' the second FieldObject further to the right, but this didn't work.
How do I get the second FieldObject to always display immediately after the first FieldObject regardless of the length of the text in the first?
Cheers in advance of any knowledge you can drop.
you can add a text object to the report. And while editing the text of the text object, drag the field you want to show from the object explorer into the text box. Then hit space, then drag the second field in to the same text box. Your two fields will always be one space a part. You could, of course, add more spaces or any other text you want.
Or you can create a function which returns field1 + " " + field2 and add the function to the report.