Access Control from another form
-
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
-
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
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"
-
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"
-
Same principal, but in C# you make the member
public
orinternal
rather thanprotected
orprivate
. For a visual control, you can go to it's properties in the designer and set this via theModifiers
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)
-
Same principal, but in C# you make the member
public
orinternal
rather thanprotected
orprivate
. For a visual control, you can go to it's properties in the designer and set this via theModifiers
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)