How can I mirror checked items from one CheckedListBox to another? - vb.net

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

Related

How can I write data into a row in Virtual Studio? (MS Access Database file"

I would like to make a small application what can make easier my work.
I have to make one ordering process some labels where I have a plan and from the plan I have to one excel list making with the labelnames what will be printed by the Manufacturer. We have a lot of type of the label (names) but they are fixing. (160 pieces differentnames).
So I make this always if I get a new plan then I make new label-list. I do it alway by hand cell by cell.. Sometime I have to filling out 400 cell for one plan... I don’t have a lot’s of time to this..
So I started making my first Windows app with Visual Studio(in Windows From – Visual Basic).
I found some cool video, so I already have the „basics”.
I connected one MS Access database to my Project where I would like to store the datas.
And now i have trouble.. I have some Comboboxes. I would like to choose some cases and then I would like to update the database where the rows will be filled with the correct values. If its done, i can this exporting to excel and sendig forward to the Manufacturer.
I have a textbox where I write the Plan name (it linked to the database first Column)
And then:
So the Combobox1 have Sektor A, B, C, D ,E, F (six value)
The Combobox2 have 09RRU, 09RSU, 18RRU ... empty - (eight value)
The Combobox3 have 21RRU, 21RSU, 26RRU ... empty - (eight value)
The Combobox4 have ja or nein (only two value)
and so on.. I have 8 Combobox.
My Idea:
If I select the Sektor A and 09RRU , the another two is empty and nein, then I click on the Update button I would like to get back in database 09.SekA1, 09.SekA2, AISG.SekA
If I select the Sektor A and 09RRU and 21RRU and ja, then after click Update, I would like get 09.SekA1, 09.SekA2, 21.SekA1, 21.SekA2, 09.21.SekA1, 09.21SekA2, AISG.09.21.SekA
….
I can every type of labels line by line writing if needed, I think I have to do this. I don’t think so can I dynamic Arrays making which can the different String e.g. ( Text (09) & Text (.SekA) & Text(1)) creating. I just now started the VB..
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox8.SelectedIndexChanged
SektorForm1()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
End Sub
Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
End Sub
Private Sub SektorForm1()
BEschriftungenDataSet.Standort.RRU18Column = "18.SekA1"
BEschriftungenDataSet.Standort.RRU21Column = "18.SekA2"
BEschriftungenDataSet.Standort.RRU26Column = "AISG.18.SekA"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.StandortTableAdapter.Fill(Me.BEschriftungenDataSet.Standort)
ComboBox1.SelectedIndex = 5
ComboBox2.SelectedIndex = 3
ComboBox3.SelectedIndex = 3
ComboBox4.SelectedIndex = 1
ComboBox5.SelectedIndex = 1
ComboBox6.SelectedIndex = 1
ComboBox7.SelectedIndex = 1
ComboBox8.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
On Error GoTo SaveErr
StandortBindingSource.EndEdit()
StandortTableAdapter.Update(BEschriftungenDataSet.Standort)
MessageBox.Show("Saved")
End Sub
Could some one for me Helping what is wrong in my code? I tried some data inserting to tha database when I choose someting in Combobox1 but it doesn't work..
I get failures: BC30526 Property'RRU18Column' is 'ReadOnly' and BC30311 Value 'String' cannot be converted to 'DataColumn'..
And giving some help how I sould staring building up my cases to my Combobox chooses..
Thanks guys

Checkboxlist applying If statements

I am stating VB and since it is so close to VBScript I have been having fun with it. But now I have come across the "Checkboxlist".
My boss saw me making a Windows Forms Application and asked me to make him a interface (GUI) for one of his batch files. In the batch you start by choosing between lines 1 through 10 and it does the rest. So I made a Checkboxlist and made check-boxes going from 1 to 10. Now I am not sure how to tell it that when I click a button a if statement looks at what has been checked and take to appropriate action.
I think i am suppose to start with something like
If CheckedListBox1.Items() = True then
But i know this does not work.
Anything thing will help.
Thank you.
It sounds like you're looking for the ItemCheck event. This event is fired when the checked state of an item changes.
Private Sub HandleCheckedListBox1ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
Dim item As Object = Me.CheckedListBox1.Items.Item(e.Index)
Dim text As String = Me.CheckedListBox1.GetItemText(item)
Select Case e.CurrentValue
Case CheckState.Unchecked
'...
Case CheckState.Checked
'...
Case CheckState.Indeterminate
'...
End Select
End Sub
Or iterate all checked items:
Private Sub HandleButton1Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each item As Object In Me.CheckedListBox1.CheckedItems
Dim text As String = Me.CheckedListBox1.GetItemText(item)
'...
Next
End Sub

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

Deleting a record from the datagridview

In VB2012, I have a created a student management software with SQL2012 as the back-end. The below code is used for deleting a selected record from the datagridview when DELETE button is clicked:
Private Sub Delete_Click(sender As Object, e As EventArgs) Handles Delete.Click
Dim DelStudent As StudentDataSet1._masterRow
DelStudent= CType(CType(StudentmasterBindingSource.Current, DataRowView).Row, StudentDataSet1.Student_masterRow)
DelStudent.Delete()
Me.Validate()
Me.StudentmasterBindingSource.EndEdit()
Me.Student_masterTableAdapter.Update(StudentDataSet1.Student_master)
End Sub
But instead of the selected row being deleted, the first row gets deleted. Please help...
Selected row is not necessary current, from the perspective of binding source. You need to be using DataGridView.SelectedRows. Each is a DataGridViewRow, and you should be able to retrieve DataGridViewRow.DataBoundItem, cast it to DataRow, and use Delete on that.
the code below is also helpful
Private Sub btnRemoveItem_Click(sender As Object, e As EventArgs) Handles btnRemoveItem.Click
If datagridviewname.SelectedRows.Count > 0 Then
For i As Integer = datagridviewname.SelectedRows.Count - 1 To 0 Step -1
datagridviewname.Rows.RemoveAt(datagridviewname.SelectedRows(i).Index)
Next
Else
MessageBox.Show("Select Row to remove")
End If
End Sub

Getting row number in a DataGridView

How do you get row number DataGridView cell? Specifically, if a user has selected a single cell, how can you get that row number? It needs to access a particular cell based on what the user has selected.
I know that the RemoveAt method can be used to remove at the Focus, but you cannot get the row number at focus apparently?
Thanks for the help!
You can simply use RowIndex on the current cell:
var row = dataGridView1.CurrentCell.RowIndex;
this one works fine .
Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
If e.RowIndex >= 0 Then
Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
End If
End Sub
It is nearly the same but you may also use this solution:
var row = dataGridView1.CurrentRow.Index
Another way if you need to track user interaction with a DataGridView:
In my case there is extra processing in some generic functions that use the Me.I_SelCol and Me.I_SelRow column and row coordinates, but I haven't shown that because it's not relevant to the OP.
Best regards,
Rob
Private Sub I_DataGridView_CurrentCellChanged(sender As Object, e As EventArgs) Handles I_DataGridView.CurrentCellChanged
If Me.I_DataGridView.CurrentCellAddress.X < 0 Or Me.I_DataGridView.CurrentCellAddress.Y < 0 Then Exit Sub
' The Windows Me.I_DataGridView object will have already deselected the current cell and selected the
' new cell as per user navigation using mouse or cursor keys. We just need to store the current
' co-ordinates for the currently selected cell.
Me.I_SelCol = Me.I_DataGridView.CurrentCellAddress.X
Me.I_SelRow = Me.I_DataGridView.CurrentCellAddress.Y
Exit Sub
If you in event procedure of datagridview you will found "e As DataGridViewCellEventArgs" you can get row number with e.RowIndex and column number with e.ColumnIndex
Private Sub myDGV_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles myDGV.CellLeave
Dim myRow As Integer = e.RowIndex
End Sub
And if you in normal procedure you can get row number like this:
Private Sub btnGetRow_Click(sender As Object, e As EventArgs) Handles btnGetRow.Click
dim myRow1 as Integer = 0
dim myRow2 as Integer = 0
myRow1 = myDGV.CurrentCell.RowIndex
myRow2 = myDGV.CurrentRow.Index
End Sub