How do I color CheckedListBox items in VB.NET? - 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

Related

DataGridView resets to top when clicked after mouse scroll

I have a DataGridView control on a TabPage of a Windows Form application.
When the user moves the mouse over the DataGrid and uses the scroll wheel, the grid scrolls as expected. But when the user clicks in a cell on the screen, instead of the cell receiving focus, the DataGrid resets to the top and requires the user to scroll down again. This response is non-intuitive since it's not immediately obvious that the cell you thought you clicked on isn't there anymore.
I would be happy to prevent the DataGrid from responding to the scroll wheel until the user clicks in the grid, or preferably to maintain the current actions except not resetting to the top when first clicked.
From what I've researched here, it appears that the DataGrid is rebinding because I'm resetting the binding when the tabpage is entered (since the database might have been updated by one of the other tabs.
Private Sub TabPage1_Enter(sender As Object, e As EventArgs) Handles TabPage1.Enter
LoadTACTable()
End Sub
In LoadTACTable():
dbGetList("spSelectTACList", dtTACs, 0, 100000, Nothing) ' Record numbers are 0 based
bsTACs.DataSource = dtTACs
With gridTACs
' TOTAL Grid width = 1380
.DataSource() = bsTACs
.
.
.
(Showing only part of the code for brevity.
Is there a way to see if the TabPage is already displayed when entered? Or, is unnecessary to reset the gridTAC datasource every time I retrieve the data from the SQL database to the dtTACs datatable using my dbGetList() sub?
There are several possible solutions to your problem. One would be to not automatically rebind the datagrid but let the user do it by clicking some refresh button. That way the user would not see non-intuitive behavior.
You mentioned that the contents of one tab may need to be refreshed when the contents of other tabs are changed. Whenever the contents of a tab is changed and can affect other tabs, you could flag these other tabs (for example, by adding a star to their titles) to indicate that they no longer have the latest data. The user would then know that the tab needs to be refreshed.
There might be other solutions, but it is difficult to tell without knowing more about your use case.
With the guidance above, I believe I solved the issue:
I created a flag:
Dim TabDirty As Boolean
Then I set it in the TabPage.Leave handler:
Private Sub TabPage1_Leave(sender As Object, e As EventArgs) Handles TabPage1.Leave
dtTACs.Dispose()
TabDirty = True
End Sub
Then I just check it when I enter the TabPage:
Private Sub TabPage1_Enter(sender As Object, e As EventArgs) Handles TabPage1.Enter
If TabDirty = True Then
TabDirty = False
LoadTACTable()
End If
End Sub
So far, this appears to work - the grid is not getting reset when clicked, but I will do a bit more testing to confirm that the data is refreshed when necessary.

Taborder jumping between frames

I have a userform containing 3 frames that all include several textboxes. I would like to be able to tab the textboxes across frames.
So, something like this: Frame1 - Textbox1 > Frame2 - Textbox1 > Frame3 - Textbox1 > Frame1 - Textbox2 > Frame1 - Textbox3 > Frame2- Textbox2
The textboxes are dynamically added and stored in an array according to the desired taborder, so the desired order is easily accesible. I just cant seem to find a way to apply this.
It would of course be possible to change the frame layout. However, the frames are used both to control the placement of the textboxes, and also to add separate scrollbars if the amount of textboxes exceeds the frame area.
Is something like this possible? Any help or suggestions much appreciated.
Edit: Added picture of the 3 frames
Frames
Edit2:
I think destination-datas comment put me on the right track.
I created a class module
Public WithEvents TxtBox1 As MSForms.TextBox
Private Sub TxtBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox ("Test")
End Sub
Private Sub TxtBox1_change()
MsgBox ("Test")
End Sub
And then in the sub that generates the textboxes, for the textboxes where I want to jump frames I do:
Dim tabArray1() As New TabBox1
Dim inputfelt As MSForms.TextBox
Set inputfelt = Hovedvindu.SkjemaFrame.SenderFrame.Controls.Add("Forms.TextBox.1", "M" & i & "SenderNavn", True)
Set tabArray1(i).TxtBox1 = inputfelt
This adds all the correct textboxes to an array so that I can make an Exit event that changes focus, which I think should not be too diffucult.
However, I cant seem to get the events to fire properly.
The change event seems to work when I change the textboxes with a sub, for example when I tested that the correct textboxes are added to the array by looping through and changing the text of the textboxes in the array. But when I change the textboxes manually, nothing happens. The exit event doesnt seem to work at all.
Im not too experienced with event handling, so I might have missed something.
I found an acceptable solution. The approach described in Edit 2 of the original post worked once I moved the definition of the TabArray() outside the sub, so that I keep track of the textboxes after the sub is ended.
I have one array, and one class module for the "end" textboxes on each of the 3 frames. The class modules tracks keydown on Tab. The 3 class modules are identical, except that they store the tabindex of the textbox from where the focus jumps to another frame in 3 different hidden labels on the userform. Keeping track of the tabindex allows me to know where to start when focus comes back to a frame.
the 3 class modules look like this:
Public WithEvents TxtBox1 As MSForms.TextBox
Private Sub TxtBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = vbKeyTab Then
'store tabindex
Hovedvindu.Controls("SisteTab1").Caption =
Hovedvindu.SkjemaFrame.SenderFrame.ActiveControl.TabIndex
'call sub that moves the focus
flyttfokus (1)
End If
End Sub
The sub that moves focus is shown below. Input is just an integer (1,2 or 3) depending on which class module the call comes from, and thus which frame to move from/to. The sub reads the 3 hidden labels to know which textbox to start from when the focus is moved to a new frame. Error handling in the last case is for when the focus is moved from the very last textbox in the 3d frame. This will try to move focus to a non-existing textbox in the first frame, which will throw an error which can be ignored.
Sub flyttfokus(SisteFrame As Integer)
Dim St1 As Integer
Dim St2 As Integer
Dim St3 As Integer
St1 = CInt(Hovedvindu.Controls("SisteTab1").Caption)
St2 = CInt(Hovedvindu.Controls("SisteTab2").Caption)
St3 = CInt(Hovedvindu.Controls("SisteTab3").Caption)
Select Case SisteFrame
Case 1
Hovedvindu.SkjemaFrame.MottakerFrame.Controls(St2 + 1).SetFocus
Case 2
Hovedvindu.SkjemaFrame.InfoFrame.Controls(St3 + 1).SetFocus
Case 3
On Error Resume Next
Hovedvindu.SkjemaFrame.SenderFrame.Controls(St1 + 1).SetFocus
On Error GoTo 0
End Select
End Sub
This solution will work as desired when starting from the very first textbox. If the user however, mouseclicks to a textbox for example on the second line and then starts tabing, focus will be moved back to the first line when focus is moved between frames. This is because the variables (labels) that keeps track og the Tabindex from where to start when a frame receives focus are only updated as the user tabs through the sheet. It is probably possible to update these variables on mousclick to a textbox, based on the amount of textboxes that are generated by the program. For my purposes this is not deemed necessary.

VB.NET Listview Textchanged Search

I've tried searching on stackoverflow and implementing code that I found but to no avail. As the text changes I want to use the current text in the textbox to filter through the listview items (so the items that are not closed to being matched get removed) and it only leaves me with whatever is contained in the columns.
Here's an example of what I mean:
Search: "Georges"
|1|Anderson Silva|Anderson silva is the champion in the ...|
|2|Georges St-Pierre| Georges is the champion in the ...|
|3|Georges Sotoropolis| Georges Sotoropolis is a fighter in the lightweight division|
With this search, only rows 2 and three would be returned. The first row would be omitted and not displayed. Once I erase the terms, then it would get displayed.
Here is the code that I currently have:
Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
lwArticles.BeginUpdate()
If tbSearch.Text.Trim().Length = 0 Then
'Clear listview
lwArticles.Clear()
'If nothing is in the textbox make all items appear
For Each item In originalListItems
lwArticles.Items.Add(item)
Next
Else
'Clear listview
lwArticles.Clear()
'Go through each item in the original list and only add the ones which contain the search text
For Each item In originalListItems
If item.Text.Contains(tbSearch.Text) Then
lwArticles.Items.Add(item)
End If
Next
End If
lwArticles.EndUpdate()
End Sub
It seems to be working but I cannot see the listview items once I enter something in the tbSearch. The scrollbar gets smaller / larger depending if there are more / less items due to the search being executed. My problem seems that they are not visible
Thank you!
Listview.Clear wipes out the items, columns, and groups. It appears you only want to wipe out the items, so call lwArticles.Items.Clear instead of lwArticles.Clear
I would suggest another approach - first create a DataTable based on your original items. Then create a DataView and assign that as DataSource of your ListView. Now you can change DataView.RowFilter, and your list will update automatically, so only items matching the filter will show up. Using this approach you don't need to recreate everything from scratch every time, and your TextChanged becomes very simple and maintainable - it just changes RowFilter, if a corresponding DataView has already been created.
Here's the final answer of my question:
Private originalListItems As New List(Of ListViewItem) 'this gets populated on form load
Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
lwArticles.BeginUpdate()
If tbSearch.Text.Trim().Length = 0 Then
'Clear listview
lwArticles.Items.Clear()
'If nothing is in the textbox make all items appear
For Each item In originalListItems
lwArticles.Items.Add(item)
Next
Else
'Clear listview
lwArticles.Items.Clear()
'Go through each item in the original list and only add the ones which contain the search text
For Each item In originalListItems
If item.Text.Contains(tbSearch.Text) Then
lwArticles.Items.Add(item)
End If
Next
End If
lwArticles.EndUpdate()
End Sub
Credits to Dan-o for explaining that lwArticles.Clear() will clear everything. He notified me that I needed lwArticles.Items.Clear() to clear only the items in the listview.

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.

Winform Textbox CanGrow?

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.