add on button click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tallapureddy
    New Member
    • Nov 2009
    • 1

    #1

    add on button click

    Hello Everybody.
    I am doing a windows appln in vb.net.
    I have a button in my form. When i click the button, textboxes are added dynamically to the form. For 1st click 4 textboxes are added. When clicked again, again the textboxes are added once again...Now i need to save the data from the textboxes to database. Pls anyone help me with that. Here is my coding.

    Code:
    Dim value1 As String = ""
    Dim value2 As String = ""
    Dim value3 As String = ""
    Dim value4 As String = ""
    Dim i As Integer
    For i = 0 To FlowLayoutPanel1.Controls.Count - 1
    Select Case FlowLayoutPanel1.Controls(i).Name
    
    Case "TextBox1"
    value1 = FlowLayoutPanel1.Controls(i).Text
    
    Case "TextBox2"
    value2 = FlowLayoutPanel1.Controls(i).Text
    
    Case "TextBox3"
    value3 = FlowLayoutPanel1.Controls(i).Text
    
    Case "TextBox4"
    value4 = FlowLayoutPanel1.Controls(i).Text
    End Select
    Next i
    Dim strsql As String
    strsql = "insert into stud values ('" & value1 & "','" & value2 & "','" & value3 & "','" & value4 & "')"
    Only the text in the first four textboxes is getting aded to the database.What about the others
    Last edited by tlhintoq; Nov 9 '09, 10:41 AM. Reason: [CODE] ...your code here...[/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    .
    Only the text in the first four textboxes is getting aded to the database.What about the others
    What others? Your code specifically is getting the text from TextBox1 - TextBox4 and putting it into value1 - value4. What else were you expecting it to do but what you have told it to do?

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Hmm I would approach this differently.

        I would create class level variable that would contain a List Of TextBoxes. When the button was clicked I would create 4 new text boxes and add them to the FlowLayoutPanel 1 and to the List Of TextBoxes.

        Now, when I need to access the TextBoxes I would just loop through the TextBoxes in the List Of TextBoxes.....

        So instead of having:
        Code:
        Dim value1 As String = ""
        Dim value2 As String = ""
        Dim value3 As String = ""
        Dim value4 As String = ""
        Dim i As Integer
        For i = 0 To FlowLayoutPanel1.Controls.Count - 1
        Select Case FlowLayoutPanel1.Controls(i).Name
         
        Case "TextBox1"
        value1 = FlowLayoutPanel1.Controls(i).Text
         
        Case "TextBox2"
        value2 = FlowLayoutPanel1.Controls(i).Text
         
        Case "TextBox3"
        value3 = FlowLayoutPanel1.Controls(i).Text
         
        Case "TextBox4"
        value4 = FlowLayoutPanel1.Controls(i).Text
        End Select
        Next i
        Dim strsql As String
        strsql = "insert into stud values ('" & value1 & "','" & value2 & "','" & value3 & "','" & value4 & "')"
        I would have:
        Code:
        'The following will add 4 more TextBoxes to the FlowLayoutPanel1 control 
        'and to the list of TextBoxes that holds a reference to all of the dynamically created TextBoxes.
        Dim i As Integer
        For i = 0 To 3
          Dim aTextBox As New TextBox()
          aTextBox.Name = "TextBox" + myListOfTextBoxes.Count.ToString
          myListOfTextBoxes.Add(aTextBox)
          FlowLayoutPanel1.Controls.Add(aTextBox)
        Next i
        Please note that myListOfTextBox es is declared as a class level member and instantiated in the constructor for the class:
        Code:
        Private myListOfTextBoxes As List(Of TextBoxes)
        
        Public Sub New()
          ' This call is required by the Windows Form Designer.
          InitializeComponent()
          myListOfTextBoxes = New List(Of TextBoxes)
        End Sub
        Later, when I need to access the dynamic TextBoxes I can just loop through the List Of TextBoxes:
        Code:
        Dim textBoxValues As New StringBuilder
        For Each dynamicallyCreatedTextBox As TextBox In myListOfTextBoxes 
          textBoxValues.Append(dynamicallyCreatedTextBox.Text)
          textBoxValues.Append(", ")
        Next
        -Frinny

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Same logic as I would employ. I just wasn't going to give the OP the complete code for his/her problem until (s)he had taken a day to think about it, visualize what was happening, and tried to figure out a better scheme on there own.

          Though there is benefit to giving a great example (which all of yours are), I think there is a lot to be said for someone having to do at least a little work, research and trial and error. We learn so much more by failure and experience than by being handed the answer. Don't you think?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I completely agree with you Tlhintoq...

            But, the OP obviously has attempted the problem....and I have this squeamish aversion to nasty looking code and couldn't help myself.

            Maybe the OP will learn better from example this time round :)

            Besides, I didn't give them the "whole" solution...I don't have a clue what they're trying to do with the database stuff. And I didn't throw everything into the methods where they belong either.

            Hehe, I didn't even answer their question out-right either....they' ll have to look at my proposed solution and think about how to do their database thing.

            Their question doesn't even make sense.
            The database table that they are updating/inserting/managing has a defined table structure.....

            I do not understand how the table is going to be updated with the information the user provides. I mean the user can potentially add an infinite number TextBoxes to fill out....but the table has a finite number of fields to update.

            ????????? I don't get it ?????????

            -Frinny

            Comment

            Working...