Dynamic Menustrip access vb.net - vb.net

I'm adding MenuStrips dynamically based on number of rs232 ports available.
The thing is i want to access the controls text in order to use them in the connection.
Private Sub FormConnection_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
Dim Ports As Array = CType(myPort, Object())
If Ports.Length = 0 Then
MessageBox.Show("No connections available.")
Else
Dim PortsLength As Integer = Ports.Length
For Length As Integer = 0 To PortsLength - 1
Dim Item As New ToolStripMenuItem(Ports(Length).ToString, Nothing, _
New EventHandler(AddressOf MenuCOMclick))
Item.CheckOnClick = True
Item.Name = "COMDYN" + Length.ToString
PortsToolStripMenuItem.DropDownItems.Add(Item)
Next
End If
Now i want to add a Event MenuCOMclick where one of the menus is clicked, all the others are unchecked.
I tried to create an array of controls but the menustrips don't work like that..
How can i do that then ?
Private Sub MenuCOMclick(ByVal sender As Object, ByVal e As EventArgs)
???
???
???
End Sub
Thank you

thats the way to access ToolStripMenuItems in your MenuStrip,
note that if you want to access the sender (the control that was raised the event) you need to cast the sender to the control type.
also you can itterate all ToolStripMenuItems. read my comments, hope it helps.
Private Sub MenuCOMclick(ByVal sender As Object, ByVal e As EventArgs)
' thats how you can check the name of the sender
MsgBox(CType(sender, ToolStripMenuItem).Name)
' thats how you can itterate all ToolStripMenuItem
For Each itm As ToolStripMenuItem In MenuStrip1.Items
For Each Childitm As ToolStripMenuItem In itm.DropDownItems
MsgBox(Childitm.Name) ' show name of the item
' example to access all items properties accept the sender
If Childitm.Name <> CType(sender, ToolStripMenuItem).Name Then
itm.ForeColor = Color.Beige
End If
Next
Next
End Sub

Related

How to change selected item text in list box at run time?

I tried with code like this:
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles MyBase.Leave
' This way is not working
ListBox1.SelectedItem = TextBox1.Text
' This is not working too
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End Sub
The form is looked like this:
I need to change that list text while user typing in the text box. Is it possible to do that at run time?
You are using the form's leave event MyBase.Leave, so when it fires, it is useless to you.
Try using the TextChanged event of the TextBox instead.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged
Make sure to check if an item is actually selected in the ListBox:
If ListBox1.SelectedIndex > -1 Then
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End If
Use Double click to select line (item) inside list box and change or modify.
Instead of using text box use ListBox1_MouseDoubleClick event
Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
then add this code inside this event
Dim intIndex As Integer = ListBox1.Items.IndexOf(ListBox1.SelectedItem)
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items.Remove(ListBox1.SelectedItem)
ListBox1.Items.Insert(intIndex, objInputBox)
End If
OR
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items(ListBox1.SelectedIndex) = objInputBox
End If

Synchronizing three comboboxes

I have, 3 comboboxes loaded from database but not binded, with different data but same indexes.
All of them are setup like this:
ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest
ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
ComboBox1.AutoCompleteCustomSource = mycolumn1
ComboBox1.DropDownStyle = DropDownList
I would like to get functionality that when I choose an item in one combo that other two selects item with same index.
Foe start I am very surprised in that event _SelectedIndexChanged is never triggered while I expected to get index from there.
Why is this so and how to get desired functionality?
I am not sure your issue partially because you have no posted code for me to help you in your situation. Here is an example I done up for you. This is a quick one, but works; you can actually accomplish this in one procedure, but did this so you could understand the functionality of how this works.
Public Class Form1
'Always give variable a default value'
Private selectedIndex As Integer = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim myArray() As String = {"1", "2", "3"}
ComboBox1.Items.AddRange(myArray)
ComboBox2.Items.AddRange(myArray)
ComboBox3.Items.AddRange(myArray)
End Sub
'Handles one of your comboboxes'
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'Cast this as Integer for selected index and set your variable'
selectedIndex = CType(ComboBox1.SelectedIndex.ToString, Integer)
'Next lets make sure that we set the other comboboxes to this index'
ComboBox2.SelectedIndex = selectedIndex
ComboBox3.SelectedIndex = selectedIndex
End Sub
'Another one of your comboboxes'
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
'Cast this as Integer for selected index and set your variable'
selectedIndex = CType(ComboBox2.SelectedIndex.ToString, Integer)
'Next lets make sure that we set the other comboboxes to this index'
ComboBox1.SelectedIndex = selectedIndex
ComboBox3.SelectedIndex = selectedIndex
End Sub
'Your last combobox'
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
'Cast this as Integer for selected index and set your variable'
selectedIndex = CType(ComboBox3.SelectedIndex.ToString, Integer)
'Next lets make sure that we set the other comboboxes to this index'
ComboBox1.SelectedIndex = selectedIndex
ComboBox2.SelectedIndex = selectedIndex
End Sub
End Class
* You must add the global variable to the top so it can be used to hold your current comboboxes selected index. You can also ignore the load event as I used this as a reference.
Thanks!

sub event to interact with more than one control

I want to call this snippet passing a "controlname" like a argument, then the sub interacts with the desired control
How I can do that?
This is the snippet:
#Region " Move a control in real-time "
' Change Textbox1 to the desired control name
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles textbox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
textbox1.Capture = False
Dim ControlMoveMSG As Message = Message.Create(textbox1.Handle, &HA1, New IntPtr(2), IntPtr.Zero)
Me.DefWndProc(ControlMoveMSG)
End If
End Sub
#End Region
UPDATE:
The solution:
Private Sub MoveControl(sender As Object, e As EventArgs) Handles _
TextBox1.MouseDown, _
TextBox2.MouseDown, _
PictureBox1.MouseDown
Dim control As Control = CType(sender, Control)
control.Capture = False
Dim ControlMoveMSG As Message = Message.Create(control.Handle, &HA1, New IntPtr(2), IntPtr.Zero)
Me.DefWndProc(ControlMoveMSG)
End Sub
In this case, you can just use sender. The sender parameter is a reference to whichever control is raising the event. So, if you add this same method as an event handler for multiple controls, sender will be which ever control raised the event that it's currently handling, for instance:
Private Sub MouseDown(sender As Object, e As EventArgs) _
Handles TextBox1.MouseDown, TextBox2.MouseDown
' Note in the line above that this method handles the event
' for TextBox1 and TextBox2
Dim textBox As TextBox = CType(sender, TextBox)
' textBox will now be either TextBox1 or TextBox2, accordingly
textBox.Capture = False
' ....
End Sub
The CType statement casts the base Object parameter to the specific TextBox class. In this example, the method only handles events for TextBox objects, so that will work. However, if you have it handle events from other types of controls, you'd need to cast to the more general Control type (i.e. CType(sender, Control)).

Winforms ListBox Control Not Updating After Source Changes

I have a ListBox (LB) with a DataTable (DT) DataSource in the Form Class globally populated in the Form_Load event.
Private Sub frmEditPresets_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DT.Columns.Add("DisplayText")
DT.Columns.Add("PresetID")
For Each TSI As ToolStripItem In Presets.DropDownItems
If TSI.Name.IndexOf("preset_") > -1 Then
DT.Rows.Add(TSI.Text, TSI.Name)
End If
Next
LB.DataSource = DT
LB.DisplayMember = "DisplayText"
End Sub
When I use my Rename button. It updates the menu item and the Data Source but the Listbox doesn't refresh until I click another item in the listbox.
Rename code:
Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click
Dim R As DataRowView = LB.SelectedItem
Dim S As String = InputBox("Preset Name", "Rename", R("DisplayText"))
If S.Trim.Length = 0 Then Exit Sub
If Presets.DropDownItems.ContainsKey(R("PresetID").ToString) Then
Presets.DropDownItems(R("PresetID").ToString).Text = S
End If
R("DisplayText") = S
End Sub
I'm sure this is a simple question with a simple answer but I can't seem to figure it out. I've tried Refresh(). I've tried setting the DataSource again. I read this StackOverflow question Winforms listbox not updating when bound data changes but ResetBindings() doesn't seem to be an available method in this context.
*Edit. I gave Steve credit for the answer as he mentioned BindingContext. Although, that led me to find BindingContext(DT).EndCurrentEdit() which updated my LB display and maintained the selection.
Tried with this, and it works.....
Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click
Dim R As DataRowView = LB.SelectedItem
Dim S As String = InputBox("Preset Name", "Rename", R("DisplayText"))
If S.Trim.Length = 0 Then Exit Sub
If Presets.DropDownItems.ContainsKey(R("PresetID").ToString) Then
Presets.DropDownItems(R("PresetID").ToString).Text = S
End If
R("DisplayText") = S
BindingContext(DT).EndCurrentEdit()
End Sub

How do you get the control that was clicked to open a ContextMenuStrip?

I'm using a ContextMenuStrip for multiple controls and I'm trying to figure out the best way to get the control that was actually clicked on to open the Context Menu. The sender just gives the ToolStripMenuItem reference, which has an Owner property that references the ContextMenuStrip, but I cannot figure out how to tell which control the click came from. There must be a simple way to check this, right? I'm checking it in the ToolStripMenuItem's click event.
Friend WithEvents mnuWebCopy As System.Windows.Forms.ToolStripMenuItem
...
Private Sub mnuWebCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuWebCopy.Click
I found a similar post about this, but that mentions using a SourceControl property which I do not see on here.
I'm using Visual Studio 2008, VB.Net winforms.
Private Sub mnuWebCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuWebCopy.Click
Dim myItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(myItem.Owner, ContextMenuStrip)
MessageBox.Show(cms.SourceControl.Name)
End Sub
Your sender is a ToolStripMenuItem -- cast it.
Its owner is a ContextMenuStrip -- get it.
SourceControl is a property on the ContextMenuStrip and references the last control from which the ContextMenuStrip was displayed.
Private Sub kdgToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles kdgToolStripMenuItem.Click
Dim sms = (sender.GetCurrentParent()).SourceControl.name
MsgBox(sms)
End Sub
'///Faster
Private Sub cmsRightClick_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cmsRightClick.MouseClick
Dim s As String = CType(sender, ContextMenuStrip).GetItemAt(CType(sender, ContextMenuStrip).DisplayRectangle.X, _
CType(sender, ContextMenuStrip).DisplayRectangle.Y + e.Y).Text.Trim()
MsgBox(s)
Select Case s
Case Is = "Select Summary Total"
Dim x = 0
Case Is = "Select Collections"
Dim x = 1
Case Is = "UnSelect"
Dim x = 2
Case Is = "Reconcile"
Dim x = 3
Case Is = "Undo Reconciliation"
Dim x = 4
End Select
End Sub
On VB.NET 2013 this work so fine:
Dim cms As ContextMenuStrip = CType(sender, ContextMenuStrip)
MessageBox.Show(cms.SourceControl.Name)