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
oText
of typeTextBox
? Or is it something else generic such asFrameworkElement
orobject
? – Rachel