user1732364
user1732364

Reputation: 985

How to retrieve value of found control on a panel for a winform?

I'll try to keep this short and sweet.

I am making a UI to run sql queries, for each parameter used for the query, it generates a label and a textbox per parameter and places the controls onto a panel. I have this part working as it should but now i need to loop through each control and pull the value from the textbox for each parameter. So if a query has 2 parameters, it generates 2 labels and 2 textbox controls onto the panel. I then loop through the controls on the panel and pull the textbox.text from each. The problem is i can not find how to get the text from the textbox controls to set the value of each parameter to run the query.

        Dim CurrentParameters As New List(Of ParameterDT)
    CurrentParameters = BCWorkerParam.GetData(cboQueries.SelectedValue)

    For Each ctl In pnlParameters.Controls
        If TypeOf ctl Is TextBox Then
            If CurrentParameters.Count > 0 Then

                CurrentParameters.Item(0).DataValue = ""

            End If
        End If
    Next

The idea here is this, i have my list of parameters for the query in CurrentParameters. I then loop through the controls on the panel and for each textbox found, i set the parameters value to the textbox.text stated by this line CurrentParameters.Item(0).DataValue = "" where "" needs to be the current found textbox control text.

If there is another way of doing this, feel free to shoot me the ideas. My thinking that this will work is because even though im using indexes to match the control to the parameter, it creates the control BASED off each parameter so they should be in sync.


Alright i'm giving the suggestion a go, the following code is what generates the controls onto the panel. I added the .Tag pieces and the rest is doing the same as above.

'Generate query parameters
        Dim CurrentTopPosition As Integer = 10
        For Each param In Parameters

            pnlParameters.AutoSize = True
            Dim lblParam As New Label

            lblParam.Text = param.DisplayName
            lblParam.TextAlign = ContentAlignment.MiddleRight
            lblParam.Top = CurrentTopPosition
            lblParam.Left = 5
            lblParam.Width = 100
            pnlParameters.Controls.Add(lblParam)

            Dim ctlParam As New Object
            Select Case param.DataType
                Case "String"
                    ctlParam = New TextBox
                    ctlParam.tag = param
                Case "DateTime"
                    ctlParam = New DateTimePicker
                    ctlParam.tag = param
                Case "Integer"
                    ctlParam = New TextBox
                    ctlParam.tag = param
                Case "Double"
                    ctlParam = New TextBox
                    ctlParam.tag = param
            End Select
            ctlParam.Top = CurrentTopPosition
            ctlParam.Left = 110
            ctlParam.Width = 150
            ctlParam.Tag = param
            pnlParameters.Controls.Add(ctlParam)
            CurrentTopPosition += 30
        Next

Now once this generates those controls, i'll loop through them and do this code for the execution of the query.

        Dim CurrentParameters As New List(Of ParameterDT)
    CurrentParameters = BCWorkerParam.GetData(cboQueries.SelectedValue)
    Dim index As Integer = 0
    For Each ctl In pnlParameters.Controls
        If TypeOf ctl Is TextBox Then
            DirectCast(ctl.Tag, ParameterDT).DataValue = ctl.Text
            'CurrentParameters.Item(index).DataValue = ctl.text
            'index += 1
        End If
    Next

Upvotes: 1

Views: 1866

Answers (1)

LarsTech
LarsTech

Reputation: 81610

When you create your TextBoxes, you can use the Tag property of the TextBox to hold each parameter object:

For Each p As ParameterDT In BCWorkerParam.GetData(cboQueries.SelectedValue)
  Dim txtBox As New TextBox()
  txtBox.Properties = yada-yada-yada
  txtBox.Tag = p
  pnlParameters.Controls.Add(txtBox)
Next

Then when you need to read the values, you can try something like this:

For Each txtBox As TextBox In pnlParameters.Controls.OfType(Of Textbox)()
  DirectCast(txtBox.Tag, ParameterDT).DataValue = txtBox.Text
Next

Upvotes: 1

Related Questions