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. .NET (Core and Framework)
  4. Access Control from another form

Access Control from another form

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionhelp
5 Posts 3 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.
  • O Offline
    O Offline
    Ola E
    wrote on last edited by
    #1

    I'm writing a kind of terminal program that uses the serial port. I have it on my "main form", which does most of the communication. My problem is that I need to add another form that also uses the serie port. Now to my question / problem. How can I access the opened serial port on my main form? //Ola

    A 1 Reply Last reply
    0
    • O Ola E

      I'm writing a kind of terminal program that uses the serial port. I have it on my "main form", which does most of the communication. My problem is that I need to add another form that also uses the serie port. Now to my question / problem. How can I access the opened serial port on my main form? //Ola

      A Offline
      A Offline
      Ashutosh Phoujdar
      wrote on last edited by
      #2

      Hope you are using VB.NET, If yes then, make the variable of serial port instance to public instead of friend e.g.

      Instead of

      Friend WithEvents SerialPort1 As System.IO.Ports.SerialPort

      Use

      Public WithEvents SerialPort1 As System.IO.Ports.SerialPort

      Then you can access it using My.Forms.main-form-name.SerialPort1 Hope I answered your query Thanks Ashu

      dnpro "Very bad programmer"

      O 1 Reply Last reply
      0
      • A Ashutosh Phoujdar

        Hope you are using VB.NET, If yes then, make the variable of serial port instance to public instead of friend e.g.

        Instead of

        Friend WithEvents SerialPort1 As System.IO.Ports.SerialPort

        Use

        Public WithEvents SerialPort1 As System.IO.Ports.SerialPort

        Then you can access it using My.Forms.main-form-name.SerialPort1 Hope I answered your query Thanks Ashu

        dnpro "Very bad programmer"

        O Offline
        O Offline
        Ola E
        wrote on last edited by
        #3

        Thanks for the advice but unfourtanly I'm not using VB.Net. I'm writing in c#...

        B 1 Reply Last reply
        0
        • O Ola E

          Thanks for the advice but unfourtanly I'm not using VB.Net. I'm writing in c#...

          B Offline
          B Offline
          Ben Fair
          wrote on last edited by
          #4

          Same principal, but in C# you make the member public or internal rather than protected or private. For a visual control, you can go to it's properties in the designer and set this via the Modifiers property rather than editing the designer generated code file. Also, the second form will have to have a reference to an instance of the main form; so you'll need to also make that link and then you can access the serial port member as a property of an instance of the main form from the second form. I find the easiest way to establish that link is in the second form's constructor by passing in a reference to the main form:

          private MainForm mainForm = null;

          public SecondForm(MainForm mainForm) <---- constructor
          {
          ...
          this.mainForm = mainForm; <---- save reference to main form
          ...
          }

          private void UseSerialPort()
          {
          // do something with the serial port here
          SerialPort sp = mainForm.SerialPort; <---- access the member on the main form
          }

          And on the main form you'd have:

          private void OpenSecondForm()
          {
          SecondForm secondForm = new SecondForm(this); <----- pass the instance of the main form here
          secondForm.Show();
          }

          Remember that even though they are Forms, they are also still just classes like everything else and the controls on them are just members of the class. So, if you wanted one class to be able to access a member from another class you'd have to increase the visibility of that member in the other class and have a reference to an instance of it. So, you're just doing the same thing here but the classes happen to be Forms. Also, it's probably better practice to make an actual property on the main form to control access to the serial port member. This way you could also make it with just a get and have a readonly property. It's important to consider the ownership and access of objects in the structure of your application. Does it make sense for the serial port object to be 'owned' by the Main Form and shared between it and then second form, or does it make more sense to have a separate class that owns it and from which both forms access it? I can't tell you the answer to that, its just something to keep in mind when you are making these kinds of decisions with your application. Hope this gets you going!

          Keep It Simple Stupid! (KISS)

          O 1 Reply Last reply
          0
          • B Ben Fair

            Same principal, but in C# you make the member public or internal rather than protected or private. For a visual control, you can go to it's properties in the designer and set this via the Modifiers property rather than editing the designer generated code file. Also, the second form will have to have a reference to an instance of the main form; so you'll need to also make that link and then you can access the serial port member as a property of an instance of the main form from the second form. I find the easiest way to establish that link is in the second form's constructor by passing in a reference to the main form:

            private MainForm mainForm = null;

            public SecondForm(MainForm mainForm) <---- constructor
            {
            ...
            this.mainForm = mainForm; <---- save reference to main form
            ...
            }

            private void UseSerialPort()
            {
            // do something with the serial port here
            SerialPort sp = mainForm.SerialPort; <---- access the member on the main form
            }

            And on the main form you'd have:

            private void OpenSecondForm()
            {
            SecondForm secondForm = new SecondForm(this); <----- pass the instance of the main form here
            secondForm.Show();
            }

            Remember that even though they are Forms, they are also still just classes like everything else and the controls on them are just members of the class. So, if you wanted one class to be able to access a member from another class you'd have to increase the visibility of that member in the other class and have a reference to an instance of it. So, you're just doing the same thing here but the classes happen to be Forms. Also, it's probably better practice to make an actual property on the main form to control access to the serial port member. This way you could also make it with just a get and have a readonly property. It's important to consider the ownership and access of objects in the structure of your application. Does it make sense for the serial port object to be 'owned' by the Main Form and shared between it and then second form, or does it make more sense to have a separate class that owns it and from which both forms access it? I can't tell you the answer to that, its just something to keep in mind when you are making these kinds of decisions with your application. Hope this gets you going!

            Keep It Simple Stupid! (KISS)

            O Offline
            O Offline
            Ola E
            wrote on last edited by
            #5

            Thanks, that helped alot. It works as I wanted it to. But I did take youre advice and moved the serial port control to a "ordanary class", witch in terms the two forms can use. Thanks Ola

            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