How to concat variable integer in control name in vb.net - vb.net

Now I have a database and pull out that data and display it to form,i have a sequence of groupbox and radiobuttons, in each groupbox (groupbox1,groupbox2,etc...) there are 2 radio buttons namely rdbtn1Yes and rdbtn1No (then it increment +1 in next Groupbox). now i use for loop to go through every groupboxes and radio buttons. And this is my code:
Dim sqlda As New SqlDataAdapter("SELECT * FROM table1 WHERE column1= '" & lblWONo.Text & "'", Constr)
Dim sqlds As New DataSet
sqlds.Clear()
sqlda.Fill(sqlds)
If sqlds.Tables(0).Rows.Count > 0 Then
With sqlds.Tables(0).DefaultView.Item(0)
txtDateCreated.Value = .Item(0).ToString
txtComments.Text = .Item(1).ToString
'check column if it contain FALSE/TRUE value
'then toggle the radiobutton state to TRUE
'In this part i know there is another/easiest way to checked radio buttons to TRUE value
'and this is my code using looping (below):
If .Item(2) = False Then
rdbtn1No.Checked = True
Else
rdbtn1Yes.Checked = True
End If
If .Item(3) = False Then
rdbtn2No.Checked = True
Else
rdbtn2Yes.Checked = True
End If
If .Item(4) = False Then
opt3N.Checked = True
Else
opt3Y.Checked = True
End If
End With
End If
SAMPLE CODE FOR LOOPING:
Dim itemNo As Integer
Dim rdbtnSet As Integer = 1
Dim grpboxCnt As Integer = 1
For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()
For itemNo = 2 To sqlds.Tables(0).Columns.Count
If .Item(itemNo) = True Then
rdbtn & rdbtnSet & "Yes".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
Else
rdbtn & rdbtnSet & "No".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
End If
Next
rdbtnSet += 1
grpboxCnt += 1
Next
Thats all. Thank you in advance!

Think about the use of a dictionary (id, control) to store your controls. Then iterate the dictionary and set your state.

Related

DataGridView live search data

I am trying to add a search function to a DataGridView in vb.net using this code
For i As Integer = 0 To ContactsList.RowCount - 1
For j As Integer = 0 To ContactsList.ColumnCount - 1
If ContactsList.Rows(i).Cells(j).Value.ToString.ToLower.Trim = ContactsListSearch.Text.ToLower.Trim Then
MsgBox("Item found " + i.ToString)
ContactsList.Rows(i).Visible = True
Else
ContactsList.Rows(i).Visible = False
End If
Next
Next
I'm seeing the MsgBox show when the value matches, but the rows are not showing, it just hides all rows
Another possible option is to load data into a DataTable, create a BindingSource, set the BindingSource DataSource property to the DataTable. Set the DataGridView DataSource property to the BindingSource.
The following example works against a column in the DataTable. If the user clears the TextBox the filter is removed while if there is text filter with trim.
Private Sub SearchButton_Click(sender As Object, e As EventArgs) _
Handles SearchButton.Click
If String.IsNullOrWhiteSpace(SearchTextBox.Text) Then
bindingSource.Filter = ""
Else
Dim currentRowCount = bindingSource.Count
bindingSource.Filter = $"TRIM(LastName) = '{SearchTextBox.Text}'"
MessageBox.Show($"Before: {currentRowCount} Now: {bindingSource.Count}")
End If
End Sub
Edit If the column name might be variable consider loading a ComboBox with column names then adjust code as follows.
bindingSource.Filter = $"TRIM({FilterComboBox.Text}) = '{SearchTextBox.Text}'"
In most cases working against a data source is better than working against cells values as per the above recommendation.
I added 2 Exits in the IF statement when the value matches as it's searching each column as well, so it was causing them to be hidden as it loops columns too
For i As Integer = 0 To ContactsList.RowCount - 1
For j As Integer = 0 To ContactsList.ColumnCount - 1
If ContactsList.Rows(i).Cells(j).Value.ToString.ToLower.Trim = ContactsListSearch.Text.ToLower.Trim Then
MsgBox("Item found " + i.ToString)
ContactsList.Rows(i).Visible = True
Else
ContactsList.Rows(i).Visible = False
End If
Next
Next

Select only 1 item from Listview

I have some code that reads the value of a Listview on a button click. However, what it does is select all the entries to add to another Listview. How do I adjust my code to allow for single items selection. Thanks
lvSelectRequestItems.Items.Clear()
While dr.Read()
Dim LVS As New ListViewItem
'LVS.SubItems.Clear()
With (LVS)
.UseItemStyleForSubItems = False
.Text = dr("Box").ToString()
.SubItems.Add(dr("CustRef").ToString())
End With
lvSelectedItems.Items.Add(LVS)
End While
If ListViewObj.SelectedItems.Count >= 1 Then
While ListViewObj.SelectedItems.Count >= 1
ListViewObj.SelectedItems(0).Selected = False
End While
ListViewObj.Items(ListViewObj.FocusedItem.Index).Selected = True
End If

VB.NET: How to dynamically select a list view item?

I need to dynamically select an item in a listview based on what was selected previously.
The items that have been selected in the past are retrieved from a database and added to an Arraylist. These items then need to be selected from a number of different listviews.
Doing this by index like so listRef1.Items(2).Checked = True is no problem but I need to do it by the item text, i.e. one of the strings in the array.
So far I have this:
For i As Integer = 0 To refsArr.Count - 1
'find the correct category id
Dim cmdRefCat As New SqlCommand("SELECT RefID from ReferencesListTable WHERE RefName = '" & refsArr(i) & "'", conn)
Dim refid As Integer = cmdRefCat.ExecuteScalar()
If refid = 1 Then
listRef1.Items(refsArr(i)).Checked = True
ElseIf refid = 2 Then
listRef2.Items(refsArr(i)).Selected = True
listRef2.Select()
ElseIf refid = 3 Then
listRef3.Items.Item(refsArr(i)).Selected = True
listRef2.Select()
ElseIf refid = 4 Then
listRef4.Items.Item(refsArr(i)).Selected = True
End If
Next
Has anyone got any ideas on this? Thanks.
You'll need to loop through each item in the listview list:
For I as Integer = 0 to ListView.Items.Count - 1 Do
If ListView.Items(i).Text = "Text" then
ListView.Items(i).Selected = true
End If
End For
You can try this ...
For i As Integer = 0 To refsArr.Count - 1
'find the correct category id
Dim cmdRefCat As New SqlCommand("SELECT RefID from ReferencesListTable WHERE RefName = '" & refsArr(i) & "'", conn)
Dim refid As Integer = cmdRefCat.ExecuteScalar()
Select case refid
case 1
CheckIt(refsArr(i),listRef1)
case 2
CheckIt(refsArr(i),listRef2)
case 3
CheckIt(refsArr(i),listRef3)
case 4
CheckIt(refsArr(i),listRef4)
End Select
Next
And Sub CheckIt
Sub CheckIt(ByVal sRef as String, ByRef lvw as Listview)
Dim x as Integer
For x = 0 to lvw.Items.Count - 1
If lvw.Items(x).Text = sRef then
lvw.Items(x).Selected = true
exit for '-- if only 1 record
End If
Next
End Sub
The code to select an item dynamically from the listview control can be as follows for vb.net.
Let lvwomominiChair1 is the name of the listview control.
Set its fullrowselect property as true.
The code will select the text in the first column of the listview control.
Private Sub lvwomominiChair1_Click(sender As Object,e As EventArgs) Handles lvwomominiChair1.Click
Dim lvwitem as ListViewItem
lvwitem = lvwomominiChair1.SelectedItems.Item(0)
MsgBox("Selected item is " + lvwitem.Text)
End Sub
There may be situations where we need to get all items in a row of a ListView control.The following code may be used for the purpose.It is assumed that there are five columns of data in a raw and are of the text data type.This can be done with a For..Next loop as follows.Let 0,1,2,3 and 4 are the five column indices.
Private Sub lvwomominiChair1_Click(sender As Object,e As EventArgs) Handles lvwomominiChair1.Click
Dim i As Int32
Dim str As String
str =""
For i =0 To 4
str = str + " " + lvwomominiChair1.SelectedItems(0).SubItems(i).Text
Next
MsgBox("Selected items of the five columns of the row are " + str)
End Sub
Or you can do this, works perfect for me:
ListView.Items(0).Selected = True
ListView.Select()

Resetting Datagridview combo box data at runtime

I am creating grid on form load event.
Initially I am setting some values in datagridview combobox as :
Dim dgvc As DataGridViewComboBoxCell
datagrigview1.Rows(0).Cells("Column1").Value = txtColumn1.Text \\setting selected item
datagrigview1.Rows(0).Cells("Column1").Value = txtColumn2.Text
dgvc = datagrigview1.Rows(0).Cells("Column1").Value
dgvc.Items.Add((" ")) \\adding blank
dgvc.Items.Add(txtColumn1.Text) \\then required value
dgvc = datagrigview1.Rows(0).Cells("Column1").Value
dgvc.Items.Add((" "))
dgvc.Items.Add(txtColumn2.Text)
Now when user clicks on particular combobox.I am setting new values in it as :
// Resetting old values
If IsDBNull(dgvc) = False Then
dgvc.DataSource = Nothing
dgvc.Items.Clear()
End If
If DtTable.Rows.Count > 0 Then
Dim k As Integer
Dim dgvc1 As DataGridViewComboBoxCell
dgvc1 = New DataGridViewComboBoxCell()
For k = 0 To DtTable.Rows.Count - 1
If DtItemCd.Rows(k)("ItemCd").ToString <> Current_Code Then
datagrigview1.Rows(e.RowIndex).Cells("Column1").Value = DtTable.Rows(k)("Column1").ToString
dgvc1 = datagrigview1.Rows(e.RowIndex).Cells("Column1")
dgvc1.Items.Add(DtTable.Rows(k)("Column1").ToString)
datagrigview1.Rows(e.RowIndex).Cells("Column2").Value = DtTable.Rows(k)("Column2").ToString
dgvc1 = datagrigview1.Rows(e.RowIndex).Cells("Column2")
dgvc1.Items.Add(DtTable.Rows(k)("Column2").ToString)
End If
Next
End If
This shows both old and new records.Please help.
Probably your code here
If IsDBNull(dgvc) = False Then
is testing if the DataGridViewComboBoxCell is Null not if it's DataSource is null.
Hence it never enters the condional code below.
Could you try to change in this way and see if now you enter in the if condition?
If IsDBNull(dgvc.DataSource) = False Then
I got below solution :
If IsDBNull(dgvc) = False Then
dgvc.Items.Clear()
dgvc.DataSource = Nothing
dgvc = dgvSO.Rows(e.RowIndex).Cells("Column1")
dgvc.Items.Remove(" ")
dgvc.Items.Remove(Current_Code)
End If
This works
cb.SelectedItem = Nothing

How can I create a loop that reveals a certain number of labels?

I want to be able to reveal and then populate a certain number of labels. The inefficient way would be to SELECT CASE on the number of labels required and then populate these in turn. I am looking for something like this:
For i = 1 to RequiredNumOfLabels
Label & i.visible = true
Label & i.text = DataTable.Rows(i).Item(2)
Next
Thank you.
EDIT:
For i = 1 To NumberOfItems
Dim lbl = Controls("lbl" & i)
lbl.Visible = True
lbl.Text = CStr(DataTable.Rows(i).Item(2))
Next
I think the line
Dim lbl = Controls("lbl" & i)
is the problem as after the line is executed, lbl still equals nothing.
The reasoning behind it is that I was trying to create an invoice generator in vb.net and I was hoping that this would be a simple way to do it - count the amount of items in the order, populate the labels with the names of the items, reveal that many labels.
If your label controls are really in order like that, you can try just referencing them from a list:
Dim myLabels As New List(Of Label)
myLabels.Add(Label1)
myLabels.Add(Label2)
Then just update them:
For i as Integer = 1 to myLabels.Count
myLabels(i - 1).Visible = True
myLabels(i - 1).Text = DataTable.Rows(i).Item(2).ToString
Next
You can get controls by name through the forms Controls property
For i = 1 To RequiredNumOfLabels
Dim lbl = TryCast(Controls("Label" & i), Label)
If lbl IsNot Nothing Then
lbl.Visible = True
lbl.Text = CStr(DataTable.Rows(i).Item(2))
End If
Next
Since you get an object of type Control, you have to cast it to Label.
UPDATE
It seems that you only use properties that are defined in Control anyway. Therfore you can simplify the code to
For i = 1 To RequiredNumOfLabels
Dim lbl = Controls("Label" & i)
lbl.Visible = True
lbl.Text = CStr(DataTable.Rows(i).Item(2))
Next