none
Check if textbox is empty then combine or not

    Question

  • I have 3 textboxes that i have to combine to one outcome with / between the numbers  .And textbox 4 is the outcome.

    But when 1 or 2 are empty or has a space ,i want to see only the one that has a mumber.
    There are about 80 other textbox's on the form but i want to combine only these textbox's nr 1,2,3

    for example.

    Textbox1.text = 123
    
    Textbox2.text = 1235
    
    Textbox3.text = 123 
    
    Textbox 4 is the outcome = 123/1235/123
    
    That is not the problem , but how do is solve this.
    
    
    
    Textbox1.text = 123
    
    Textbox2.text =( a space or empty)
    
    Textbox3.text = 123 
    
    Textbox 4 is the outcome = 123/123
    
    Or
    
    Textbox1.text = ( a space or empty)
    
    Textbox2.text =12
    
    Textbox3.text = 123 
    
    Textbox 4 is the outcome = 12/123
    
    Or
    
    Textbox1.text = ( a space or empty)
    
    Textbox2.text =( a space or empty)
    
    Textbox3.text = 123 
    
    Textbox 4 is the outcome = 123
    
    
    

    Thursday, December 07, 2017 9:22 AM

Answers

  • The code I used is valid after VS2010 so it will not work. Try the following which I'm not setup for VS2010, only VS2013, VS2015, VS2017. I built the following with Framework 2 in VS2015.

    Dim tbs As TextBox() = {TextBox1, TextBox2}
    Dim tbUse As New List(Of String)
    
    For Each tb As TextBox In tbs
        If Integer.TryParse(tb.Text, Nothing) Then
            tbUse.Add(tb.Text)
        End If
    Next
    
    TextBox3.Text = String.Join("/", tbUse.ToArray)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by KeesBlunder Thursday, December 07, 2017 7:25 PM
    Thursday, December 07, 2017 11:34 AM
    Moderator

All replies

  • Perhaps this will work for you.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim tbs As New List(Of TextBox) From {TextBox1, TextBox2}
    
            TextBox3.Text = String.Join("/",
                tbs.Where(Function(tb) Integer.TryParse(tb.Text, Nothing)) _
                .Select(Function(tb) tb.Text))
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Thursday, December 07, 2017 10:04 AM
    Moderator
  • Try such approach too:

    TextBox4.Text = String.Join("/", {TextBox1, TextBox2, TextBox3}.Select(Function(tb) tb.Text).Where(Function(t) Not String.IsNullOrWhiteSpace(t)))

    Thursday, December 07, 2017 10:19 AM
  • Hello Kareninstructor,

    Thanks for the code , but i get an error when i put some numbers in the textboxes

    It is in Dutch so i used google translate .

    Kan een object van het type WhereSelectListIterator`2[System.Windows.Forms.TextBox,System.String] niet converteren naar het type System.String[].
    
    
    
    Can not convert an object of the type WhereSelectListIterator`2 [System.Windows.Forms.TextBox, System.String] to the type System.String [].
    I am using version 2010 


    • Edited by KeesBlunder Thursday, December 07, 2017 10:57 AM
    Thursday, December 07, 2017 10:56 AM
  • The code I used is valid after VS2010 so it will not work. Try the following which I'm not setup for VS2010, only VS2013, VS2015, VS2017. I built the following with Framework 2 in VS2015.

    Dim tbs As TextBox() = {TextBox1, TextBox2}
    Dim tbUse As New List(Of String)
    
    For Each tb As TextBox In tbs
        If Integer.TryParse(tb.Text, Nothing) Then
            tbUse.Add(tb.Text)
        End If
    Next
    
    TextBox3.Text = String.Join("/", tbUse.ToArray)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by KeesBlunder Thursday, December 07, 2017 7:25 PM
    Thursday, December 07, 2017 11:34 AM
    Moderator
  • Hi

    Here is another way:

        

    Imports System.Text

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim sb As New StringBuilder Dim a1 As String = TextBox1.Text Dim a2 As String = TextBox2.Text Dim a3 As String = TextBox3.Text If Not a1 = Nothing Then sb.Append(a1) If Not a2 = Nothing Then sb.Append("/" & a2) If Not a3 = Nothing Then sb.Append("/" & a3) TextBox4.Text = sb.ToString End Sub



    Regards Les, Livingston, Scotland

    Thursday, December 07, 2017 11:37 AM
  • Thanks , 

    It works great.

    Thursday, December 07, 2017 7:25 PM