Click here to Skip to main content
15,908,115 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: connection string Pin
Abhijit Jana21-Jan-09 21:10
professionalAbhijit Jana21-Jan-09 21:10 
GeneralRe: connection string Pin
kulandaivel_mca200721-Jan-09 22:46
kulandaivel_mca200721-Jan-09 22:46 
GeneralRe: connection string Pin
Abhijit Jana21-Jan-09 23:18
professionalAbhijit Jana21-Jan-09 23:18 
AnswerRe: connection string Pin
saberbladez21-Jan-09 21:39
saberbladez21-Jan-09 21:39 
QuestionAccess Control from another form Pin
Ola E21-Jan-09 19:46
Ola E21-Jan-09 19:46 
AnswerRe: Access Control from another form Pin
Ashutosh Phoujdar22-Jan-09 0:35
Ashutosh Phoujdar22-Jan-09 0:35 
AnswerRe: Access Control from another form Pin
Ola E22-Jan-09 1:17
Ola E22-Jan-09 1:17 
GeneralRe: Access Control from another form Pin
Ben Fair22-Jan-09 6:21
Ben Fair22-Jan-09 6:21 
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)

AnswerRe: Access Control from another form Pin
Ola E22-Jan-09 19:06
Ola E22-Jan-09 19:06 
Questionbinary file encryption Pin
Member 455503121-Jan-09 4:22
Member 455503121-Jan-09 4:22 
AnswerRe: binary file encryption Pin
Ben Fair22-Jan-09 5:59
Ben Fair22-Jan-09 5:59 
QuestionTimed GTalk Status Updater Pin
itsravie21-Jan-09 0:25
itsravie21-Jan-09 0:25 
AnswerRe: Timed GTalk Status Updater Pin
Dave Kreskowiak21-Jan-09 2:02
mveDave Kreskowiak21-Jan-09 2:02 
AnswerRe: Timed GTalk Status Updater Pin
Eddy Vluggen21-Jan-09 2:34
professionalEddy Vluggen21-Jan-09 2:34 
GeneralRe: Timed GTalk Status Updater Pin
Mohammad Dayyan21-Jan-09 8:52
Mohammad Dayyan21-Jan-09 8:52 
AnswerRe: Timed GTalk Status Updater Pin
itsravie25-Jan-09 12:07
itsravie25-Jan-09 12:07 
QuestionDeserialisation Problem with Generic Methods and Delegates Pin
mi220-Jan-09 22:43
mi220-Jan-09 22:43 
QuestionBCP process does not throw proper exceptions incase of any failures Pin
indian14320-Jan-09 19:52
indian14320-Jan-09 19:52 
AnswerSorry the message became too big as I am unable to edit it, again I am writing the question here [modified] Pin
indian14320-Jan-09 19:57
indian14320-Jan-09 19:57 
GeneralRe: Sorry the message became too big as I am unable to edit it, again I am writing the question here Pin
Dave Kreskowiak21-Jan-09 2:00
mveDave Kreskowiak21-Jan-09 2:00 
AnswerRe: BCP process does not throw proper exceptions incase of any failures Pin
Wendelius21-Jan-09 5:19
mentorWendelius21-Jan-09 5:19 
Question.Net Pin
Smtnri20-Jan-09 8:50
Smtnri20-Jan-09 8:50 
AnswerRe: .Net Pin
Not Active20-Jan-09 9:01
mentorNot Active20-Jan-09 9:01 
AnswerWow. Pin
leckey20-Jan-09 9:47
leckey20-Jan-09 9:47 
AnswerRe: .Net Pin
Pete O'Hanlon20-Jan-09 10:38
mvePete O'Hanlon20-Jan-09 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.