I have make a ToolStrip works like windows task bar. When user opens a new form, a label with form's icon appears on ToolStrip as new label item. I also have a button that closes all forms at once. But I want to remove every relevant label too, so I added this into the click event...
For Each mdichildlabel As ToolStripLabel In Me.BottomToolStrip.Items
If mdichildlabel.Name = "NewLabel" Then
BottomToolStrip.Items.Remove(mdichildlabel)
End If
Next
But I get this error: An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Doesn't work doesn't tell us what you are seeing that is not working.
Generally, you can't modify a collection while for-eaching it because of the changing index positions. Try iterating over it backwards:
For i As Integer = Me.BottomToolStrip.Items.Count - 1 to 0 Step -1
If Me.BottomToolStrip.Items(i).Name = "NewLabel" Then
Me.BottomToolStrip.Items.RemoveAt(i)
End If
Next
Related
I'm working in VB.NET, Visual Studio 2017. I have a combobox with DropdownStyle = Dropdown. If the user types something invalid in the text field of the combobox (invalid means it doesn't match a value in the combobox) then we display a message and then return focus to the text field with the text highlighted, so they can see what they typed. The message is displayed from the Validating event.
This works fine if they don't open the dropdown. If they do open the dropdown and, while it is open, they type in an invalid entry, the message displays but the entry they typed clears.
I have put in debug statements to see what events are firing. Before the message displays, I get a DropDownClosed (text is still there), then a TextChanged (text is still there), then a second TextChanged (text is now empty). I think something about losing focus to display the message may be triggering something, but I can't figure out what.
I can save off the text and then replace it after the message displays, but while the message is up, the text field is blank.
Any ideas?
While I still don't understand the series of events that causes the problem, I found a solution. Just before the message is displayed from within the Validating event of the combobox, I set focus to the combobox. I'm guessing that tabbing away from the invalid entry loses focus, and because the dropdown is open, it then closes which somehow clears the entry. Go figure.
Here is the code from the validating event:
If testInList Then
ResultTextBox.Enabled = True
Else
TestComboBox.Focus
'If test is not in combobox, display message.
MyUtility.MicroUtilities.DisplayMessage(
My.Resources.RES_MSG_INVALID_TEST, MessageBoxIcon.Information, , , True)
e.Cancel = True
End If
The "TestComboBox.Focus" line has fixed the issue of the text disappearing. However, if the user's invalid entry is a partial match for an item in the dropdown, then the text in the text field of the combobox is updated to that item, so now it looks like they typed a valid entry but are getting an message that it's invalid. For example, if there is an entry in the dropdown of "NAMC" and they type "NA" (with the dropdown open) and tab away, the entry changes to "NAMC".
Any ideas on how to prevent that?
p.s. The AutocompleteMode is set to None.
So again, I don't understand the sequence of Events that causes the issue, but I've found something that works. If I move the new "TestCombobox.Focus" line outside of the If condition, the text remains in the text field before, during and after the message displays:
TestComboBox.Focus()
testInList = TestEntryValid()
If testInList Then
EnableResultFields(True)
Else
'If test is not in combobox, display message.
MyUtility.MicroUtilities.DisplayMessage(
My.Resources.RES_MSG_INVALID_TEST, MessageBoxIcon.Information, , , True)
e.Cancel = True
End If
This moves the .Focus before TestEntryValid, but I don't see anything in there that would trigger any events:
Private Function TestEntryValid() As Boolean
Dim item As String = TestComboBox.Text
Dim validTest As Boolean = False
If item.Length > 0 Then
Dim index As Integer = TestComboBox.FindStringExact(item)
If index > -1 Then
validTest = True
End If
End If
Return validTest
End Function
If anyone can explain why this works, I'd love to know. If not, thanks to everyone who responded!
When I reference to subform error occurs. I have 1 main form and 2 subforms on the main one. One of subform has On Current event which trigger code:
Me.Parent![1ChildEquipFilter].Form.RecordSource = StrSQL
(it changes second subform's RecordSource)
When the main form is opened the first time, the error message appears
'you entered an expression that has an invalid reference to the property form/report'.
But when I click on the debug and then reset, all work properly. What's the matter?
When you open the form, first the two subforms are opened, then the main form, and then the two subforms again.
The simple workaround is to eat the error:
On Error Resume Next
Me.Parent![1ChildEquipFilter].Form.RecordSource = StrSQL
On Error GoTo 0
In VB.NET (VS2008 .NET Framework 2.0) I want to launch an event when double clicking any row of a DataGridView. I got this using the event CellContentDoubleClick, but the problem is when I do double click in the titles of DataGridView the event launches too. I just want to avoid actions in the titles.
How could I solve this?
Thanks in advance.
In CellContentDoubleClick event you can check if you click on column/row header using this code:
If e.ColumnIndex = -1 Or e.RowIndex = -1 Then
'do what you want (maybe Exit Sub)
End If
If you need to stop only column header double click remove any reference to e.RowIndex.
I have a VB.Net WinForm Program.
I dynamically create panels with controls.
Each panel has:
2 Labels
1 DataGridView
1 Button
Everything works fine the first time I create the panels.
Everything gets created, and everything is functional.
If I have to re-create the form, I get rid of the existing panels (and their controls) with this code:
For P = 0 To Panels.Count - 1
For Each PControl In Panels(P).controls
Panels(P).controls.remove(PControl)
Next
Me.Controls.Remove(Panels(P))
Next
Panels.Clear()
DataGrids.Clear()
lblCounts.Clear()
Where:
Panels, DataGrids, & lblCounts are ArrayLists holding controls
When I re-create the panels, I get the panels and all of their controls except Buttons
When I step through the debugger, I see the buttons being removed, and I see them being created, but they don't appear in the panel
Any ideas?
Your question is regarding a button not appearing when you are adding the controls, but you are only showing the removal process, which is flawed.
Make a UserControl that holds your Labels, Grid and Button. Add that to your form. That's what UserControls are for.
Also, when you are done using it, just call:
MyControl.Dispose()
Otherwise, I suspect you are leaking memory. Remove does not destroy the object.
For Each PControl In Panels(P).controls
Panels(P).controls.remove(PControl)
Next
This part may kick you out of your code. The 'For Each' does not like it when its items change during execution. check it with Breakpoints. if is is really a problem , you could do..
lazy method, by just adding .ToList
For Each PControl In Panels(P).controls.ToList
Panels(P).controls.remove(PControl)
Next
similar to:
Dim AllControls as New List(Of control)
AllControls.AddRange(Panels(P).controls)
For Each PControl in AllControls
Panels(P).controls.remove(PControl)
Next
or:
For i as integer = Panels(P).controls.count -1 to 0 step -1
Dim PControl as control = Panels(P).controls(i)
PControl.parent.remove(PControl)
Next
Try this
WHILE Panels(P).controls.count > 0
Panels(P).controls.removeAt(1)
so im dynamically populating a checkbox list. I have confirmed that my text and values are correct for each checkbox but when I check a few and click my event button when I loop through the items they are all set to select=false...
Dim resource As ListItem
Dim SelectedHashTable As New Hashtable
For Each resource In chkResources.Items
If resource.Selected = True Then
SelectedHashTable.Add(resource.Text, resource.Value)
End If
Next
set checkpoint at line 5 to view contents of hash table but it is never triggered. Even when I check all boxes. Anyone any idea?
Where are you dynamically populating the checkboxlist? If it's any time after the OnInit event, then the control's viewstate is not getting saved properly and your selections will be overridden on every postback. Try dynamically populating your list in the OnInit handler.