Results 1 to 9 of 9

Thread: help minimizing If Then statement

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    109

    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?

  2. #2
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: help minimizing If Then statement

    Try this:
    VB.net Code:
    1. txtComplete.Clear()
    2.  
    3. If Not txtName1.Text = Nothing Then
    4.    txtComplete.Text += "txtName1=" & txtName1.Text & " "
    5. End If
    6. If Not txtName2.Text = Nothing Then
    7.    txtComplete.Text += "txtName2=" & txtName2.Text & " "
    8. End If
    9. If Not txtName3.Text = Nothing Then
    10.    txtComplete.Text += "txtName3=" & txtName3.Text & " "
    11. End If
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  3. #3
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    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:
    1. Imports System.Text
    2. Public Class Form1
    3.     Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
    4.         Dim Output As New StringBuilder()
    5.  
    6.         If Not String.IsNullOrEmpty(Name1.Text) Then
    7.             Output = Output.Append("Name1 = " & Name1.Text & " ")
    8.         End If
    9.  
    10.         If Not String.IsNullOrEmpty(Name2.Text) Then
    11.             Output = Output.Append("Name2 = " & Name2.Text & " ")
    12.         End If
    13.  
    14.         If Not String.IsNullOrEmpty(Name3.Text) Then
    15.             Output = Output.Append("Name3 = " & Name3.Text & " ")
    16.         End If
    17.  
    18.         If Not String.IsNullOrEmpty(Name4.Text) Then
    19.             Output = Output.Append("Name4 = " & Name4.Text)
    20.         End If
    21.  
    22.         Complete.Text = Output.ToString
    23.         Output = Nothing
    24.     End Sub
    25. End Class

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: help minimizing If Then statement

    Quote Originally Posted by firstascent View Post
    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.

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  7. #7
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    109

    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.

  9. #9
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    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
  •  



Click Here to Expand Forum to Full Width