Accessing elements from a different window form  
Author Message
SneakyNinja





PostPosted: Visual C# Express Edition, Accessing elements from a different window form Top

Hey everyone,

Just downlaoded C# EE (Express Edition) the other day and ive gone through the 10 hours of Absolute Beginners videos, so i have a decent understanding of C# now and the way Visual CSharp Express Edition 2005 works.

My question is this,

Say i have a new Windows Form popup as a dialogue box with a single text area known as textBox1, and when someone clicks on the "Add" button it is to add the contents of the textbox onto a RichTextArea known as mainWin in the "main" class. So what im asking is, in the class button click 'contructor' i think its called (when you double click on the button in the designer view) How would i go about accessing main.mainWin (and its set to public)


(ps. id LOVE to post source code, but im currently away from my home computer, so if this cannot be helped without the use of source code, then please inform me)



Visual Studio Express Editions21  
 
 
ahmedilyas





PostPosted: Visual C# Express Edition, Accessing elements from a different window form Top

dont know if this answers your question...

...if you need to acccess a component from another form for example, you need to pass the reference of the other form, to the form you will be calling from, and set the component as public.

Example...

I have 2 forms, form A has a textbox. FormB has button:

A -> has textbox

B -> has button.

 

in form B, if I press the button, I want Form A's textbox to display some text. To do this, I need to make the textbox in formA public (look in the modifiers property on the FormA designer for the textbox control). I also need to pass form A to formB so it can access it. Example:

 

//FormB:

private FormA theMainForm = null;

public class FormB(FormA mainForm)

{

   this.theMainForm = mainForm;

}

 

private void button1_Click(object sender, EventArgs e)

{

   this.theMainForm.theTextBox.Text = "hello!";

}

 

FormA:

//some method:

 

FormB theSecondForm = new FormB(this);

theSecondForm.Show();

 

 

does this help

in order to access one component from another, you need to pass the component appropriate to the callee so it can have a reference and access it



 
 
OmegaMan





PostPosted: Visual C# Express Edition, Accessing elements from a different window form Top

Examples are some of the best way to learn. Check out some of the VB/C# examples for the basic operations or some more robust examples as found in the Starter Kits.


 
 
SneakyNinja





PostPosted: Visual C# Express Edition, Accessing elements from a different window form Top

The quick response is much appreciated and ill give it a shot when i get home to try it out,

and OmegaMan, thanks for the link to the starter kits, those will help immensely. :D