declare a form so that you can reference it's controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tanzen

    declare a form so that you can reference it's controls

    Here's what I'm trying to do in vb.net 2005.

    I have a public declared method that sets the value of a control on a
    form, but that form could change, so I don't want to code its actual
    name. I want it to be a parameter in the module method so that you
    pass the form name of whatever form called the method.

    So let's say we call the method from form 1:

    UpdateText(frmN ame)


    Then in the module we have the actual method

    Public Sub UpdateText(ByVa l frmName as Form)

    frmName.textbox 1.text = "Hi, I'm Me."

    end Sub

    Now, the problem that arises is that frmName gets underlined by visual
    studio and says: TextBox1 is not a member of
    'System.Window. Forms.Form'. So apparently my parameter in the method
    is wrong. But I can't find out what type the form should be. Can
    anyone help me?

    Thanks
  • breitak67

    #2
    Re: declare a form so that you can reference it's controls


    TextBox1 is not a member of System.Windows. Forms.Form - it is a member
    of the Form1 class, which is a subclass of System.Windows. Forms.Form.
    Try this:

    Public Class Form1
    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles Button1.Click
    Utilities.SetCo ntrol(Me, \"ExampleText\" )
    End Sub
    End Class

    Public Class Utilities
    Public Shared Sub SetControl(ByRe f MyForm As Form1, ByVal
    MyTextVal As String)
    MyForm.TextBox1 .Text = MyTextVal
    End Sub
    End Class

    If you need the flexibility to set the text of forms that are of class
    Form1, then pass a reference to the control instead of the form, like
    this:

    Public Class Form1
    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles Button1.Click
    Utilities.SetCo ntrol(Me.TextBo x1, \\"ExampleText\ \")
    End Sub
    End Class

    Public Class Utilities
    Public Shared Sub SetControl(ByRe f MyTextBox As TextBox, ByVal
    MyTextVal As String)
    MyTextBox.Text = MyTextVal
    End Sub
    End Class


    --
    breitak67

    Comment

    • Tanzen

      #3
      Re: declare a form so that you can reference it's controls

      On Jul 4, 8:30 pm, breitak67 <gu...@unknow n-email.comwrote:
      TextBox1 is not a member of System.Windows. Forms.Form - it is a member
      of the Form1 class, which is a subclass of System.Windows. Forms.Form.
      Try this:
      >
      Public Class Form1
      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
      As System.EventArg s) Handles Button1.Click
      Utilities.SetCo ntrol(Me, \"ExampleText\" )
      End Sub
      End Class
      >
      Public Class Utilities
      Public Shared Sub SetControl(ByRe f MyForm As Form1, ByVal
      MyTextVal As String)
      MyForm.TextBox1 .Text = MyTextVal
      End Sub
      End Class
      >
      If you need the flexibility to set the text of forms that are of class
      Form1, then pass a reference to the control instead of the form, like
      this:
      >
      Public Class Form1
      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
      As System.EventArg s) Handles Button1.Click
      Utilities.SetCo ntrol(Me.TextBo x1, \\"ExampleText\ \")
      End Sub
      End Class
      >
      Public Class Utilities
      Public Shared Sub SetControl(ByRe f MyTextBox As TextBox, ByVal
      MyTextVal As String)
      MyTextBox.Text = MyTextVal
      End Sub
      End Class
      >
      --
      breitak67
      Thank you very much for the help. Most appreciated!

      Comment

      Working...