help minimizing If Then statement
I have 4 textboxes that a user can input date into, a save button, and another textbox that gets filled automatically depending on what is entered into the 4 text boxes.
Lets call the 4 text boxes: Name1, Name2, Name3 and Name4
we'll call the last text box Complete.
When you press the save button I want the values to be displayed in the Complete text box like this: Name1="text" Name2="text" Name3="text" Name4="text", BUT, only if they actually have a value.
So if they all have some text in them except Name2 then the Complete box would read: Name1="text" Name3="text" Name4="text" - so Name2 wouldn't show up at all.
Is there a simple way to put this in an If Then statement without scripting out every possible outcome?
Re: help minimizing If Then statement
Try this:
VB.net Code:
txtComplete.Clear()
If Not txtName1.Text = Nothing Then
txtComplete.Text += "txtName1=" & txtName1.Text & " "
End If
If Not txtName2.Text = Nothing Then
txtComplete.Text += "txtName2=" & txtName2.Text & " "
End If
If Not txtName3.Text = Nothing Then
txtComplete.Text += "txtName3=" & txtName3.Text & " "
End If
Re: help minimizing If Then statement
I am sure there are more clever ways to do this that would scale better for more textboxes but here is my version. It should work for the requirements you posted.
vb Code:
Imports System.Text
Public Class Form1
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
Dim Output As New StringBuilder()
If Not String.IsNullOrEmpty(Name1.Text) Then
Output = Output.Append("Name1 = " & Name1.Text & " ")
End If
If Not String.IsNullOrEmpty(Name2.Text) Then
Output = Output.Append("Name2 = " & Name2.Text & " ")
End If
If Not String.IsNullOrEmpty(Name3.Text) Then
Output = Output.Append("Name3 = " & Name3.Text & " ")
End If
If Not String.IsNullOrEmpty(Name4.Text) Then
Output = Output.Append("Name4 = " & Name4.Text)
End If
Complete.Text = Output.ToString
Output = Nothing
End Sub
End Class
Re: help minimizing If Then statement
Code:
If Not String.IsNullOrEmpty(Name1.Text) then strTmp = _
"Name1=" & """" & Name1.Text & """" & " "
If Not String.IsNullOrEmpty(Name2Text) then strTmp = _
strTemp & "Name2=" & """" & Name2.Text & """" & " "
If Not String.IsNullOrEmpty(Name3.Text) then strTmp = _
strTemp & "Name3=" & """" & Name3.Text & """" & " "
If Not String.IsNullOrEmpty(Name4.Text) then strTmp = _
strTemp & "Name4=" & """" & Name4.Text & """"
Complete.Text = strTemp
Edit: Oh I forgot to refresh the page!!!
Re: help minimizing If Then statement
Quote:
Originally Posted by
firstascent
Is there a simple way to put this in an If Then statement without scripting out every possible outcome?
Simple as defined by "absence of complexity", yes - but as you can tell by the existing examples, all of the textboxes will need to be accounted for.
Re: help minimizing If Then statement
Could be done in a For loop looking at all the controls on the page.
Get only the textbox controls and do the containation (or stringbuilder)
If more then just the Name series of TextBoxControls place Name1 .... into the Tag Property and then just do the work if the Tag Starts with Name
Re: help minimizing If Then statement
Here is my version:
Code:
'Provide list of texbox names to check
Dim textboxes() As String = {"Name1", "Name2", "Name3", "Name4"}
'Create a control array
Dim ctrl() As Control
'Loop through the strings
For Each s As String In textboxes
'Check to see if the control exists
ctrl = Me.Controls.Find(s, True)
'If control exists and the length of its text is > 0
'then add its text to the complete box.
If ctrl IsNot Nothing _
AndAlso ctrl.Length > 0 _
AndAlso CType(ctrl(0), TextBox).Text.Trim.Length > 0 Then
Complete.Text += s & "=" & CType(ctrl(0), TextBox).Text.Trim
End If
Next
Let me know if you have any questions!
D
Re: help minimizing If Then statement
thanks for all the examples, I will play around with those and see what will best fit my needs.
Im sure at least one (if not all) will work as I need it to.
Re: help minimizing If Then statement
How about this?
Code:
Complete.Text = ""
Dim txt() As TextBox = {"Name1", "Name2", "Name3", "Name4"}
For i As Short = 0 To txt.Length - 1
txt(i).Text = txt(i).Text.Trim
If txt(i).Text <> "" Then
Complete.Text = Complete.Text & txt(i).ID & "=" & txt(i).Text
If i <> txt.Length - 1 Then Complete.Text = Complete.Text & " "
End If
Next i