Reputation: 115
I just want to write the text in these textboxes to a notepad.
I have 4 Textboxes
1) Textbox1.Text
2) Textbox2.Text
3) Textbox3.Text
4) Textbox4.Text
I have compulsary data in Textbox1.Text and Textbox4.Text all the time
And I have 4 Options. like
Option 1: If Textbox2 and Textbox3, both are not empty
Option 2: If Textbox2 is empty, but Textbox3 is not empty
Option 3: If Textbox2 is not empty, but Textbox3 is empty
Option 4: If both Textbox2 and Textbox3 are empty
My Code is
Dim Tags As String
If (Not String.IsNullOrWhiteSpace(Textbox2.Text)) Then
If (Not String.IsNullOrWhiteSpace(Textbox3.Text)) Then
Tags = String.Format("{0}: {1}, {2}, {3}{4}{4}", Textbox1.Text, Textbox2.Text, Textbox4.Text, Textbox3.Text, Environment.NewLine)
objWriter2.WriteLine(Tags)
ElseIf (String.IsNullOrWhiteSpace(Textbox2.Text)) Then
If (Not String.IsNullOrWhiteSpace(Textbox3.Text)) Then
Tags = String.Format("{0}, {2}, {3}{4}{4}", Textbox1.Text, Textbox2.Text, Textbox4.Text, Textbox3.Text, Environment.NewLine)
objWriter2.WriteLine(Tags)
End If
ElseIf (Not String.IsNullOrWhiteSpace(Textbox2.Text)) Then
If (String.IsNullOrWhiteSpace(Textbox3.Text)) Then
Tags = String.Format("{0}: {1}, {2}{4}{4}", Textbox1, Textbox2.Text, Textbox4.Text, Textbox3.Text, Environment.NewLine)
objWriter2.WriteLine(Tags)
End If
Else
objWriter2.WriteLine(Textbox1.Text + ", " + Textbox4.Text)
End If
End If
As you can see there's some difference in my code where Tags = String.Format()
, when i choose a different option.
Upvotes: 0
Views: 333
Reputation: 1
Actually there is something easier you can do for this with much less code:
if textbox1.text.trim.length Or textbox2.text.trim.length Or textbox3.text.trim.length Or textbox4.text.trim.length < 1 Then
MessageBox.Show("Textbox1 and Textbox4 must contain some data")
Upvotes: 0
Reputation: 112
I would try something like this:
'set variables equal to textbox values
Dim strText1 As String = TextBox1.Text
Dim strText2 As String = TextBox2.Text
Dim strText3 As String = TextBox3.Text
Dim strText4 As String = TextBox4.Text
'better option than If...Then because it doesn't go through all possibilities,
'only goes until it finds one that is valid for use
Select Case strText2
Case Is <> Nothing And strText3 <> Nothing
'option 1
Case is = Nothing And strText3 <> Nothing
'option 2
Case Is <> Nothing And strText3 = Nothing
'option 3
Case Else
'option 4
End Select
Upvotes: 0
Reputation: 27322
An idea is to load a boolean array with a True/False depending on whether each textbox has a value or not, then use these in your logic:
Dim boxData As Boolean() = {TextBox1.Text.Length > 0, TextBox2.Text.Length > 0, TextBox3.Text.Length > 0, TextBox4.Text.Length > 0}
If Not boxData(0) OrElse Not boxData(3) Then
Throw new Exception("Textbox1 and Textbox4 must contain some data")
ElseIf boxData(1) AndAlso boxData(2) Then
'option 1
ElseIf boxData(2) Then
'option 2
ElseIf boxData(1) Then
'option 3
Else
'option 4
End If
Note that the array is zero based so TextBox1
is boxData(0)
, etc.
Upvotes: 1