locked
form1 textboxes data use on form 2 RRS feed

  • Question

  • Hi guyz

    i have some problem i m using 2 forms form1 and form2 and on form1 have 50 textboxes, requirement is save 50 all textboxes

    on form 2 have combobox if change the value then retrieve data on particular textboxes from form1

    data in any other file or may be temp save on anywhere .. i m using these values in form 2 plz help us how to solve this problem..


    • Edited by Albertostin Monday, November 11, 2013 10:05 AM addition
    Monday, November 11, 2013 9:49 AM

Answers

  • Hi,

    For your requirement, I saved the data from Form1 to a xml file:

    Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
            Dim settings As New XmlWriterSettings()
            settings.Indent = True
    
            Dim XmlWrt As XmlWriter = XmlWriter.Create("MyXML.xml", settings)
            With XmlWrt
                .WriteStartDocument()
                .WriteComment("XML Database.")
                .WriteStartElement("Data")
    
                For Each txb As TextBox In GroupBox1.Controls
                    ' The nodes.
                    .WriteStartElement(txb.Name)
                    .WriteString(txb.Text)
                    .WriteEndElement()
                Next
    
                .WriteEndDocument()
                .Close()
            End With
        End Sub

    Of course, we should add event handler first(textboxs are all in a groupbox):

    Public Sub New()
            MyBase.New()
            InitializeComponent()
    
            For Each txb As TextBox In GroupBox1.Controls
                AddHandler txb.TextChanged, AddressOf TextBox_TextChanged
            Next
        End Sub

    In the Form2 combobox, when the selectedindex changed, just read the xml file to get specific textbox data:

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            If (IO.File.Exists("MyXML.xml")) Then
                Dim document As XmlReader = New XmlTextReader("MyXML.xml")
                While (document.Read())
                    Dim type = document.NodeType
                    If (type = XmlNodeType.Element) Then
                        If (document.Name = "TextBox" + ComboBox1.SelectedItem.ToString) Then
                            MsgBox(document.ReadInnerXml.ToString())
                        End If
                    End If
                End While
            Else
                MessageBox.Show("The filename you selected was not found.")
            End If
        End Sub

    Screenshot:


    Download sample:
    http://sdrv.ms/19YWQSK


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 12, 2013 3:37 AM
  • Hi,

    I will tell you what I'm trying to accomplish:

    Firstly, in Form1, their are five textbox controls in a GroupBox, we you input text into them, their data will be stored into a XML file.

    Secondly, click the button "Open Form2", the Form2 window will pop up, there is a ComboBox in it, you can choose 1-5 items to get the data (corresponding to TextBox1-5)

    For example, If I select the third item, a messagebox with the text of TextBox3 will pop up, this gif may be more clearly:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 13, 2013 12:09 PM

All replies

  • Hi,

    For your requirement, I saved the data from Form1 to a xml file:

    Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
            Dim settings As New XmlWriterSettings()
            settings.Indent = True
    
            Dim XmlWrt As XmlWriter = XmlWriter.Create("MyXML.xml", settings)
            With XmlWrt
                .WriteStartDocument()
                .WriteComment("XML Database.")
                .WriteStartElement("Data")
    
                For Each txb As TextBox In GroupBox1.Controls
                    ' The nodes.
                    .WriteStartElement(txb.Name)
                    .WriteString(txb.Text)
                    .WriteEndElement()
                Next
    
                .WriteEndDocument()
                .Close()
            End With
        End Sub

    Of course, we should add event handler first(textboxs are all in a groupbox):

    Public Sub New()
            MyBase.New()
            InitializeComponent()
    
            For Each txb As TextBox In GroupBox1.Controls
                AddHandler txb.TextChanged, AddressOf TextBox_TextChanged
            Next
        End Sub

    In the Form2 combobox, when the selectedindex changed, just read the xml file to get specific textbox data:

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            If (IO.File.Exists("MyXML.xml")) Then
                Dim document As XmlReader = New XmlTextReader("MyXML.xml")
                While (document.Read())
                    Dim type = document.NodeType
                    If (type = XmlNodeType.Element) Then
                        If (document.Name = "TextBox" + ComboBox1.SelectedItem.ToString) Then
                            MsgBox(document.ReadInnerXml.ToString())
                        End If
                    End If
                End While
            Else
                MessageBox.Show("The filename you selected was not found.")
            End If
        End Sub

    Screenshot:


    Download sample:
    http://sdrv.ms/19YWQSK


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 12, 2013 3:37 AM
  • I would probably try to do something like make the textboxes on Form1 an array, then line up the .SelectedIndex of the combobox on Form2 with the Index of the array on Form1...should make picking the proper box pretty simple...

    Form1.Textboxes(Form2.Combobox.SelectedIndex).Text or something of that nature...


    EDIT: (Not that Mr. Chen's example above wouldn't work, just seems to me to be a lot of trouble for what you are trying to accomplish...but I may be missing something...I often do! :))
    • Edited by HarleyHetz Tuesday, November 12, 2013 3:26 PM
    Tuesday, November 12, 2013 3:24 PM
  • Hi,

    I will tell you what I'm trying to accomplish:

    Firstly, in Form1, their are five textbox controls in a GroupBox, we you input text into them, their data will be stored into a XML file.

    Secondly, click the button "Open Form2", the Form2 window will pop up, there is a ComboBox in it, you can choose 1-5 items to get the data (corresponding to TextBox1-5)

    For example, If I select the third item, a messagebox with the text of TextBox3 will pop up, this gif may be more clearly:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 13, 2013 12:09 PM