• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

Help with passing Textbox value to a variable (VS 2010 Windows Application)

hi,

I am using the code below to loop through multiple controls and pass text entered to a variable Txt and save  its values to the xml files. However, eventhough I have data in my Manufacturer Textbox, Txt is still empty. How do I fix this problem?

  Dim xyz() As String = New String() {"NSN", "Manufacturer", "Date"}
         
For i = 1 To 2
            Dim curXYZ = xyz(i)
            Dim MyFix As XDocument = XDocument.Load((Application.StartupPath + "\App_DataA\" & curXYZ & ".xml"))

            Dim xid As String
            xid = curXYZ & "_ID"
            Dim LargestFixID = (From el In MyFix.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToList
            LargestFixID.Sort()
            Dim xtable As String
            xtable = curXYZ & "Table"

            Dim ctrl As Control = Screen1.Controls("C1" & curXYZ)

            Dim Txt As String = Nothing
            If TypeOf (ctrl) Is ListBox Then
                Txt = CType(ctrl, ListBox).Text
            ElseIf TypeOf (ctrl) Is TextBox Then
                Txt = CType(ctrl, TextBox).Text
            End If

  Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).Elements(curXYZ) Select el.Value).ToList
            'Check if value is already in xml files, if not add it to the xml files
            If Not CheckForItemFix.Contains(Txt) Then
                Dim xNew As XElement = New XElement(xtable)
                xNew.Add(New XElement(xid, (LargestFixID.Count() + 1)))
                xNew.Add(New XElement(curXYZ, Txt))
                MyFix.Root.Add(xNew)
                If i = 1 Then
                    MsgBox("TXT" & Txt)
                End If
                MyFix.Save((Application.StartupPath + "\App_DataA\" & curXYZ & ".xml"))
Next


Thanks,

Victor
0
vcharles
Asked:
vcharles
1 Solution
 
igordevelopCommented:
Hi,

If I understood you well, you have problem in this code:

Dim Txt As String = Nothing
            If TypeOf (ctrl) Is ListBox Then
                Txt = CType(ctrl, ListBox).Text
            ElseIf TypeOf (ctrl) Is TextBox Then
                Txt = CType(ctrl, TextBox).Text
            End If


You can try to access the control value by this way:

Dim Txt As String = Nothing
Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is TextBox Then
                Txt = ctrl.Text
            If .......
        Next

Let me know if anything.

Regards,
Igor
0
 
vcharlesAuthor Commented:
Hi,

Thank you  for the solution.

Victor
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now