locked
get text from textbox added dynamically(winform) RRS feed

  • Question

  • hi 

    I have a form that contain a datagridview and a textbox as a filter  to show specfic data in the datagridview 

    Both controls datagridview and textbox are added dynamically 

    how can i get the text of the texbox to use in the select query to show the datagridview ?? is it possible?

    i must notice that i use another project (designer host of microsoft) to create forms with controls and insert it in a database.and in my current project i select the form and its controls from database and add it dynamically 

    there's some code :

     Dim CommandeControle As SqlCommand = ConnexionForm.CreateCommand()
    
            CommandeControle.CommandText = "SELECT idControle,name,type,text,sizeH,sizeL,locationX,locationY,tag FROM [Controle] WHERE idForm='" & idforme & "' "
       
            ConnexionForm.Open()
            Dim ReaderControle As SqlDataReader = CommandeControle.ExecuteReader()
            Do While ReaderControle.Read()
    
                Dim componentType As String
                'extract component from database 
                componentType = coupeChaine(CType(ReaderControle.GetValue(2), String))
                Select Case componentType
    
                    Case "Button"
    
                        Dim c As New Button
                        Me.Controls.Add(c)
                        c.Text = CType(ReaderControle.GetValue(3), String)
                        c.Name = CType(ReaderControle.GetValue(1), String)
                        c.Size = New Size(CType(ReaderControle.GetValue(4), Integer), CType(ReaderControle.GetValue(5), Integer))
                        c.Location = New Point(CType(ReaderControle.GetValue(6), Integer), CType(ReaderControle.GetValue(7), Integer))
    
                    Case "Label"
                        Dim c As New Label
                        Me.Controls.Add(c)
                        c.Text = CType(ReaderControle.GetValue(3), String)
                        c.Name = CType(ReaderControle.GetValue(1), String)
                        c.Size = New Size(CType(ReaderControle.GetValue(4), Integer), CType(ReaderControle.GetValue(5), Integer))
                        c.Location = New Point(CType(ReaderControle.GetValue(6), Integer), CType(ReaderControle.GetValue(7), Integer))
    
    
                    Case "TextBox"
                        Dim c As MyTextBox
                        Me.Controls.Add(c)
                        c.Text = CType(ReaderControle.GetValue(3), String)
                        c.Name = CType(ReaderControle.GetValue(1), String)
                        c.Size = New Size(CType(ReaderControle.GetValue(4), Integer), CType(ReaderControle.GetValue(5), Integer))
                        c.Location = New Point(CType(ReaderControle.GetValue(6), Integer), CType(ReaderControle.GetValue(7), Integer))
    
                   
                    Case "DataGridView"
                        Dim c As New DataGridView
                        Me.Controls.Add(c)
                        c.Text = CType(ReaderControle.GetValue(3), String)
                        c.Name = CType(ReaderControle.GetValue(1), String)
                        c.Size = New Size(CType(ReaderControle.GetValue(4), Integer), CType(ReaderControle.GetValue(5), Integer))
                        c.Location = New Point(CType(ReaderControle.GetValue(6), Integer), CType(ReaderControle.GetValue(7), Integer))
                        Dim query As String
                       
                                query = FirstVariable
                        
                        ShowData(query, c)
                    
                    
                End Select
    
    
            Loop
            ReaderControle.Close()
    
            ConnexionForm.Close()
    



    Best Regards...Please mark as answer if my post is helpful
    Monday, September 12, 2011 3:04 PM

Answers

  • You can try following code to get text from textbox added dynamically.

            Dim txtControlName As String = "MyTextBox1" 'Name of TextBox control
     
            'Dim txt() As TextBox = Me.Controls.Find(txtControlName, True)
            Dim txt As TextBox = Me.Controls.Item(txtControlName)
     
     
            Dim query As String = "SELECT * FROM TableName WHERE ColumnName='" & txt.Text & "'"
            'Code to retrieve the data for datagridview in datatable or datareader
            Dim dt As DataTable = GetData(query)
     
            Dim dg As DataGridView = Me.Controls.Item(datagridName)
            dg.DataSource = dt


    Gaurav Khanna | http://gauravkhanna.blog.co.in
    • Proposed as answer by Helen Zhou Tuesday, September 13, 2011 8:13 AM
    • Marked as answer by YosrJ Tuesday, September 13, 2011 10:26 AM
    Monday, September 12, 2011 7:05 PM

All replies

  • You can try following code to get text from textbox added dynamically.

            Dim txtControlName As String = "MyTextBox1" 'Name of TextBox control
     
            'Dim txt() As TextBox = Me.Controls.Find(txtControlName, True)
            Dim txt As TextBox = Me.Controls.Item(txtControlName)
     
     
            Dim query As String = "SELECT * FROM TableName WHERE ColumnName='" & txt.Text & "'"
            'Code to retrieve the data for datagridview in datatable or datareader
            Dim dt As DataTable = GetData(query)
     
            Dim dg As DataGridView = Me.Controls.Item(datagridName)
            dg.DataSource = dt


    Gaurav Khanna | http://gauravkhanna.blog.co.in
    • Proposed as answer by Helen Zhou Tuesday, September 13, 2011 8:13 AM
    • Marked as answer by YosrJ Tuesday, September 13, 2011 10:26 AM
    Monday, September 12, 2011 7:05 PM
  • thanks khanna

    your code gave me an idea to change my way of adding controls dynamically ill try it and tell you


    Best Regards...Please mark as answer if my post is helpful
    Tuesday, September 13, 2011 7:40 AM
  • Hi Yosrj,

    I see you already get the dynamically added textbox (c in your code) by the part of "Case "TextBox"", so you can make it as the  filter to show specfic data in the datagridview like:            

    using(SqlConnection myConnection = new SqlConnection(conn))                //connect your application to the DataBase
                {
                    SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable Where A = @a", conn);
                    cmd.Parameters.Add("@a", SqlDbType.Int).Value = c.Text;
                    da.SelectCommand = cmd;  //da is a SqlDataAdapter     
    DataSet ds = new DataSet(); da.Fill(ds); connection.Open(); dataGridView1.DataSource = ds.Tables[0]; //use YourControl to host the query result, that means bind the control to the query result, that control can be i.e DataGridView normally. }

     


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Helen Zhou Tuesday, September 13, 2011 8:29 AM
    Tuesday, September 13, 2011 8:28 AM
  • Hi Helen

    Thk for your reply 

    exactly my textbox is added already (the c control) (i got all my controls from a database ) and my datagridview is also added dynamically 

    so the line that your proposed dataGridView1.DataSource = ds.Tables[0] is concerning a datagridview that doed exist in the form but i added the datagridview dynamically

    like the textbox

     

       Case "DataGridView"
                        Dim c As New DataGridView
                        Me.Controls.Add(c)
                        c.Text = CType(ReaderControle.GetValue(3), String)
                        c.Name = CType(ReaderControle.GetValue(1), String)
                        c.Size = New Size(CType(ReaderControle.GetValue(4), Integer), CType(ReaderControle.GetValue(5), Integer))
                        c.Location = New Point(CType(ReaderControle.GetValue(6), Integer), CType(ReaderControle.GetValue(7), Integer))
                        Dim query As String
                       
                                query = FirstVariable
                        
                        ShowData(query, c)
    
    so how can i bind the textbox and the datagridview ? and if there's another idea ill be greatful

     



     


    Best Regards...Please mark as answer if my post is helpful
    • Edited by YosrJ Tuesday, September 13, 2011 8:36 AM
    Tuesday, September 13, 2011 8:34 AM
  • it worked by using the idea of Khanna

     

    in fact ,i declared as private the txt textbox and :

     

         Case "TextBox"
                        '  Dim t As New TextBox
                        Me.Controls.Add(txt)
                        txt.Text = CType(ReaderControle.GetValue(3), String)
                        txt.Name = CType(ReaderControle.GetValue(1), String)
                        txt.Size = New Size(CType(ReaderControle.GetValue(4), Integer), CType(ReaderControle.GetValue(5), Integer))
                        txt.Location = New Point(CType(ReaderControle.GetValue(6), Integer), CType(ReaderControle.GetValue(7), Integer))
                        txt = Me.Controls.Item(txt.Name)
    

    and use it in my query to show the datagridview 

    Thx Helen and khanna


    Best Regards...Please mark as answer if my post is helpful
    Tuesday, September 13, 2011 10:38 AM