|
-
Mar 8th, 2010, 02:27 PM
#1
Thread Starter
Lively Member
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?
-
Mar 8th, 2010, 03:02 PM
#2
Hyperactive Member
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
-
Mar 8th, 2010, 03:04 PM
#3
Hyperactive Member
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
-
Mar 8th, 2010, 03:05 PM
#4
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!!!
Last edited by Siddharth Rout; Mar 8th, 2010 at 03:08 PM.
Reason: Typo... Amended
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Mar 8th, 2010, 03:07 PM
#5
Re: help minimizing If Then statement
 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.
-
Mar 8th, 2010, 03:25 PM
#6
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
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Mar 8th, 2010, 03:59 PM
#7
Fanatic Member
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
Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP
Please Rate If I helped you. 
Please remember to mark threads as closed if your issue has been resolved.
Reserved Words in Access | Connection Strings
-
Mar 8th, 2010, 06:20 PM
#8
Thread Starter
Lively Member
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.
-
Mar 9th, 2010, 02:58 AM
#9
Addicted Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|