Win App VB : how do we access or address a form and its element from a module?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pod
    Contributor
    • Sep 2007
    • 298

    Win App VB : how do we access or address a form and its element from a module?

    I am trying to seperate my functions from the form file.

    In my function I want to modify the properties and values of the form and its elements. How should I formulate this?

    In Access I could address a form in this manner but it doesn't work in .NET:
    Code:
    Form_OrderForm.Visible = false
    Forms!OrderForm.Visible = false
    Forms!OrderForm.Controls!NewData
    any input will be appreciated

    Thank you


    P:oD
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Originally posted by pod
    I am trying to seperate my functions from the form file.

    In my function I want to modify the properties and values of the form and its elements. How should I formulate this?

    In Access I could address a form in this manner but it doesn't work in .NET:
    Code:
    Form_OrderForm.Visible = false
    Forms!OrderForm.Visible = false
    Forms!OrderForm.Controls!NewData
    any input will be appreciated

    Thank you


    P:oD
    Ok, let's say you have a form called Form1. On that form you have TextBox1, Button1, and Label1. If you double-click on the form background (assuming you are using visual studio) it will generate the function for the form's "load" event. Then inside that function, or any function within that class (note that the function is a member of the class Form1) you can reference any object directly. Say I want to change the text box's text:
    [code=vb]
    TextBox1.Text = "Text"
    [/code]

    That's it. Say I want to change the background color of a label:
    [code=vb]
    Label1.BackColo r = Color.Brown
    [/code]

    You can also generate the event methods by double clicking the object in the design view. If you double click the button, it generates the method for that button's "click" event.

    Overall point being, you don't have to reference the form's controls through the form itself.

    Comment

    • pod
      Contributor
      • Sep 2007
      • 298

      #3
      I appreciate your reply and understand what you are saying, but it's from a MODULE that I want to make those changes.

      If I double-click an element, VS generates all the code for me but that code is on the Form1.vb. I am trying to move the property and value changing code away from the main form file "Form1.vb" to other file such as generalFunction s.vb, and headerandFooter .vb

      Thanks anyway for your reply


      P :oD

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Originally posted by pod
        I appreciate your reply and understand what you are saying, but it's from a MODULE that I want to make those changes.

        If I double-click an element, VS generates all the code for me but that code is on the Form1.vb. I am trying to move the property and value changing code away from the main form file "Form1.vb" to other file such as generalFunction s.vb, and headerandFooter .vb

        Thanks anyway for your reply


        P :oD
        Sorry, I misread your original question. My post must have seemed condescending. Didn't mean it that way. Sorry I couldn't help.

        Comment

        • pod
          Contributor
          • Sep 2007
          • 298

          #5
          I figured out a way, may not be the best way but it works

          I am sending the form as an object to my functions on the modules and I am able address the elements using that pointer.

          Code:
          'on the main form
          
              Private Sub HDR_edcode_txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HDR_edcode_txt.TextChanged
          
                   fill_district_lst(Me)  
          
              End Sub
          
          
          
          'on the module
              Public Sub fill_district_lst(ByRef myForm As Form)
                  'first trim any leading or lagging spaces
                  Dim HDR_txt As TextBox = myForm.Controls.Item("HDR_edcode_txt")
                  Dim HDR_lst As ListBox = myForm.Controls.Item("HDR_edcode_lst")
          
                  Dim strTrim As String = HDR_txt.Text.ToLower()
                  Dim fldLEN As Int32 = strTrim.Length
                  ' VISIBILITY: visible
                  HDR_lst.Visible = True
                  ' clear the existing options
                  HDR_lst.Items.Clear()

          Comment

          • pod
            Contributor
            • Sep 2007
            • 298

            #6
            Originally posted by insertAlias
            Sorry, I misread your original question. My post must have seemed condescending. Didn't mean it that way. Sorry I couldn't help.

            No worries mate, I took no offense :)

            Comment

            Working...