Is it possible to use Findcomponet method in vb.net? - vb.net

There are seven combo box on form
I want to add the item in each combo box. But i don't want to manual mode for add the item.
I want to use the ForLoop to add the item.
I try like this, but second line is get exception occurred.
Dim cmb_Temp as ComboBox = New ComboBox
cmb_Temp.Name = "cmb_a" + "7" <- exception
so, I don't know about how to handling the ForLoop to add the item.
is it impossible in vb.net?.
is it use the FindeComponet method?

Assuming WinForms, you can use Controls.Find() like this:
Dim matches() As Control
For i As Integer = 1 To 7
matches = Me.Controls.Find("cmb_a" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is ComboBox Then
Dim cb As ComboBox = DirectCast(matches(0), ComboBox)
cb.Items.Add("SomeItem")
End If
Next

Related

Accessing buttons names using variables

In visual basic I want to be able to access a button's name using the number stored in a variable.
For example if I have 24 buttons that are all named 'Button' with the numbers 1, 2, 3... 22, 23, 24 after it. If I want to then change the text in the first eight buttons how would I do that.
Here's my example to help show what I mean:
For i = 1 to 8
Button(i).text = "Hello"
Next
The proposed solutions so far will fail if the Buttons are not directly contained by the Form itself. What if they are in a different container? You could simple change "Me" to "Panel1", for instance, but that doesn't help if the Buttons are spread across multiple containers.
To make it work, regardless of the Buttons locations, use the Controls.Find() method with the "searchAllChildren" option:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ctlName As String
Dim matches() As Control
For i As Integer = 1 To 8
ctlName = "Button" & i
matches = Me.Controls.Find(ctlName, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is Button Then
Dim btn As Button = DirectCast(matches(0), Button)
btn.Text = "Hello #" & i
End If
Next
End Sub
For index As Integer = 1 To 8
CType(Me.Controls("Button" & index.ToString().Trim()),Button).Text = "Hello"
Next
Use LINQ and you're good to go:
Dim yourButtonArray = yourForm.Controls.OfType(of Button).ToArray
' takes all controls whose Type is Button
For each button in yourButtonArray.Take(8)
button.Text = "Hello"
Next
Or
Dim yourButtonArray = yourForm.Controls.Cast(of Control).Where(
Function(b) b.Name.StartsWith("Button")
).ToArray
' takes all controls whose name starts with "Button" regardless of its type
For each button in yourButtonArray.Take(8)
button.Text = "Hello"
Next
In any case, .Take(8) will iterate on the first 8 items stored inside yourButtonArray

What is the best way to loop this program?

This is one of the form, all the usercontrol value in this form will store in My.Settings
I have another form with a FlowLayoutPanel, everytime when the application start,
if Active checked then it will add a Button with discount value to the FlowLayoutPanel.
Should I add those usercontrol to a list and then loop through the list? Or what is the best way to solve this kind of problem?
UPDATED
How can I add multiple item to list in 1 code? I getting this error when system run to line 5
An exception of type 'System.NullReferenceException' occurred in XXX.exe but was not handled in user code
Additional information: Object reference not set to an instance of an object.
Public Sub RefreshDiscount(ByRef ref As scr_mainDiscount)
Dim li_disName As New List(Of TextBox)
Dim li_disValue As New List(Of TextBox)
Dim li_disType As New List(Of ComboBox)
Dim li_active As New List(Of CheckBox)
Dim tb_disName As TextBox() = {ref.tb_name1, ref.tb_name2, ref.tb_name3, ref.tb_name4, ref.tb_name5, ref.tb_name6, ref.tb_name7, ref.tb_name8, ref.tb_name9, ref.tb_name10}
Dim tb_disValue As TextBox() = {ref.tb_value1, ref.tb_value2, ref.tb_value3, ref.tb_value4, ref.tb_value5, ref.tb_value6, ref.tb_value7, ref.tb_value8, ref.tb_value9, ref.tb_value10}
Dim cb_disType As ComboBox() = {ref.cb_type1, ref.cb_type2, ref.cb_type3, ref.cb_type4, ref.cb_type5, ref.cb_type6, ref.cb_type7, ref.cb_type8, ref.cb_type9, ref.cb_type10}
Dim chkb_active As CheckBox() = {ref.CheckBox1, ref.CheckBox2, ref.CheckBox3, ref.CheckBox4, ref.CheckBox5, ref.CheckBox6, ref.CheckBox7, ref.CheckBox8, ref.CheckBox9, ref.CheckBox10}
li_disName.AddRange(tb_disName)
li_disValue.AddRange(tb_disValue)
li_disType.AddRange(cb_disType)
li_active.AddRange(chkb_active)
For index As Integer = 0 To li_active.Count - 1
If li_active(index).Checked = False Then
li_disName.RemoveAt(index)
li_disValue.RemoveAt(index)
li_disType.RemoveAt(index)
li_active.RemoveAt(index)
Else
Dim btn As New ctrl_DiscountButton
With btn
.Text = li_disName(index).Text
.Price = li_disValue(index).Text
.Type = li_disType(index).Text
End With
scr_sales.flp_discount.Controls.Add(btn)
End If
Next
li_disName.Clear()
li_disValue.Clear()
li_disType.Clear()
li_active.Clear()
End Sub
Here's a simple example showing how to find CheckBox1 thru CheckBox10, "by name", using the "searchAllChildren" option of Controls.Find():
For i As Integer = 1 To 10
Dim ctlName As String = "CheckBox" & i
Dim matches() As Control = Me.Controls.Find(ctlName, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then
Dim cb As CheckBox = DirectCast(matches(0), CheckBox)
' do something with "cb"
If cb.Checked Then
' ... code ...
' possibly use code just like this to find the matching discount value control?
End If
End If
Next

How can I loop through the systems.windows.forms.controls that has the control names in the sequential order?

I have radio buttons named in the sequential order like "Jbtn1' to "Jbtn20". I am trying to name those buttons using set toolTip by using for loop.
Dim toolTip1 As New ToolTip()
For j As Integer = 1 To 20
Dim pinInfo As String = "J2-" & j
'Tried to convert the buttonName as Control , but got an error as
'Value of string cannot be converted to systems.windows.forms.control
Dim buttonName As Control = "Jbtn" & j
toolTip1.SetToolTip(buttonName, pinInfo)
Next
Any suggestions are appreciated.
You can do something like this:
Dim toolTip1 As New ToolTip()
For j As Integer = 1 To 20
Dim pinInfo As String = "J2-" & j
Dim control As Control = Me.Controls.Item("Jbtn" & j)
toolTip1.SetToolTip(control, pinInfo)
Next
This code uses the form's Controls property to access controls on the form. You can lookup individual controls by their names using the indexer on Controls.
I would consider creating and adding the button dynamically rather then relying on the hard-coded name.
That said, you can use the ControlCollection(controlNameString) to find them by the name.
You can also use LINQ to Objects:
Dim radioButtons = From control In Me.Controls.OfType(Of RadioButton)
Where control.Name.StartsWith("Jbtn")
Dim toolTip = New ToolTip()
For Each radioButton In radioButtons
toolTip.SetToolTip(radioButton, radioButton.Name.Replace("Jbtn", "J2-"))
Next

Looping though elements in vb.net

Is there a way I can loop though multiple elements with number based names, changing their properties?
Example:
Here are an example set of elements on my form:
Element1
Element2
Element3
Element4
Element5
This is the kind of thing i was thinking of:
For i = 1 To 5
Element + i .BackColor = Color.Maroon
Next
This is just because I have a large number of elements, which i would like to change the properties of during the application running.
Thanks for any answers.
Sure, replace Me.Controls with another control collection if that is not the right one.
Label example:
For i = 1 To 5
Dim lb = TryCast(GameInterface.Controls("Element" & i.ToString), Label)
If lb IsNot Nothing Then lb.BackColor = Color.Maroon
Next
If it is for labels on form then try this:
replace parent with Parent control
Private Sub SetControls()
Dim xControl As Control
For Each xControl parent.Controls
If (TypeOf xControl Is Label ) Then
xControl.backcolor= Color.Maroon
End If
Next xControl
End Sub

Checking if text boxes are empty

I have a winform with a bound datagridview, 4 text boxes and a button. With the click of the button the "fillby" statement is called, the data is loaded on the datagridview and the content of the text boxes become the default value of some fields in the new rows of the datagridview.
I need to check all the text boxes to make sure they're not empty, if any of them is empty then a message should pop up saying which text box is empty and also keeps the datagridview from filling.
This is the code I have so far:
Private Sub btnCargarInformacion_Click(sender As System.Object, e As System.EventArgs) Handles btnCargar.Click
Dim emptyTextBoxes =
From txt In Me.Controls.OfType(Of TextBox)()
Where txt.Text.Length = 0
Select txt.Name
If emptyTextBoxes.Count >= -1 Then
MessageBox.Show(String.Format("Please fill following textboxes: {0}", String.Join(",", emptyTextBoxes)))
Else
Dim PartePersonalTableApt As New PersonalObraDataSetTableAdapters.PartePersonalTableAdapter
Dim PersonalObTableApt As New PersonalObraDataSetTableAdapters.PersonalObTableAdapter
PartePersonalTableApt.ClearBeforeFill = True
PartePersonalTableApt.FillByFecha(PersonalObraDataSet.PartePersonal, txtDate.Text, txtDepartamento.Text, txtTurno.Text)
PersonalObTableApt.ClearBeforeFill = True
PersonalObTableApt.Fillby(PersonalObraDataSet.PersonalOb)
End If
End Sub
I don't get any errors, the message appears even if all textboxes are filled, the message box does not specifying any text boxes as empty and stops the datagridview from filling.
I'm very new to codding so please explain in more detail your solution. Thanks
.Count >= -1
The count will be 0 if it's empty
If emptyTextBoxes.Count <> 0 Then
MessageBox.Show(String.Format("Please fill following textboxes: {0}", String.Join(",", emptyTextBoxes)))
Else
Dim PartePersonalTableApt As New PersonalObraDataSetTableAdapters.PartePersonalTableAdapter
Dim PersonalObTableApt As New PersonalObraDataSetTableAdapters.PersonalObTableAdapter
PartePersonalTableApt.ClearBeforeFill = True
PartePersonalTableApt.FillByFecha(PersonalObraDataSet.PartePersonal, txtDate.Text, txtDepartamento.Text, txtTurno.Text)
PersonalObTableApt.ClearBeforeFill = True
PersonalObTableApt.Fillby(PersonalObraDataSet.PersonalOb)
End If
You can try to loop at your container (ie form, panel, groupbox) for each textbox without content, then increment whenever there is a textbox that doesn't have content.
Example:
Dim checkr as integer = 0
Dim this As Control
For Each this In that.Controls
If TypeOf this Is TextBox Then
If this.text = "" then
Checkr += 1
End if
End If
Next
If checkr > 0 then
msgbox("Cannot proceed because a textbox has no content")
Else
'......(what you were gonna do)
End If