Synchronizing three comboboxes - vb.net

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!

Related

How to transfer data from listbox to textbox in Visual Basic

I did some activity but I cant figure out how to retrieve data from listbox1 to textbox1. Example in the listbox1 there are 4 names: John, Jorge, Joe. Then I want to transfer Joe from listbox1 to textbox1. I did an arraylist where I adding those 3 names in the listbox1 but I didn't know how to retrieve the name "Jorge" from listbox1 to textbox1. Send help.
Here's the code where I try to retrieve one of the name from listbox1 to textbox1
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Here's the whole code
Public Class Form1
Dim ArrayofNames() As String
Dim x As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Private Sub Addbtn_Click(sender As Object, e As EventArgs) Handles Addbtn.Click
Dim x As Integer = 0
ReDim ArrayofNames(x)
For x = 0 To ArrayofNames.Length - 1
ArrayofNames(x) = Addtextbox.Text
ListBox1.Items.Add(ArrayofNames(x))
Next
End Sub
Private Sub removeBtn_Click(sender As Object, e As EventArgs) Handles removeBtn.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
End Class
Here's the Image of the interface i try to retrieve the name Joe but it wasn'tshowing
Let's go over the code you posted.
In the retrieve button click event you are comparing and Integer to a String with textbox1.Text = ListBox1.Items.Count This won't compile with Option Strict On. You do have Option Strict On, don't you? You should always have this on.
On the next line, you assign the value of ArrayofNames(x), where x refers to your form level variable, to a text box's Text property. Since the value of x is never changed anywhere in the code this will always return the 0 index in the array. (The first element) I can't imagine why it should matter that the Textbox.Text should equal the count of the ListBox items.
In the add button click event you first declare a local variable x. Any code in the method will use this x, not the form level x. You ReDim (without the Preserve keyword) the array to an array with a single element. Your For loop is silly because the length of ArrayofNames is 1, so it is 0 To 0.
The ArrayofNames will never have more than a single element and it will be overwritten each time the add button is clicked.
Fortunately the Listbox maintains its own collection of items.
We can use this collection to simplify your code. Just add the contents of the text box to the the list items.
To retrieve a value from the list box you must first determine if the user has entered a valid index. First, is the entry a valid Integer and is it an index present in the list box.
Remember that .net collections start at index 0.
I use default names in my test program but you should continue to use meaningful names for your controls.
'Your add button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim index As Integer
If Integer.TryParse(TextBox1.Text, index) AndAlso index >= 0 AndAlso ListBox1.Items.Count - 1 >= index Then
TextBox2.Text = ListBox1.Items(index).ToString
End If
End Sub
'Your add button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox3.Text)
End Sub

Changing a Combobox selected Item

Here's what I need to do:
I have a ComboBox that has a whole list of items, for this example lets just call them 1, 2, 3.
If someone selects 3 I want to reset the ComboBox.
So if I was to select 3, the ComboBox would then return to its default blank state.
I just want to make this clear that I don't want the actual ComboBox to reset, that is, I don't want to remove the items I have listed in it. I just want the selection to go blank again.
As LarsTech said, set the SelectedIndex = -1. Here is a complete example with just a ComboBox on the form.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = Enumerable.Range(1, 10).ToList()
ComboBox1.SelectedIndex = -1
End Sub
Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
If CInt(Me.ComboBox1.SelectedValue) = 3 Then
ComboBox1.SelectedIndex = -1
End If
End Sub

Moving average method VB

i'd try to make moving average in vb
i want to check the cells and set the value to text box
but the result is all the text box has the same value
how to make my first check value (penjualan/bulan) is inputed into first text box and the second check (penjualan/bulan) to second text box.
here is my code
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = 5 Then
tb1.Text = DataGridView1.CurrentRow.Cells(3).Value
tb2.Text = DataGridView1.CurrentRow.Cells(3).Value
tb3.Text = DataGridView1.CurrentRow.Cells(3).Value
End If
End Sub
thanks.
You set EVERY time the cellClicked-event is raised all 3 Textboxes to the same value, CurrentRow.Cells(3).Value.
Another problem is that your code will set the text in the Textboxes always. It dont check if the Checkbox is checked or not. it just updated every time you click in any cell in this column, the text in the 3 boxes to the value of your currently selected row.
Here you have a solution. Its not perfect but should work, although you should try to understand and optimize it.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeDgv()
End Sub
Private Sub InitializeDgv ()
Dim row as String() = New String(){"2016",240}
DataGridView1.Rows.Add(row)
row = New String(){"2017",223}
DataGridView1.Rows.Add(row)
row = New String(){"2015",54}
DataGridView1.Rows.Add(row)
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
if e.ColumnIndex=2
Dim checkedRows=(From dgv as DataGridViewRow in DataGridView1.Rows where dgv.Cells(2).Value=True select dgv).ToList()
Dim controlsList As new List(of TextBox)
controlsList.Add(TextBox1)
controlsList.Add(TextBox2)
controlsList.Add(TextBox3)
for Each item in controlsList
item.Text=String.Empty
Next
for i=0 to checkedRows.Count-1
controlsList(i).Text=checkedRows.Item(i).Cells(0).Value
Next
End If
End Sub
End Class

Multiple selection in datagridview

I work on a new project that need a multiple row selection/deselection by the user in a datagridview with only a tap on a touch screen.
The form should look like this:
For exemple, if the user want to delete row 2 and 5, he only need to tap once on each line to select/deselect them. After the selection is done, he tap on "Delete Row" button.
I've already try to play with the CellClick event without success!!
Can someone have a clue how can I handle this problem?
After setting MultiSelect property to True and SelectionMode to FullRowSelect you can use a List to store which row of your DataGridView is selected.
On CellClick you can add/remove rows from your List, on RowPostPaint you can select a row if it's included in the List and on RowsRemoved you have to clear the List.
Private intSelectedRows As New List(Of Integer)
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
With CType(sender, DataGridView)
Dim intRow As Integer = .CurrentRow.Index
If Not Me.intSelectedRows.Contains(intRow) Then
Me.intSelectedRows.Add(intRow)
Else
.CurrentRow.Selected = False
Me.intSelectedRows.Remove(intRow)
End If
End With
End Sub
Private Sub DataGridView1_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
If Me.intSelectedRows.Contains(e.RowIndex) Then
CType(sender, DataGridView).Rows(e.RowIndex).Selected = True
End If
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
Me.intSelectedRows.Clear()
End Sub
If you want to clear selection you can use this code:
Private Sub btnClearSelectedRows_Click(sender As System.Object, e As System.EventArgs) Handles btnClearSelectedRows.Click
For Each intSelectedRow As Integer In Me.intSelectedRows
Me.DataGridView1.Rows(intSelectedRow).Selected = False
Next intSelectedRow
Me.intSelectedRows.Clear()
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