locked
TextBox RRS feed

  • Question

  • I have been using the designer to add textbox to my projects. I then have to hide them. What is the correct code to creating a textbox that I can use to pass and store data?
    Tuesday, November 11, 2014 8:14 AM

Answers

  • Hello,

    To set the Text property from a child form we can set the Owner when creating the child form then in the child assign text to the TextBox in the parent form TextBox.

    Both forms have a TextBox1

    Parent form

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2 With {.Owner = Me}
            Try
                f.ShowDialog()
            Finally
                f.Dispose()
            End Try
        End Sub
    End Class
    

    Child form

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            CType(Me.Owner, Form1).TextBox1.Text = Me.TextBox1.Text
        End Sub
    End Class

    We can use a Singleton pattern (if you know C# it's similar to a static class). In short the class is created once and used throughout the project. We could even place the code in a class project in the same solution and use it.

    Class to use

    Public Class Person
        Public Property Identifier As Integer
        Public Property FirstName As String
        Public Property LastName As String
        Public ReadOnly Property FullName As String
            Get
                Return FirstName & " " & LastName
            End Get
        End Property
        Public Sub New()
        End Sub
    End Class
    


    Singleton class

    Public Class Example1
        Private Shared _Instance As Example1
        ''' <summary>
        ''' Only executes first time
        ''' </summary>
        ''' <remarks></remarks>
        Protected Sub New()
            People = New List(Of Person)
        End Sub
        Public ReadOnly Property Count As Integer
            Get
                Return People.Count
            End Get
        End Property
        Public Sub Clear()
            People.Clear()
        End Sub
        Public Sub Add(ByVal sender As Person)
            Dim ExistTest = (From p In People Where p.Identifier = sender.Identifier).FirstOrDefault
            If ExistTest Is Nothing Then
                People.Add(sender)
            End If
        End Sub
        Public Shared Function Instance() As Example1
            If _Instance Is Nothing Then
                _Instance = New Example1
            End If
            Return _Instance
        End Function
        Public Property People As List(Of Person)
    End Class
    

    Form1 (parent)

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2
            Try
                f.ShowDialog()
            Finally
                f.Dispose()
            End Try
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If Example1.Instance.Count > 0 Then
                MessageBox.Show(Example1.Instance.People.First.FullName)
            Else
                MessageBox.Show("Need to show form2")
            End If
        End Sub
    End Class
    

    Form2 (child)

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Example1.Instance.People.Add(
                New Person With
                {
                    .Identifier = 1,
                    .FirstName = "Karen",
                    .LastName = "Payne"
                }
            )
        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.

    • Marked as answer by -tE Wednesday, November 12, 2014 9:15 AM
    Wednesday, November 12, 2014 1:24 AM

All replies

  • A textbox for what?

    Phone, Office, Windows Forms, a Web page, WPF, ..............................................................................


    Success
    Cor

    Tuesday, November 11, 2014 8:18 AM
  • I have been using the designer to add textbox to my projects. I then have to hide them. What is the correct code to creating a textbox that I can use to pass and store data?

    Don't use a textbox to pass and store data.  Use it to display some text or to obtain text input from the user.  Use variables to store and pass data.  You create a textbox using a constructor -

      Dim TB As New TextBox

    To associate a textbox with a form so it shows on the form add it to the form's control collection (or the control collection of whatever container you are putting it in).
    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls(v=vs.110).aspx


    • Edited by Acamar Tuesday, November 11, 2014 9:26 AM sp
    Tuesday, November 11, 2014 9:26 AM
  • Hello,

    As already mentioned, using a TextBox for passing and storing information is not a good idea. Instead consider using a List(Of T) i.e.

    Private MyList As New List(Of String)

    Or perhaps the list might store strong typed data for a class i.e.

    Public Class Perrson
        Public Property FirstName As String
        Public Property LastName As String
        Public Sub New()
        End Sub
    End Class

    Adding items

    Private PersonList As New List(Of Person)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PersonList.Add(New Person With {.FirstName = "Karen", .LastName = "Payne"})
    End Sub

    If you need to pass this to another class, module, form or function that is easily done. 

    Best to explain what you need to do and if you are set on creating a bunch of TextBox controls any ways we can still assist. For example, create a bunch of TextBox controls

    Public Class TextBoxCreate
        Public Property TextBoxes As TextBox()
        Public Property TextBoxBaseName As String
        Public Property TextBoxCount As Integer
        Public Property ParentControl As Control
    
        Public Sub New(
            ByVal ParentControl As Control,
            ByVal BaseName As String,
            ByVal Count As Integer)
    
            Me.ParentControl = ParentControl
            Me.TextBoxBaseName = BaseName
            Me.TextBoxCount = Count
    
        End Sub
        Public Sub CreateTextBoxes()
            Dim Base As Integer = 10
            TextBoxes = Enumerable.Range(0, TextBoxCount).Select(
                Function(Indexer)
    
                    Dim b As New TextBox With
                        {
                            .Name = String.Concat(TextBoxBaseName, Indexer),
                            .Text = Base.ToString,
                            .Width = 150,
                            .Location = New Point(25, Base),
                            .Parent = Me.ParentControl,
                            .Visible = False
                        }
    
                    Me.ParentControl.Controls.Add(b)
    
                    Base += 30
    
                    Return b
    
                End Function).ToArray
    
        End Sub
    End Class
    

    Simple usage

    Private tbs As New TextBoxCreate(Me, "txt", 20)
    The above to be modified to allow data to be passed in rather than number of TextBox controls to create and then iterate thru the list and create that many TextBox controls but as mentioned earlier it is better not to use TextBox controls this way, it's how some did work back in VB6 and there are much better methods today.


    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.

    Tuesday, November 11, 2014 7:59 PM
  • So if I have a function that is creating a value to a variable in a sub and I want to store that in a table,  where would I store the value as it is changing before I collet it to be added to the table?  I would have passed it to a textbox and then stored that value   by referring to the textbox not the variable. 
    Tuesday, November 11, 2014 8:21 PM
  • Hello,

    Like this

    Public Class Form1
        Private NameList As New List(Of String)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim FullName As String = GetName()
            If FullName IsNot Nothing Then
                NameList.Add(FullName)
            Else
                MessageBox.Show("Requires first and last name")
            End If
        End Sub
        ''' <summary>
        ''' This is hard coded for demoing
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function GetName() As String
            If Not String.IsNullOrWhiteSpace(txtFirstName.Text) AndAlso Not String.IsNullOrWhiteSpace(txtLastName.Text) Then
                Return String.Concat(txtFirstName.Text, " ", txtLastName.Text)
            Else
                Return Nothing
            End If
        End Function
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            For Each FullName In NameList
                '
                ' Do something with the item
                '
            Next
        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.

    Tuesday, November 11, 2014 8:32 PM
  • So if I have a function that is creating a value to a variable in a sub and I want to store that in a table,  where would I store the value as it is changing before I collet it to be added to the table?

    If you have a variable that needs to be referred to from several different pieces of code within a form, then declare that variable at the form level. It will then be available to any code that is part of that form. 

    Public Class Form4 Dim TableItem As String = "" Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'etc

    See: http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx

    Tuesday, November 11, 2014 8:47 PM
  • But,  what about multiple forms?  I am trying to pass from one form to another and used textbox to do so.  Do I declare a class on a form?  This effort didn't seem to work.

    I just found declaring it as a Public variable.  As for A Class should I be able to do this as well?

    • Edited by -tE Wednesday, November 12, 2014 1:07 AM
    Wednesday, November 12, 2014 1:04 AM
  • But,  what about multiple forms?  I am trying to pass from one form to another and used textbox to do so.

    If you need to access information between forms then you must use one of the available processes for passing that data back and forth.  There are some examples here:

    http://vbdotnetblog.wordpress.com/forms/

    Another option is to create a shared variable in a module. See:

    http://msdn.microsoft.com/en-us/library/7825002w(v=vs.90).aspx

    If you have been using form controls to pass data then you were probably relying on the default form instance (which is a shared instance of your form that is created automatically for you).  This approach is simple to start with but quickly becomes unmanageable and unreliable.  It only exists because it was the way it used to be done in previous versions of VB that did not properly support objects.

    • Edited by Acamar Wednesday, November 12, 2014 1:14 AM sp
    Wednesday, November 12, 2014 1:12 AM
  • Hello,

    To set the Text property from a child form we can set the Owner when creating the child form then in the child assign text to the TextBox in the parent form TextBox.

    Both forms have a TextBox1

    Parent form

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2 With {.Owner = Me}
            Try
                f.ShowDialog()
            Finally
                f.Dispose()
            End Try
        End Sub
    End Class
    

    Child form

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            CType(Me.Owner, Form1).TextBox1.Text = Me.TextBox1.Text
        End Sub
    End Class

    We can use a Singleton pattern (if you know C# it's similar to a static class). In short the class is created once and used throughout the project. We could even place the code in a class project in the same solution and use it.

    Class to use

    Public Class Person
        Public Property Identifier As Integer
        Public Property FirstName As String
        Public Property LastName As String
        Public ReadOnly Property FullName As String
            Get
                Return FirstName & " " & LastName
            End Get
        End Property
        Public Sub New()
        End Sub
    End Class
    


    Singleton class

    Public Class Example1
        Private Shared _Instance As Example1
        ''' <summary>
        ''' Only executes first time
        ''' </summary>
        ''' <remarks></remarks>
        Protected Sub New()
            People = New List(Of Person)
        End Sub
        Public ReadOnly Property Count As Integer
            Get
                Return People.Count
            End Get
        End Property
        Public Sub Clear()
            People.Clear()
        End Sub
        Public Sub Add(ByVal sender As Person)
            Dim ExistTest = (From p In People Where p.Identifier = sender.Identifier).FirstOrDefault
            If ExistTest Is Nothing Then
                People.Add(sender)
            End If
        End Sub
        Public Shared Function Instance() As Example1
            If _Instance Is Nothing Then
                _Instance = New Example1
            End If
            Return _Instance
        End Function
        Public Property People As List(Of Person)
    End Class
    

    Form1 (parent)

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2
            Try
                f.ShowDialog()
            Finally
                f.Dispose()
            End Try
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If Example1.Instance.Count > 0 Then
                MessageBox.Show(Example1.Instance.People.First.FullName)
            Else
                MessageBox.Show("Need to show form2")
            End If
        End Sub
    End Class
    

    Form2 (child)

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Example1.Instance.People.Add(
                New Person With
                {
                    .Identifier = 1,
                    .FirstName = "Karen",
                    .LastName = "Payne"
                }
            )
        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.

    • Marked as answer by -tE Wednesday, November 12, 2014 9:15 AM
    Wednesday, November 12, 2014 1:24 AM