Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. why it didn't find the control with its name?[SOLVED]

why it didn't find the control with its name?[SOLVED]

Scheduled Pinned Locked Moved Visual Basic
helpquestion
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sanyexian
    wrote on last edited by
    #1

    Hello,everyone! I tried to use Sub like below to caculate. But when the project run, the two textboxes which I wanted to get their "Text" value could not find.Could anyone give me some suggestions or help? Thank you!

    Public Function TotalSub(ByVal j As Integer, ByVal diameter As String, ByVal thickness As String, ByVal material As String)
    Dim txtGG, txtYL As TextBox
    Dim b As String = ""
    Dim sum As Single = 0
    For Each ctrl As Control In frmQia.Controls
    If Not TypeOf ctrl Is TextBox Then Continue For
    For i = 1 To j
    If ctrl.Name = "txtTJGG" & i Then
    txtGG = ctrl
    ElseIf ctrl.Name = "txtTJYL" & i Then
    txtYL = ctrl
    End If
    b = Caculate.Weight_Sub(diameter, txtGG.Text, thickness, material, txtYL.Text)
    sum += CSng(b)
    Next
    Next
    Return sum
    End Function

    (I have try to find textboxes like these code in another project with only one form, it could find them. So I don't know why it didn't work when it in a project with some forms:confused::confused:)

    L L D 3 Replies Last reply
    0
    • S sanyexian

      Hello,everyone! I tried to use Sub like below to caculate. But when the project run, the two textboxes which I wanted to get their "Text" value could not find.Could anyone give me some suggestions or help? Thank you!

      Public Function TotalSub(ByVal j As Integer, ByVal diameter As String, ByVal thickness As String, ByVal material As String)
      Dim txtGG, txtYL As TextBox
      Dim b As String = ""
      Dim sum As Single = 0
      For Each ctrl As Control In frmQia.Controls
      If Not TypeOf ctrl Is TextBox Then Continue For
      For i = 1 To j
      If ctrl.Name = "txtTJGG" & i Then
      txtGG = ctrl
      ElseIf ctrl.Name = "txtTJYL" & i Then
      txtYL = ctrl
      End If
      b = Caculate.Weight_Sub(diameter, txtGG.Text, thickness, material, txtYL.Text)
      sum += CSng(b)
      Next
      Next
      Return sum
      End Function

      (I have try to find textboxes like these code in another project with only one form, it could find them. So I don't know why it didn't work when it in a project with some forms:confused::confused:)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      because your TextBoxes have names you are not looking for? because they aren't part of your Form, but are part of another container (Panel, GroupBox, ...)? because they aren't exactly TextBoxes? (maybe MaskedTextBox) because your input parameters are wrong? (such as j=0) because you are calling the SUB when the Form isn't holding anything yet/any more? your guess could be even better as mine... :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, and update CP Vanity to V2.0 if you haven't already.

      1 Reply Last reply
      0
      • S sanyexian

        Hello,everyone! I tried to use Sub like below to caculate. But when the project run, the two textboxes which I wanted to get their "Text" value could not find.Could anyone give me some suggestions or help? Thank you!

        Public Function TotalSub(ByVal j As Integer, ByVal diameter As String, ByVal thickness As String, ByVal material As String)
        Dim txtGG, txtYL As TextBox
        Dim b As String = ""
        Dim sum As Single = 0
        For Each ctrl As Control In frmQia.Controls
        If Not TypeOf ctrl Is TextBox Then Continue For
        For i = 1 To j
        If ctrl.Name = "txtTJGG" & i Then
        txtGG = ctrl
        ElseIf ctrl.Name = "txtTJYL" & i Then
        txtYL = ctrl
        End If
        b = Caculate.Weight_Sub(diameter, txtGG.Text, thickness, material, txtYL.Text)
        sum += CSng(b)
        Next
        Next
        Return sum
        End Function

        (I have try to find textboxes like these code in another project with only one form, it could find them. So I don't know why it didn't work when it in a project with some forms:confused::confused:)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Hi :) It might be handy to emit the name and types of the controls that you encounter to the debugger. Something similar to the code below would reveal the names of the components on your form that get looped;

        Public Function TotalSub(ByVal j As Integer, ByVal diameter As String, ByVal thickness As String, ByVal material As String)
        Dim txtGG, txtYL As TextBox
        Dim b As String = ""
        Dim sum As Single = 0
        For Each ctrl As Control In frmQia.Controls
        ** System.Diagnostics.Debugger.WriteLine ("Type {0}, named {1}", ctrl.GetType().FullName, ctrl.Name)**
        If Not TypeOf ctrl Is TextBox Then Continue For
        For i = 1 To j
        If ctrl.Name = "txtTJGG" & i Then
        txtGG = ctrl

        I'm guessing that the TextBox is contained inside another control. That means that you'd have to loop the Controls-collection of the Control that you've put the textbox in.

        I are Troll :suss:

        1 Reply Last reply
        0
        • S sanyexian

          Hello,everyone! I tried to use Sub like below to caculate. But when the project run, the two textboxes which I wanted to get their "Text" value could not find.Could anyone give me some suggestions or help? Thank you!

          Public Function TotalSub(ByVal j As Integer, ByVal diameter As String, ByVal thickness As String, ByVal material As String)
          Dim txtGG, txtYL As TextBox
          Dim b As String = ""
          Dim sum As Single = 0
          For Each ctrl As Control In frmQia.Controls
          If Not TypeOf ctrl Is TextBox Then Continue For
          For i = 1 To j
          If ctrl.Name = "txtTJGG" & i Then
          txtGG = ctrl
          ElseIf ctrl.Name = "txtTJYL" & i Then
          txtYL = ctrl
          End If
          b = Caculate.Weight_Sub(diameter, txtGG.Text, thickness, material, txtYL.Text)
          sum += CSng(b)
          Next
          Next
          Return sum
          End Function

          (I have try to find textboxes like these code in another project with only one form, it could find them. So I don't know why it didn't work when it in a project with some forms:confused::confused:)

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Did you put these TextBoxs in another container on the Form such as a Panel?? If they are inside another container, you won't find them because they are not in the Forms Controls collection.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          S 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Did you put these TextBoxs in another container on the Form such as a Panel?? If they are inside another container, you won't find them because they are not in the Forms Controls collection.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            S Offline
            S Offline
            sanyexian
            wrote on last edited by
            #5

            Hi,everyone!The Textboxes were in a Tabpage which was in the Form I wrote in code to find. So I should find textboxes I need in frmQia.Tabcontrol1? :wtf:

            S 1 Reply Last reply
            0
            • S sanyexian

              Hi,everyone!The Textboxes were in a Tabpage which was in the Form I wrote in code to find. So I should find textboxes I need in frmQia.Tabcontrol1? :wtf:

              S Offline
              S Offline
              Simon_Whale
              wrote on last edited by
              #6

              Yes you would go through the controls on the tab page e.g. where 0 is the tab page you want to check for controls

              For Each ctrl As Control In TabControl1.TabPages(0).Controls

              .... do checks and other stuff here
              

              Next

              As barmey as a sack of badgers Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.

              S 2 Replies Last reply
              0
              • S Simon_Whale

                Yes you would go through the controls on the tab page e.g. where 0 is the tab page you want to check for controls

                For Each ctrl As Control In TabControl1.TabPages(0).Controls

                .... do checks and other stuff here
                

                Next

                As barmey as a sack of badgers Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.

                S Offline
                S Offline
                sanyexian
                wrote on last edited by
                #7

                Thank you, I will try it now! :-D

                1 Reply Last reply
                0
                • S Simon_Whale

                  Yes you would go through the controls on the tab page e.g. where 0 is the tab page you want to check for controls

                  For Each ctrl As Control In TabControl1.TabPages(0).Controls

                  .... do checks and other stuff here
                  

                  Next

                  As barmey as a sack of badgers Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.

                  S Offline
                  S Offline
                  sanyexian
                  wrote on last edited by
                  #8

                  Hi,Simon! I have tried what like you said,but it didn't find the textboxes.In this project, the Form "frmQia" was Inited after I clicked a button and after I inputed some textes in it, I would leave it. The sub that I wrote will be called in another Button_Click event after I leave the form.I think the form "frmQia" has hold the textboxes and their name were right.So...What happened to me...

                  H 1 Reply Last reply
                  0
                  • S sanyexian

                    Hi,Simon! I have tried what like you said,but it didn't find the textboxes.In this project, the Form "frmQia" was Inited after I clicked a button and after I inputed some textes in it, I would leave it. The sub that I wrote will be called in another Button_Click event after I leave the form.I think the form "frmQia" has hold the textboxes and their name were right.So...What happened to me...

                    H Offline
                    H Offline
                    Henry Minute
                    wrote on last edited by
                    #9

                    It sounds as if you are trying to get values from one form by clicking a button on a different form. If so, Passing Values between Forms in .NET 1.x with C# and VB.NET examples[^] might be useful to you.

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                    S 1 Reply Last reply
                    0
                    • H Henry Minute

                      It sounds as if you are trying to get values from one form by clicking a button on a different form. If so, Passing Values between Forms in .NET 1.x with C# and VB.NET examples[^] might be useful to you.

                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                      S Offline
                      S Offline
                      sanyexian
                      wrote on last edited by
                      #10

                      Hello,Henry! Thanks for your suggestion and the artcile.I've read it.It also give me some help!I have solved this quesiton with all you suggest.Finally I found the textboexes were in a form's tabpage's pannel. Thanks all of you!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups