How to change selected item text in list box at run time? - vb.net

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

Related

VB.NET Trigger Datagridview Cell Click on Button CLick

I'm trying to trigger this command when the button is clicked
Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
TextBox1.Text = row.Cells(0).Value.ToString
TextBox2.Text = row.Cells(1).Value.ToString
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ClickDataGridview()
End Sub
but sadly I received two error
Argument not specified for parameter 'sender' of 'Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)'.
Argument not specified for parameter 'e' of 'Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)'
Should I make it an if statement to work? or should I try something else to trigger this event
There is a way but you have to careful about selection of cells . If you have to do only row operations then it is ok with this. I recommend don't do this instead that put button in gridview for each row and perform operation
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
If DataGridView1.SelectedCells.Count > 0 Then
'Here you can change Datagridview row selection property and get selectedrows instead of selected cells
Dim i_rowindex As Integer = DataGridView1.SelectedCells(0).RowIndex
Dim i_colIndex As Integer = DataGridView1.SelectedCells(0).ColumnIndex
DataGridView1_CellMouseClick(sender, New DataGridViewCellMouseEventArgs(i_colIndex, i_rowindex, 0, 0, New MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0)))
End
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
As I may have mentioned once or twice, don't call event handlers directly. Put the common code in it's own method and then call that method from each event handler as appropriate. In this case:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GetRowValues(DataGridView1.CurrentRow)
End Sub
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
If e.Button = MouseButtons.Left AndAlso e.RowIndex >= 0 Then
GetRowValues(DataGridView1.Rows(e.RowIndex))
End If
End Sub
Private Sub GetRowValues(row As DataGridViewRow)
TextBox1.Text = row.Cells(0).Value.ToString()
TextBox2.Text = row.Cells(1).Value.ToString()
End Sub
I may be missing something, however… I do not understand why you would want to force the user to “click” a button to set the text boxes. If you wire up the grids “SelectionChanged” event and update the text boxes in that event, then the user neither has to click on a button or a cell. If the user uses the Arrow keys, Enter key, Tab key or even “clicks” a cell, the text boxes will change automatically without the user having to click a button.
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If (DataGridView1.CurrentRow IsNot Nothing) Then
TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()
TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
End If
End Sub
Or... better yet... if the grid uses a data source, then "Binding" the text boxes to that "same" data source is all that is needed. You will not have to wire up any grid events.
TextBox1.DataBindings.Add(New Binding("Text", datasource, "datasourceColumnName1"))
TextBox2.DataBindings.Add(New Binding("Text", datasource, "datasourceColumnName2"))

Replacing listview items with another list using combo box

Pardon for being novice at vb.net. I have a combo box and a list view. What I needed is when I change the category in the combo box and pressed 'OK', the old list added before will be replaced by a new list.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub
What should I need to add?
What you are doing there is adding a different row on each case. What you ask is to fill the listview with different items depending on the item you pick at the combobox.
Basically, you need to clear the listview on each case. I'd do something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
ListView1.Items.Clear
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub

Dynamic Menustrip access 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

Suggest Append Count in combobox (vb.net)

I'm searching for a way to count my remaining suggest appends in my combobox.
In my example I have a list of 7 items
When I start to type with the suggest append function, this list gets narrowed down. But I don't see any possibility to count these remaining appends.
What my main objective is, is that I do an action once I have only 1 suggest append remaining.
But I can only check on the selectedindex, which is in this case always -1, or my comboboxcount is still 7. I don't see a way to count the remaining suggest appends.
Any idea?
Supposing that your combobox listitems are of string type then this code will do that. First you should create a list of string with combobox items. Then on keyup event of combobox you should create the searchtext which you use to filter list then count. See code below (i have shown also searchtext just to see its value):
Dim lst As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each it In ComboBox1.Items
lst.Add(it)
Next
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
Dim seltext = ComboBox1.SelectedText
Dim searchtext = ""
If seltext <> "" Then
searchtext = ComboBox1.Text.ToLower.Replace(seltext, "")
Else
searchtext = ComboBox1.Text.ToLower
End If
Label1.Text = lst.Where(Function(d) d.ToLower.StartsWith(searchtext)).Count & " - " & searchtext
End Sub
If your combobox listitems are of different object type then you have to populate list with text field of listitem.
I had the same basic idea as Shurki, except I didn't use a list or replace the selected text with a zero-length string.
I use the SelectionStart property of the ComboBox to get a substring from the ComboBox's Text property..
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ComboBox1.Items.Add("Candy")
ComboBox1.Items.Add("Car")
ComboBox1.Items.Add("Crush")
ComboBox1.Items.Add("Canned")
ComboBox1.Items.Add("Can")
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
Dim query As IEnumerable(Of Object) =
From item As Object In ComboBox1.Items
Where item.ToString().ToUpper().StartsWith(ComboBox1.Text.Substring(0, ComboBox1.SelectionStart).ToUpper())
Select item
Debug.WriteLine("Number of items: " & query.Count())
End Sub
End Class

Extract text from clicked submenu item of Menustrip

I've been dealing and looking for a way of extracting the text of any given subitem when clicked and write the text in textbox1.
this is the code that I have so far, but it does not seem to work.
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
Dim result As String
If AccionAToolStripMenuItem.Checked = True Then
result = AccionAToolStripMenuItem.Text
TextBox1.Text = result
End If
End Sub
You are using the wrong event. The ItemClicked event works for the items on the menu. You need to add a event for each of the subitems:
Sub SomeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SomeTSMenuItem.Click
TextBox1.Text = Ctype(sender, ToolStripMenuItem).Text
End Sub
You can make a function for each subitem or handle every event on the same function:
Handles item1.Click, item2.Click, item3.CLick
Try reading e.ClickedItem.Text.