0

I have 4 textboxes, and I am trying to validate that at least one of the textboxes is filled before they hit search to check their search criteria.

My Problem: My code is working, but when I want to continue with one filled field, it still shows the messageBox.

If txtMember.Text = "" Then
            MessageBox.Show("Please enter a value!")
        ElseIf txtReference.Text = "" Then
            MessageBox.Show("Please enter a value!")
        ElseIf txtName.Text = "" Then
            MessageBox.Show("Please enter a value!")
        ElseIf txtCode.TextLength = "" Then
            MessageBox.Show("Please enter a value!")
        Else
            SearchClick()
        End If

How do I fix this? Your help would be much appreciated.

Ridge
  • 19
  • 4

2 Answers2

2

The All and Any extension methods are useful in such cases, e.g.

If {TextBox1, TextBox2, TextBox3}.Any(Function(tb) tb.TextLength > 0) Then
    'At least one TextBox has some text in it.
jmcilhinney
  • 40,280
  • 5
  • 23
  • 39
  • I think this creates more overhead and not easily readable than just using an if/elseif. First create an array... then create the enumerable.., then search the elements... I would stick to the if-elseif – Kevbo Apr 06 '18 at 20:23
  • What does "create the enumerable" mean? If you're talking about the `Enumerable` class then an instance is not created because extension methods are static. If you mean the object that is enumerated then the array IS that object, i.e. a `TextBox` array implements `IEnumerable(Of TextBox)`. I wouldn't necessarily say that this is superior in this specific case but it's good to know about such options. As far as readability is concerned, I think that that depends on your familiarity with LINQ. I tend to find this sort of thing more readable. There is an overhead but it's miniscule. – jmcilhinney Apr 07 '18 at 00:33
  • "Any" is an extension method for IEnumerable. yes its static but you still have to create all the internals for it to work. As of 3.5 the Array class implements IEnumerable. Thats a lot of internal setup just to check something. If this was a large list of items then i would agree but this is only a few textboxes so i think it overkill. – Kevbo Apr 09 '18 at 15:43
0

Is something like below what you want? So if every text box is blank you want a message box to appear otherwise if one of the text boxes is filled in then do SearchClick How to use If Else. Learning to step through the code, might have helped you realise what was happening in your code.

If txtMember.Text = "" AndAlso txtReference.Text = "" AndAlso txtName.Text = "" AndAlso  txtCode.TextLength = "" Then
    MessageBox.Show("Please enter a value!")
Else
    SearchClick()
End If
Cal-cium
  • 604
  • 10
  • 18
  • You might find this interesting: [What is the difference between And and AndAlso in VB.NET?](https://stackoverflow.com/q/302047/1115360) – Andrew Morton Apr 06 '18 at 07:42
  • @AndrewMorton I know the difference, I kind of wrote this in a hurry but it would be useful for OP to know certainly. Updated answer. – Cal-cium Apr 06 '18 at 07:46
  • I see you changed to "AndAlso". "And" did the trick for me as well. Is there a difference to these two, or are they the same? – Ridge Apr 06 '18 at 08:06
  • @Ridge if you follow the link Andrew Morton, it will explain it better in more detail. But in short, `AndAlso` is a short-circuit method. `And` will check all the statements whereas `AndAlso` will stop once once it knows one of the conditions is false. So if `txtRefence` isn't empty for example it will stop checking the rest because it knows the condition is false so it saves time when doing a lot of checks. – Cal-cium Apr 06 '18 at 08:11
  • This fails if a space is entered... need to check this. – zaggler Apr 06 '18 at 11:53
  • `txtMember.Text.Trim() = ""` would work to remove spaces – Cal-cium Apr 06 '18 at 13:07
  • 1
    use string.empty instead of "". this causes the compiler to create an empy string just to do the compare whereas string.empty is a constant and will save some clock cycles. – Kevbo Apr 06 '18 at 20:18
  • That would make sense as well. Thanks, guys. – Ridge Apr 10 '18 at 15:14