Winform Textbox CanGrow? - vb.net

I don't find a CanGrow property on the Textbox control. This is common in some other controls, and what it does is expand the control to acomodate more data. Anyway to get this feature in the TextBox?

Well, I came up with this:
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox.TextChanged
'check to see if textbox has text
If (TextBox.TextLength > 0) Then
'resize height of textbox by count of lines (plus add some padding)
TextBox.ClientSize = New Size(TextBox.ClientSize.Width, Convert.ToInt32((TextBox.Lines.Length * TextBox.Font.Height) + (TextBox.Font.Height * 0.5)))
Else
'resize to one line height (plus padding)
TextBox.ClientSize = New Size(TextBox.ClientSize.Width, Convert.ToInt32(TextBox.Font.Height + (TextBox.Font.Height * 0.5)))
End If
End Sub
Note: it doesn't work with word-warp.

I'm not familiar with CanGrow. Are you looking for Anchor property perhaps?

Anyway to get this feature in the
TextBox?
Well, yes, but, you may need to look into doing this manually. The Graphic.MeasureString() function may be what you are looking for in order to set the width properly.
Keep in mind that MeasureSting may have issues measuring multiline strings.

If you set the anchor properties to top,left,bottom,right then the control will grow as the form resizes.
I think a better option is to use docking though. I usually set up a panel layout with one docked to client, then I put the control I want resized in the panel docked to client, and set the control to dock to client as well.

Related

Cut status strip label to width of form

I have a form with a status strip, which contains a progress bar an a label. Both of these are used to show the user the status/progress of several background workers.
My problem is that sometimes the label is longer than the form is wide (it contains Parameter names that vary quite widely in length). The form has a constant width and is not re-sizable by the user. When this issue occurs the label just appears as blank, I would instead like to cut the label to the length of the form and concatenate "..." on the end.
Can anyone give me some advise on where to start with this? i have tried Google and SO searches and have been unable to come up with anything similar. I essentially need to find the length of the string as it will display on the form, but I don't know where to start with that.
First thing to do is to change the StatusStrip.LayoutStyle from Table to Flow. Which will prevent the label from disappearing. Next, you still want the user to have a chance to read the full text of the label even though it is truncated. Set the StatusStrip.ShowItemToolTips property to True and the label's AutoToolTip to True.
Getting the label's text to not overlap the grip is an uglier problem to fix but one you don't have since you made your form un-resizable. Set the form's SizeGripStyle property to Hide.
This will fix your problem, no code required.
You can try something like this:
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
If Label1.Text.Length > iMaxLblLenght Then
Label1.Text = Label1.Text.Substring(0, iMaxLblLenght) & "..."
End If
End Sub

Winforms list view, anchoring the columns with the form?

this sounds like a really simple question but could easily have a very complicated answer knowing listviews but I basically just want it so that the column width inside a list view will expand as I expand the application window, essentially the same way as anchoring the listView itself?
If I'm not being clear please comment and I'll try explaining better.
My only guess at the moment would be setting the columns to have percentage widths that relate to the listview's size?
Thanks
yes you can do this by modifying the columns with when the form is resized. You will need to anchor the ListView on the form to start with:
Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
With ListView1
.Columns(0).Width = CInt(.Width * 0.2) 'set column width to be 20% of controls width
.Columns(1).Width = CInt(.Width * 0.5) 'set column width to be 50% of controls width
End With
End Sub
Be careful though that you don't end up with column widths that total more than 100% due to rounding as this may give undesirable results

Toggling panel visibility isn't working when they are stacked

I have a form that has two views. These views are controlled by radio buttons on top of the form.
Here is the program:
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc1.PNG
Notice how the Radio button for Number Converter is selected.
Here is what it looks like when you select the Text Converter radio button:
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc2.PNG
That isn't right. I have it set to hide the panel containing the number converter and show the one containing the text converter when you click that one. It hides the number converter but doesn't show the text converter.
Here is a picture of the text converter panel:
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc4.PNG
Here is the relevant code:
Private Sub frmCalculator_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
rdoNumberConverter.Checked = True
End Sub
Private Sub rdoTextConverter_Click(sender As Object, e As System.EventArgs) Handles rdoTextConverter.Click
pnlTextConverter.Visible = True
pnlNumberConverter.Visible = False
End Sub
Private Sub rdoNumberConverter_Click(sender As Object, e As System.EventArgs) Handles rdoNumberConverter.Click
pnlNumberConverter.Visible = True
pnlTextConverter.Visible = False
End Sub
Everything seems right and I can't figure out why the text converter doesn't show up. I've determined that it has something to do with the fact that both of the panels are right on top of each other because when I move them apart, the visibility toggling works perfectly.
Here are the supporting pictures:
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc5.PNG
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc6.PNG
So how do I make it work when they are on top of each other?
I tried using BringToFront() and SendToBack() to make sure the visible panel is in the front and it didn't make a difference.
Make sure the TextConverter panel isn't "inside" the NumberConverter panel.
From the designer, move them into different places so that they do not overlap at all.
Then in code, move them into place:
textConverterPanel.Location = numConvertPanel.Location
Your visible, not visible toggling should work then.
The issue is the panels becoming embedded, as pointed out by #LarsTech. This occurs if you use the GUI to move them to the same location.
If you want to overlap them at design time, create the second panel in a different location. Then in the Properties of the panel in the final location, copy the Location, and paste it into the Location property of the second panel. This will move it to the proper location in the Designer without embedding one into another. This can be repeated for as many additional panels as needed.

How do I color CheckedListBox items in VB.NET?

I am making a personal application in VB.NET that uses a CheckedListBox to store items. I have three buttons on my form, with which I would like to change the selected item's color with (to green, orange, and red.)
I have tried numerous approaches to this issue and have had no such luck. Could someone lend a helping hand?
Use a ListView instead. It has support for checkboxes and selected item colors.
There is a very similar answer here:
For each <item> in CheckedListBox. <item> returns as Object and not as Control
Basically, this control won't do what you want it to (at least not without much complexity). You need to upgrade your control to a ListView.
You can also use TreeView that looks and acts like a checked list box:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.TreeView1.ShowLines = False
Me.TreeView1.CheckBoxes = True
Dim MyColors() As Color = {Color.Black, Color.Blue, Color.Red, Color.Green, Color.Aqua}
For x As Integer = 0 To 4
Dim NewNode As TreeNode = Me.TreeView1.Nodes.Add("Node" & x)
NewNode.ForeColor = MyColors(x)
Next
End Sub
Took the advice of using a ListView. Looked and worked great on my Windows 7 and Vista boxes but on XP, the ListView did not display properly (items were crunched overtop of one another, checkboxes didn't always display). Checked to make sure it was not a framework version issue and that it was not a screen resolution issue. Ended up retreating back to the CheckedListBox implementation which did NOT have the inconsistency.
Found this solution that accomplished the removal of the blue highlight in the CheckedListBox scenario for me. Using it however you have to keep track of the selection in another manner (global variable, looking at the checked item(s), etc.).
I simply clear the selected item(s) after processing the "..._SelectedIndexChanged". The first thing I do in the "..._SelectedIndexChange" is test for no Selection and do nothing if that is the change. The result is that the currently selected item appears unselected (and actually is unselected, i.e. no blue highlight) however the CheckBox remains checked indicating the most recent selection for the user.
Example ==>
Private Sub ModelCheckedListBox_SelectedIndexChanged(ByVal sender As System.Object,...
Dim x As Short = ModelCheckedListBox.SelectedIndex
If x >= 0 Then
'Something I always do since the Selection Mode = "One" doesn't bother to clear
'the checks itself
ModelCheckedListBox.SetItemChecked(x, True)
If ModelCheckedListBox.CheckedItems.Count > 1 Then
For Each item In ModelCheckedListBox.CheckedIndices
If item <> x Then
ModelCheckedListBox.SetItemChecked(item, False)
End If
Next
End If
ModelCheckedListBox.Refresh()
'More of your code
ModelCheckedListBox.ClearSelected()
End If
End Sub

Odd ComboBox behavior on resize

I have an issue where a ComboBox control will change it's Text value when it is resized. Here is some sample code that I worked up:
Option Explicit On
Option Strict On
Public Class FMain
Private Sub FMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
uxComboBox.DropDownStyle = ComboBoxStyle.DropDown
uxComboBox.AutoCompleteSource = AutoCompleteSource.ListItems
uxComboBox.AutoCompleteMode = AutoCompleteMode.Suggest
ComboTest()
End Sub
Private Sub ComboTest()
Dim value As String = "6"
uxComboBox.Text = String.Empty
uxComboBox.Items.Clear()
uxComboBox.Items.AddRange(New String() {"4 9/16", "6 9/16", "7 9/16", "8 9/16"})
Dim index As Integer = uxComboBox.FindStringExact(value)
If uxComboBox.SelectedIndex index Then
uxComboBox.SelectedIndex = index
End If
If uxComboBox.SelectedIndex = -1 AndAlso _
Not String.Equals(uxComboBox.Text, value, StringComparison.OrdinalIgnoreCase) Then
uxComboBox.Text = value
End If
' unselect the text in the combobox
'
uxComboBox.Select(0, 0)
End Sub
End Class
Note that this form (FMain) has a single combobox on it (uxComboBox) that is docked to the top. When I run the code I see that the combobox has a value of "6" which is what I would expect. When I then resize the form, the combobox gets a value of "6 9/16" which is what I would NOT expect.
Does anyone know why this happens? Any suggested workarounds?
Thanks!
Stephen
Yes, this is a known bug in the native Windows implementation of ComboBox. There's another aspect to this bug. Put a button on your form and give it TabIndex = 0, change the CB's TabIndex to 1. Run it, the button will have the focus. Resize. Note that the ComboBox's text changes as before but now also gets highlighted, as though it has the focus. Even though it hasn't.
I think this bug has been around since Vista, it didn't get fixed in Win7. There's no known workaround for it.
When the form loads, ComboTest gets executed, and you see a '6', however when you resize it does not show the new data, sounds like you need to refresh the combo box, regardless of the resize or not.
Try uxComboBox.Refresh() immediately after the line uxComboBox.Items.AddRange.
And after the line 'ComboTest', set the selected index to 0 uxComboBox.Index = 0 also.
Hope this helps,
Best regards,
Tom.
I am using windows 10 and Visual Studio 2017. It appears that this bug is still around. With Hans Passant's answer above I worked around the problem in this way.
I had a combo as a control anchored left and right so it stretched when the form expanded. When the screen expanded, the combobox text was highlighted as if it had got focus even though it hadn't.
As a work around I took one of the anchors off and added it to text box that was next to it. Now my combo box doesn't expand with the screen, the text box does instead. I know its not a fix all solution but it may help someone in a similar situation to sort the problem.