Click here to Skip to main content
15,888,047 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hey everyone.

I have a little question.

I have a main windows form and another form.

Another form needs access the controls(buttons, picturebox, etc) of the main form.
When I inherit the other form from the main form, the appearance of the inhereited one becomes exactly like main form.

How can I overcome this and access the main form's controls at the same time.

I want another design not to change!

My best regards...
Posted
Updated 15-Nov-11 22:55pm
v3
Comments
Pandya Anil 16-Nov-11 4:42am    
how can you inherit properties partially ? you want to access its value property but dont want to access the visibly property.. i think inheritance is not your solution...
[no name] 16-Nov-11 4:53am    
This a volunteer site and people will answer on their time not yours. Asking urgent help is very RUDE
Un_NaMeD 16-Nov-11 4:56am    
Apologies.
My english is poor, so if I mean rude unknowingly, sorry for that...
BillWoodruff 16-Nov-11 6:41am    
Please describe:

1. where are Form1 and Form2's instances created: does Form1 create Form2 when it is created ? Are they both created by some other Form ?

2. give a concrete example of some interaction you want to happen between a Control on Form2 and a Control on Form1 : for example: if a user chooses a certain value in ComboBox1 on Form2: then on Form1's TextBox2 I want to see the text of the value the user chose in the ComboBox on Form2.
Un_NaMeD 16-Nov-11 6:43am    
Hi sir.
Example:
choser will click on a button on form2, then a map will be drawn in form1.
This is the scenario.

... edit #1 ... in which Form2 is not derived from Form1 ...

Un_NaMed wrote: "
Quote:
In form 2(derived form), the user will click on a button,
a map will be drawn in Form 1's picturebox.
Okay, let's assume that Form2 is created in Form1:

0. in Form1's code:
C#
// we create Form2 in a button click in Form1
private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.Show();

    // now we can assign an Event Handler in Form1 to Form2's button click
    // until the Form is shown the Button's not created yet ...
    f2.f2Button.Click += new EventHandler(f2Button_Click);
}

public void f2Button_Click(object sender, EventArgs e)
{
    // this is where you would need to draw you Map, or whatever on Form1
    MessageBox.Show("button clicked in Form2 handled in Form1");
}
1. in Form2's code:
C#
// public property of type Button
public Button f2Button { get; set; }

private void Form2_Load(object sender, EventArgs e)
{
   // set the public property when Form2 is created
   f2Button = this.button1;
   // assign a click handler
   f2Button.Click +=new EventHandler(f2Button_Click);
}

private void f2Button_Click(object sender, EventArgs e)
{
   MessageBox.Show("button click handled in Form2");
}
If you compile and run this example, in a project where you have created a Main Form, Form1, and added another form, Form2, and added a button on each Form, then you should observe that when the project is run: both Forms appear, and that clicking on the button in Form2 will trigger two events: it will execute the button-click handler on Form2 first and then execute the button-click handler fired on Form1.

... end edit #1 ...

Discussion: You are confusing here two very different issues:

1. inheritance: if you create an instance of Form2 that inherits from Form1: you get an exact copy of Form1: each Form has its own set of identical controls, and they are completely independent of each other.

2. communication across Forms between Controls/Objects: I believe this is what you are asking about, and this is one of the most frequently asked and answered questions here on QA.

The important thing here is to define the nature of the communication required:

1. do you want something on Form2 to raise an Event that Form1 will subscribe to, and pass information in that Event that Form1 can then use ?

2. do you want to achieve "binding:" where whatever is typed, for example, in a certain TextBox in Form2 appears in a TextBox in Form1 ?

3. Or, does some control on Form2 need access to some control on Form1 in order to manipulate it: for example, you might wish a change in selection in a ComboBox drop-down on Form2 to have some effect on a control in Form1.

4. In the case that every control on Form2 requires access to every control on Form1: a different scenario arises: involving injecting a reference to the instance of Form1 into Form2 when it is created, and making controls on Form1 publicly accessible through public properties. This may be loosely called "injection."

The first step is to define exactly the nature of the dynamic links between the controls on Form2 and Form1.

You need to get a good book on C#, like one from Jesse Liberty, and study up on inheritance, events, properties, etc.
 
Share this answer
 
v3
Comments
Un_NaMeD 16-Nov-11 6:41am    
Hi sir.
The thing I'm trying to do is briefly this:
In form 2, the user will click on a button,
a map will be drawn in Form 1's picturebox.
So this is related to #1.
How can I achieve this?
Thank you for ur advices.
Make that controls modifier public from Properties Panel .Then just pass main form instance to child form.Also wou can write wrapper for that controlsto modify them.


C#
 public partial class mainForm : Form
{
    public mainForm(){
      new childForm(this).setMyPictureLeft(0);
   }

}  
  public partial class childForm : Form
{

     mainForm form;
    public childForm(mainForm main){
   
       form=main;

    }
   public setMyPictureLeft(int left){

      form.pictureBox1.left=0;

    }


}


This is worst you can do.Instead write setMyPictureLeft in mainForm and no need to set modifer public.
 
Share this answer
 
v2
Comments
Un_NaMeD 16-Nov-11 4:39am    
Hi dieforwhat.
The control's are all public.
What you mean by pass main form instance to child form? I couldn't get it.
Thank you.
Un_NaMeD 16-Nov-11 5:03am    
How can I do that? "just pass main form instance"
programmatically?
Setting something?
I'm confused :/
dieforwhat 16-Nov-11 5:27am    
Before you use the other form,you should have that form's instance.
How to set this instance is your choice.You can pass it in childform constructor,set some properties which is in type of main form,or call a function on child form give main form as parameter and set what you want in that form ...Is this explict?
Another form needs access the controls(buttons, picturebox, etc) of the main form.
When I inherit the other form from the main form, the appearance of the inhereited one becomes exactly like main form.

Your question getting ambiguous as shown above bold and underline so first correct your question what exactly you want.
 
Share this answer
 
Comments
Un_NaMeD 16-Nov-11 4:52am    
Ok.
There is one main form and another form.
Another form can set/get the properties of controls of main form.
Each forms designs must be different.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900