none
Linked Textboxes RRS feed

  • Question

  • Hello,

    I currently have a TabControl with a couple pages. On each TabPage are several textboxes. On TabPage1, for example, I would have some textboxes like Name, Number, and a few others. TabPage2 might have Name, Number, and some other textboxes that are not shared with TabPage. Ideally, any time a textbox that is "shared" between the TabPages is edited, I'd like the edits to occur in all instances of the shared textbox.

    For example, if I typed "John Doe" in the Name textbox in TabPage1, the Name textbox in TabPage2 would be automatically filled out. I know I can create some subs that will update the values of the textboxes every time another one is updated, but this method means I'll a sub for every textbox I want to be automatically filled out from other textboxes. Is there a more efficient or less cumbersome way to do this? I don't think it's possible to have the exact same control with the same name in two different TabPages, let me know if that's not true.

    Tuesday, June 25, 2019 12:56 PM

Answers

  • Hello,

    Personally I would name each TextBox like FirstNameTextBoxTab1 etc then we can link/data bind them together as shown below.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            FirstNameTextBoxTab1.DataBindings.
                Add("Text", FirstNameTextBoxTab2, "Text")
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Matt_946 Tuesday, June 25, 2019 2:16 PM
    Tuesday, June 25, 2019 1:25 PM
    Moderator

All replies

  • Hello,

    Personally I would name each TextBox like FirstNameTextBoxTab1 etc then we can link/data bind them together as shown below.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            FirstNameTextBoxTab1.DataBindings.
                Add("Text", FirstNameTextBoxTab2, "Text")
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Matt_946 Tuesday, June 25, 2019 2:16 PM
    Tuesday, June 25, 2019 1:25 PM
    Moderator
  • Hi

    Here is some code that illustrates that multiple controls can be handled in the same sub. This example has a TabControl1 with TabPages 1 through 3. Each TabPage has a pair of TextBoxes named Name1, Address1     Name2, Address2     Name3, Address3

    these names are used to distinguish which of all the textboxes fall into the same group, and can be made to hold the same text.

    This example sets up for 2 TextBoxes on each TabPage, but, with little effort, code can be added to handle any number of 'grouped' controls just by copy/paste the Handler Sub and renaming a couple of variables. Any number of TabPages can also be handled.This code is a stand alone example, just create the controls listed and copy/replace the code with this code to try. Any of the textboxes Text can be edited and will be reflected in the others of the group.

    ' Form1 with TabControl1 containing
    ' TabPages 1 - 3, each with 2 TextBoxes
    ' named as Name1, Name2, Name3 and 
    ' Address1, Address2, Address3
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Name_TextChanged(sender As Object, e As EventArgs) Handles Name2.TextChanged, Name1.TextChanged, Name3.TextChanged
    		Dim tb As TextBox = DirectCast(sender, TextBox)
    		Dim txt As String = tb.Text
    		For Each c As Control In TabControl1.Controls
    			If c.GetType() Is GetType(TabPage) Then
    				For Each cc As Control In c.Controls
    					If cc.GetType() Is GetType(TextBox) Then
    						If cc.Name.StartsWith("Name") Then
    							If Not cc Is tb Then
    								cc.Text = tb.Text
    							End If
    						End If
    					End If
    				Next
    			End If
    		Next
    	End Sub
    	Private Sub Address_TextChanged(sender As Object, e As EventArgs) Handles Address1.TextChanged, Address2.TextChanged, Address3.TextChanged
    		Dim tb As TextBox = DirectCast(sender, TextBox)
    		Dim txt As String = tb.Text
    		For Each c As Control In TabControl1.Controls
    			If c.GetType() Is GetType(TabPage) Then
    				For Each cc As Control In c.Controls
    					If cc.GetType() Is GetType(TextBox) Then
    						If cc.Name.StartsWith("Address") Then
    							If Not cc Is tb Then
    								cc.Text = tb.Text
    							End If
    						End If
    					End If
    				Next
    			End If
    		Next
    	End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Tuesday, June 25, 2019 2:03 PM
    Tuesday, June 25, 2019 1:50 PM