How to Change index value - vb.net

hello guys i have a problem with index value in my combobox i have these items
(A)
(B)
(C)
(D)
(E)
(F)
these items have index value like 0 1 2 3 4 and so on
i want to start index 83 84 99 45 22 ... ?
ComboBox1.Items.Add(line)
is there way to index start 83 84 99 45 22 ... ?
Or any suggestion from in My.Resources.TextFile1
(A),83
(B),84
(C),99
(D),45
(E),22
(F),10
and so on
Items,Index
can i do that like index=0 to 83 and 1 = 84 son on
sorry for bad english

Not entirely sure what you are trying to achieve, but from what I can gather you want to add "(A)", "(B)" and so on to the combobox, but you want the combobox's index to start from numbers that you choose.Not sure if you can do that as it's like a primary key and there to only keep things in order.With that being said, you could use a Dictionary to achieve this to some extent.Here is how I would do it, think of the key as the index value
Public Class Form1
Dim items As New Dictionary(Of Integer, String) ''your items Dictinary List
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
''add items here.
items.Add(83, "(A)")
items.Add(84, "(B)")
items.Add(99, "(C)")
items.Add(45, "(D)")
items.Add(22, "(E)")
items.Add(10, "(F)")
For Each item As String In items.Keys ''add the items key or value to the list.
ComboBox1.Items.Add(item)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Label1.Text = items.Values(ComboBox1.SelectedIndex) ''we can select the value or key based on the items key or value that is allocated to it depending on which way you do it
End Sub
End Class
So now if you select an item from the combobox, the label will display the 'key' of the value that is selected.

Related

Updating Values in a Data Grid View With Key Press Data Entry in Numeric Up and Down

I am setting up a Data Grid View that contains a list of items, with one column as the Item Number (1, 2, 3) and the second number as the name of the items. I am controlling the input of the Item Number column using a Numeric Up and Down, so when I increase it, the rows are added and the Item Number column is automatically updated (so the user doesn't have to enter this in). However, this only works if I use the numeric up and down by clicking the arrows. If I type in the number (say 4 items), I get an exception (System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index') and the procedure doesn't work. The code is below:
Private Sub numItemNumber_ValueChanged(sender As Object, e As EventArgs) Handles numItemNumber.ValueChanged
While (dgvItems.Rows.Count < numItemNumber.Value)
dgvItems.Rows.Add()
i = numItemNumber.Value
dgvItems.Rows(i).Cells(0).Value = i + 1 'This is where the exception is
End While
End Sub
I added in a KeyPress event to see if that could handle the item entered in but it doesn't.
Private Sub numItemNumber_KeyPress(sender As Object, e As EventArgs) Handles numItemNumber.KeyPress
For i = 0 To numItemNumber.Value - 1
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End Sub
How can I edit this to include both events (the user using the up and down keys or just entering the number in directly)? Thank you.
Don't you still have to Add the new row before you can enter a value in it?
Private Sub numItemNumber_KeyPress(sender As Object, e As EventArgs) Handles numItemNumber.KeyPress
For i = 0 To numItemNumber.Value - 1
dgvItems.Rows.Add()
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End Sub
Figured out this works, essentially adding a loop through the rows of the data grid view in the Value Changed event so that it can handle any number > 0. Adding the code here for anyone who possibly needs it:
Private Sub numItemNumber_ValueChanged(sender As Object, e As EventArgs) Handles numItemNumber.ValueChanged
While (dgvItems.Rows.Count < numItemNumber.Value)
For i=0 to numItemNumber.Value-1
dgvItems.Rows.Add()
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End While
End Sub
The KeyPress Event is not needed.

One row selector in all Datagridview

Based on what you see below is the sample of a datagridview that an item has been selected.
Here is my question what if I have a more than 1 Datagridview? I mean 5 Datagridview like this.
All of them contains 1 column only. Based on the 1st image, the row selector or the blue one select an item.
My question is how can I make all of the datagridview only have one row selector?
What happens is when i selected each of them all of it has a row selected 5 selections.
How can I make 1 row selector for all of them.
Thinking of changing the Selection Color but I think that's not applicable.
TYSM for future help.
If you're looking for an alternative, you can also try this approach:
Private Sub DataGridView_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles _
DataGridView1.CellEnter, DataGridView2.CellEnter, DataGridView3.CellEnter, DataGridView4.CellEnter, DataGridView5.CellEnter
Dim MyDataGrids() As DataGridView = {DataGridView1, DataGridView2, DataGridView3, DataGridView4, DataGridView5}
For i = 0 To MyDataGrids.Count - 1
If MyDataGrids(i).Name = sender.Name Then
Continue For
Else
MyDataGrids(i).ClearSelection()
End If
Next
End Sub
MyDataGrids() is an array of DataGridViews. If for example, the controls you need to check increases, just add the name of the DataGridView in this array and it will be included in the checking and clearing of selections. Don't also forget the Handles event. As you can see here, all of the five grids .CellEnter event are included so you don't have to copy-paste it to five separate events.
Try this maybe it is more easy to edit if you add more grid
Private Sub ClearSelectedCells(ByVal Identifier As Integer)
If Identifier = 1 Then 'for datagrid 1
dg2.ClearSelection()
dg3.ClearSelection()
ElseIf Identifier = 2 Then 'for datagrid 2
dg1.ClearSelection()
dg3.ClearSelection()
'and so on
.
.
End If
End Sub
Private Sub dg1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dg1.CellClick
ClearSelectedCells(1)
End Sub
'and other gridcellclick
.
.

How can I mirror checked items from one CheckedListBox to another?

I have 2 checklistbox controls and want the items in the second control to mirror the checked state of those in the first. For example:
Checklistbox1 = APPLE, MANGGO, BANANA, STRAWBERRY, GRAPE
Then i checked manggo and grape.
checklistbox2 = 0,1,0,0,1
How do I go about this?
This should accomplish what you want. Note that if you have a CheckedListBox2_SelectedIndexChanged event, you could get unexpected results, as this code will trigger it.
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
Dim i As Integer
For i = 0 To CheckedListBox2.Items.Count - 1
CheckedListBox2.SetItemChecked(i, False)
Next
For Each i In CheckedListBox1.CheckedIndices
CheckedListBox2.SetItemChecked(i, True)
Next
End Sub
If you have a large list, this might be a bit more efficient, but you end up with the same result.
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
Dim i As Integer
For i = 0 To CheckedListBox2.Items.Count - 1
CheckedListBox2.SetItemChecked(i, CheckedListBox1.GetItemCheckState(i))
Next
End Sub
Also you might want to have the checkonclick property of your listboxes set to true to save you having to click the item twice - and it yields more consitent results with both my code and the code from #josh , but if you need to do anything else when you're selecting an item, you might want it turned off

vb: How to select multiple items in a Listbox at once?

I have a Listbox with a list of numbers from 1 to 10.
Now I want to program to select those numbers greater than 5. But I also want to trigger the SelectedIndexChanged event only once.
I know I can add multiple items into Listbox at once by using addrange() method.
But it seems there isn't a similar solution for select multiple item at once ?
How can i do this ?
youre question is a litle unclear but ...
First you need to set your Listbox SelectionMode to MultySimple.
Then you use ListBox1.SelectedItems.Count < 2 So it trigger the SelectedIndexChanged event only once at the start of the selecting.
Of course you can edit the code to fit your needs and let it trigger whenever you want.
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItems.Count < 2 Then
MsgBox("one")
End If
End Sub
And to select everything above 5 you need to make a list of integers.
Then use a for each loop to get the items in the list of integers with the values you want.
Then use the list of integers in a for loop to select the items in the listbox.
Dim l As New List(Of Integer)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Item As Integer In ListBox1.Items
If Item > 5 Then
l.Add(ListBox1.FindString(Item))
End If
Next
For SetItem As Integer = 0 To l.Count - 1
For i = 0 To ListBox1.Items.Count - 1
If i = l.Item(SetItem) Then
ListBox1.SetSelected(i, True)
Exit For
End If
Next
Next
End Sub

multidimension Arrays and Listboxes (VB.Net)

I'm new to VB.Net and feel a bit confused about something.
I want to have two list boxes, in each ones I already have items. In my first list box I have 4 items, in my second List box I have 5 items. I have also added a text box, for the value I want to be stored in the array.
For example: if I select the first value of the first text box, and the second value of the second text box and type "5" in the text box, 5 will be stored in (0,1) of the array.
I then want all the values for each item of my first list box to be displayed in a label, same for the second items, third item, and fourth item. I suppose I would need a loop for this.
I know how to create an array, and how to store values in an array, but I can't seem to figure out how to get it to work using list boxes and a text box.
I've created a form with the following controls:
ComboBox1
ComboBox2
Button1
TextBox1
I've added code to the Form_Load and Button1_Click events and created a single ComboBox_SelectedIndexChanged event handler to deal with both combo boxes index changes.
Public Class Form1
Private _array(,) As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ReDim _array(0 To ComboBox1.Items.Count, 0 To ComboBox2.Items.Count)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim c1 As Integer = If(ComboBox1.SelectedIndex = -1, 0, ComboBox1.SelectedIndex)
Dim c2 As Integer = If(ComboBox2.SelectedIndex = -1, 0, ComboBox2.SelectedIndex)
Debug.Print(String.Format("Set ({0},{1}) to {2}", c1, c2, TextBox1.Text))
_array(c1, c2) = TextBox1.Text
End Sub
Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
Dim c1 As Integer = If(ComboBox1.SelectedIndex = -1, 0, ComboBox1.SelectedIndex)
Dim c2 As Integer = If(ComboBox2.SelectedIndex = -1, 0, ComboBox2.SelectedIndex)
Debug.Print(String.Format("Get ({0},{1}) to {2}", c1, c2, TextBox1.Text))
TextBox1.Text = _array(c1, c2)
End Sub
End Class
What I'm demonstrating is that:
1. the array is resized when the form is loaded to match the number of elements in your comboboxes.
2. the data is loaded into the array on an event (in this case the button click event).
3. Retrieving the data again as either of the combo boxes changes.
Hope that helps.