0
votes

In WPF, I created several TextBoxes dynamically, then later, I go find all the child objects of the Canvas I created them in. As I search, I can get the name of the textbox, but how do I change the text in the textbox?

I have tried:

// oText is the visual object I found when searching for the textbox
oText.Text = "Software" // doesnt work.
oText.SetValue(control.Text) // doesnt work, because there is no .text property

Even through I can debug it, and hover over the oText object, and scroll down and find that Text property is set to "Software", but I can't read it like I can with

oText.GetValue(control.width)

How do we read the text value in WPF of this dynamically created textbox?

Here is the code:

I create a canvas in XAML:

 <Canvas x:Name="Can1" Height="700" Width="874">

        </Canvas>

Then, I make the textboxes and put them on the canvas...

 For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(Can1) - 1
        ' Retrieve child visual at specified index value.
        Dim childVisual As Visual = CType(VisualTreeHelper.GetChild(Can1, i), Visual)
        ' Return the offset vector for the TextBlock object.
        Dim vector As Vector = VisualTreeHelper.GetOffset(childVisual)
        ' Convert the vector to a point value.
        Dim currentPoint As New Point(VisualOffset.X, VisualOffset.Y)
        x = Canvas.GetLeft(childVisual)
        y = Canvas.GetTop(childVisual)

        A = childVisual.GetValue(Control.ActualHeightProperty)
        B = childVisual.GetValue(Control.ActualWidthProperty)     

        Dim myTextbox As New TextBox
        Dim c As Int16
        myTextbox.Width = B
        myTextbox.Text = "Software"
        myTextbox.Name = "TextB" & i.ToString
        Can1.Children.Add(myTextbox)
        Canvas.SetTop(myTextbox, y + A)
        Canvas.SetLeft(myTextbox, x)
        Canvas.SetZIndex(myTextbox, 0)
next i

Then, I use a button on the main window to call GetData...

Private Sub GetData(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim iCount As Int16
    Dim oText As Visual
    Dim sTemp As String
    Dim cs = My.Settings.ConnectionString
    Dim oConn = New SqlConnection(cs)
    Dim cmd As New SqlCommand()
    Text1.text = ""
    cmd.Connection = oConn
    Try
        oConn.Open()
        cmd.CommandText = "select top 5 finumber from fiheading "
        Dim myReader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        While myReader.Read()
            iCount += 1
            oText = FindChild(Can1, "TextB" & iCount.ToString)
            'sTemp = oText.GetValue(Control.NameProperty)
            'oText.text = (myReader.GetString(0))
            'oText.SetValue(Control.text)

        End While
        myReader.Close()
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try


    Try
        oConn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try


End Sub
1
Is oText of type TextBox? Or is it something else generic such as FrameworkElement or object?Rachel
Ok, I put some of my code in the window above. I create the textbox and then go try to find them when a button is pressed. I am getting data from the SQL database, and as you can see I have several commented out lines where I was trying different things. So the object is a visual that I found as a child on the canvas. I can hover over it in debug and click on the +'s and expand the properties to see there is a text property = "Software", but I dont know how to read it or write to it.SDanks

1 Answers

1
votes

oText is defined as a Visual, which does not have a Text property

Change it to a TextBox and cast the result of FindChild to a TextBox and it should work fine

Dim oText As TextBox
...

oText = CType(FindChild(Can1, "TextB" & iCount.ToString), TextBox)