Verify multiple comboboxes/textboxes have value - vb.net

I have a winform that has about 6 comboboxes and 3 textboxes. I also have a button that takes the values of aforementioned controls and inserts them into SQL. Is there a better way, than a bunch of nested if/then statements, to verify that the controls all have values before inserting the data? This gets ugly very fast. I have tried googling this answer, but I get too many ASPx answers. I will entertain any ideas you may have. I am just trying to find a better way to do this. Thanks for any help.

I think its not too hard.
This code shows error message about the first empty textbox or combobox. But you can easily modify to show error message for all empty comboboxes and textboxes if required.
Private Function ValidateMyControls() As Boolean
Dim allComboBoxes() As ComboBox = {ComboBox1, ComboBox2, ComboBox3} ' add all your comboboxes here
Dim allTextBoxes() As TextBox = {TextBox1, TextBox2, TextBox3} ' add all your textboxes here
Dim emptyTB As TextBox = allTextBoxes.Where(Function(f) f.Text = "").FirstOrDefault
Dim emptyCB As ComboBox = allComboBoxes.Where(Function(f) f.SelectedIndex = -1).FirstOrDefault
If emptyTB IsNot Nothing Then
MessageBox.Show("Please fill value in " & emptyTB.Name)
Return False
ElseIf emptyCB IsNot Nothing Then
MessageBox.Show("Please select a value in dropdown " & emptyCB.Name)
Return False
Else
' All set to go!
Return True
End If
End Function

Related

Append Strings and values together to target a form control in VBA

I'm so close to getting this code working, I just need a little push please. I would like to
take the name of a combo box and then add a string to the end, But then get the value of a textbox with that string. This is to create a dynamic function instead of pasting the same code over and over.
Here's what I have so far, after you select something in the dropdown, the data is then pulled to populate the boxes next to it. I have about 8 drop downs so far so that's why I need this to work.
'Combobox after update
Call GrabData(Me, Me.ActiveControl)
Then
Private Sub GrabData(ctl As Control)
'name of ctl/combobox is "Kitchen"
data1 = (ctl.Name & "Size") '"KitchenSize"
'Here is where it all goes wrong
data1.Value = size.value
'size.value is just a textbox for example
End Sub
I can debug this with:
msgbox(data1)
'outputs "KitchenSize"
But I cannot get the value of kitchensize's textbox with data1.value
Error:
Object Required
I have also added Dim As String / Dim As Control.
I will be assigning the variable to some other stuff in this 50 line code I wrote so please don't take the above example as exactly what I intend to do, I just need help appending the ctl.name to a string, then use that to reference another control and so on.
EDIT
For anyone who wants to know, I figured it out.
Dim Ctrl As Control
Dim CtrlName As String
CtrlName = ctl.Name & "Size"
Set Ctrl = Me.Controls(CtrlName)
Ctrl.Value = 'Wherever you want to send the values to
See the edit.
You need to dim it as a string, then use Set Ctrl

How can I get the checkbox.checked event to fire when I pass it as a parameter in vb.net?

I have programmed a long time but I’m relatively new to vb.net. And I’ve avoided subroutines and functions in which I passed parameters because I always get stuck. I’m trying to write a subroutine to pass information that will fill a TextBox or a checkbox with either the value from a table or clear the field or set to false. The first code below is an example of what I’ve been doing and this works. I trying to write a subroutine to pass 1.the name of the textbox or checkbox control on my form,2.the data row value, and 3.the column name in the table. The problem is when I passed a checkbox I can’t get the checked event to show on my control(CoreCol) that I passed. It knows it’s a checkbox and it will set the text of the checkbox too true or false but it won’t change the box checked.
This is an example of the old way that works. For a TextBox and a checkbox
' A Machine
If Not IsDBNull(r("A Machine")) Or Not IsNothing(r("A Machine")) Then
TbXMachA.Text = r("A Machine")
Else
TbXMachA.Text = ""
End If
If Not IsDBNull(r("A CO2 Box?")) Or Not IsNothing(r("A CO2 Box?")) Then
CkbxCO2BoxA.Checked = r("A CO2 Box?")
Else
CkbxCO2BoxA.Checked = False
End If
This works
LoadData2TextBox(Me. TbXMachA, r, "A Machine ")
This doesn’t
LoadData2TextBox(Me.CkbxCO2BoxA, r, "A CO2 Box?")
this is the sub routine I'm writing
Private Sub LoadData2TextBox(ByRef CoreCol As Control, CoreRow As DataRow, BoxStage As String)
If Not IsDBNull(CoreRow(BoxStage)) Then
If TypeOf CoreCol Is TextBox Then
CoreCol.Text = CoreRow(BoxStage)
End If
If TypeOf CoreCol Is CheckBox Then
CoreCol.??? = CoreRow(BoxStage)
End If
Else
CoreCol.Text = ""
End If
You know that CoreCol is a CheckBox so you can cast it as one then use it as a CheckBox.
If TypeOf CoreCol Is CheckBox Then
Dim myCheckBox = DirectCast(CoreCol, CheckBox)
myCheckBox.Checked = DirectCast(CoreRow(BoxStage), Boolean)
End If
Another cast in getting the boolean value out of CoreRow(BoxStage). The above code assumes this will work, but I am not sure what is in CoreRow(BoxStage). You may need to add some logic based on the value depending on what it is. For example:
myCheckBox.Checked = CoreRow(BoxStage) = "somevalue"

How do I fill the first empty TextBox in a GroupBox?

I have a GroupBox with multiple TextBox controls and I can check to see if any are empty just fine but I'm trying to make my program fill the first empty textbox it finds in the GroupBox.
Code:
Dim empty = From txt In grpbill.Controls.OfType(Of TextBox)()
Where txt.Text.Length = 0
If empty.Any Then
End If
Any ideas?
You're almost there. I'm unsure on how you define first but I've gone off the TabIndex property:
Dim firstEmptyTextBox As TextBox = (From txt In GroupBox1.Controls.OfType(Of TextBox)()
Where txt.Text.Length = 0
Order By txt.TabIndex Ascending).FirstOrDefault()
If firstEmptyTextBox IsNot Nothing Then
firstEmptyTextBox.Text = "Text"
End If
You can use the .FirstOrDefault() method:
Returns the first element of a sequence, or a default value if the sequence contains no elements.
In my example I have three TextBox controls. The first one has the text Not empty whilst the other two have nothing. When I run the code, this is my output:
Here another idea which will also work in cases where all TextBox controls in GroupBox are not empty.
Private Sub SetTextJForFirstEmptyTextBoxIfExists(text As String)
Dim emptyTextBoxes As IEnumerable(Of TextBox)
= grpbill.Controls.
OfType(Of TextBox)().
Where(Function(txtbox) txtbox.Text.Length = 0)
For Each emptyTextBox In emptyTextBoxes
emptyTextBox.Text = text
Exit Sub
Next
End Sub

How to check if a textbox is empty VB.Net [duplicate]

This question already has answers here:
Check for empty TextBox controls in VB.NET
(7 answers)
Closed 6 years ago.
I want to test if a textbox is empty for validation purposes, rather than using if x = "". Instead I was wondering if there was a better way to do this. Currently I have:
If txtDob Is Nothing Or txtFirst Is Nothing Or txtGender Is Nothing Or txtLast Is Nothing Or txtPostcode Is Nothing Or txtStreetName Is Nothing Or txtStreetNo.Text Is Nothing Then
MessageBox.Show("One or more fields have not been completed")
Return
End If
However, this doesnt seem to work, can someone show me the correct method or another way to do this please?
For example:
If String.IsNullOrEmpty(txtDob.Text) Then
' "Contains Empty value or Null Value"
End If
You could use this:
Dim emptyTextBoxes =
From txt In Me.Controls.OfType(Of TextBox)()
Where txt.Text.Length = 0
Select txt.Name
If emptyTextBoxes.Any Then
MessageBox.Show(String.Format("Please fill following textboxes: {0}",
String.Join(",", emptyTextBoxes)))
End If
Code from Tim Schmelter's answer on Check for empty TextBox controls in VB.NET
You need to check the Text property.
If txtDob.Text = string.Empty Then
With your Code you're checking if the Object of your TextBox is nothing, not the content. As long as the TextBox exists, your condition would return false.

Select Textbox using For Loop

Sorry this is weird and silly question but I need a solution of this...
I have 30 textboxes named as Txt1,Txt2,Txt3,...,Txt30
I have to fill textbox as Txt1.text = 0 on button click..
Is there any way like On Button Click
Dim i as Integer
For i = 1 to 30 '----- Possible in string but don't know if possible in textboxes
Txt(i).text = 0
Next
Or I have to write all 30 lines like
Txt1.text = 0
...
Txt30.text = 0
I don't know how this question asked, maybe Question improper.
Thanx in Advance...
If the textboxes are all contained in Controls collection of the Form, then it is really easy to loop over them
For Each(t in Me.Controls.OfType(Of TextBox)())
t.Text = "0"
Of course the advantage of this approach is that you don't have to worry if you add other textboxes to your form. They will be found using the foreach loop without having a fixed top limit.
And, if not all of your textboxes should be included in the loop, then you can simply use the Tag property of the textboxes that you want to use. For example, if you set the Tag property to the string "Y", then you can change the foreach loop to find only the controls with the matching Tag property
For Each t in Me.Controls.OfType(Of TextBox)() _
.Where(Function(x) x.Tag = "Y")
t.Text = "0"
The two solutions above works well if all your textboxes are contained in the same container (Form, GroupBox or Panel), instead, if these textboxes are dispersed in different containers (some in a groupbox, others in a panel etc) then you can build a List(Of TextBox) variable filling it with the textboxes instances and use it when the need arise
Dim myTexts = New List(Of TextBox)() From { Txt1, Txt2, Txt3, ....}
And loop over this variable
You can do as Steve said, but if you have other textboxes that you don't want to edit then there is another way:
For i = 1 to 30
dim found = Me.Controls.Find("Txt" & i, True) '<- the True argument is for recursive search
If Not IsNothing(found) AndAlso found.Length > 0 Then
found(0).Text = "0"
End If
Next
If your textboxes are all in the same control, for example GroupBox, you can do GroupBox.Controls.Find saving CPU resources