how to access objects in one form via a different one

G

Guest

hi,

i am making a word processor. i have the main form and my own version of the
character map in another. the main form has a rich textbox in it. my aim is
for the user to click on a character in the character map and that character
will appear in the main form's richtextbox.

i hope my question was understandable. if not, feel free to ask any
questions about this question

thanks in advance

whoops, gotta go pick up my son!
 
O

Octavio Hernandez

Hi Alvo,

Just create a public variable (a read only property would be a better
design) in your character map form and assign the selected character to it
so that you can access it from the word processor form:

// *** IN WORD PROCESSOR FORM
// launch Character Map form
FormCharacterMap cm = new FormCharacterMap();
if (cm.ShowDialog() == DialogResult.Ok)
{
char ch = cm.SelectedCharacter;
}

// *** IN CHAR MAP FORM
public char SelectedCharacter;
// assign value to this var whenerver the user selects a char...

Regards - Octavio
 
G

Guest

hi there,

i just realised that i didnt even ask the question! sorry! here it is:

how do i access the main form's (Form1) richtextbox?

i just need to know that. the rest wont be a challenge unless any of you
dudes know from experience that it is a pain in the backside
 
O

Octavio Hernandez

Alvo,

Rewrite your Main() method so that instead of:

public static void Main()
{
Application.Run(new MainForm());
}

it says:

public static MainForm AppMainForm;

public static void Main()
{
AppMainForm = new MainForm();
Application.Run(AppMainForm);
}

After that, you'll be able to refer to your main form from any other form or
class using:

MainForm.AppMainForm

Regards - Octavio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top