Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow
in Myproject ,i have 2 forms (Form1 and Form2)
i want to disable or enable one member of the menuestrip (formatToolStripMenuItem) either in Form1 or in Form2
the error i have got is "an object refrence is required for the non-static field ,method,or property
Myproject.form1.formatToolStripMenuItem"

assume Form1 has the following 3 methods
C#
    private void Form1_Load(object sender, EventArgs e)
    {
     //some codes then
     TurnOff_RestOfToolStripItems();
}

private void TurnOff_RestOfToolStripItems()
    {
        formatToolStripMenuItem.Enabled = false;
    }

public static void TurnOn_RestOfToolStripItems_22()//<-------Error
{
        formatToolStripMenuItem.Enabled = true;
}

in Form2 i wrote method like this
C#
private void SomeCalculation()
{
//few codes then
Form1.TurnOn_RestOfToolStripItems_22();// i could not turn menue strip to true
}

i appreciate your kind help
Posted
Updated 5-Sep-14 2:21am
v4

I'm posting a 2nd (more detailed) solution since replies to the thread doesn't support formatted text.

  1. Expose a Form1 method that allows its toolstrip items to be enabled/disabled.
    public void EnableAdditionalToolstripItems
        (bool enable)
    {
        if (enable)
            this.TurnOn_RestOfToolStripItems_22();
        else
            this.TurnOf_RestOfToolStripItems_22();
    }
    

  2. In Form2, define a reference to Form1.
    public Form2 : Form
    {
        ...
    
        public Form1 ReferenceToForm1 {
            get;
            set;
        }
    }
    

  3. When Form1 is about to display Form2, pass it a reference to Form1.
    // Construct and display Form2 from Form1
    Form2 f2 = new Form2();
    f2.ReferenceToForm1 = this;
    f2.ShowDialog(this);
    

  4. Form2 can now enable/disable its parent Form1's toolstrip items by calling:
    this.ReferenceToForm1.EnableAdditionalToolstripItems (true/false);
    

/ravi
 
Share this answer
 
Comments
Engineer khalid 6-Sep-14 14:30pm    
Excellent it works
Ravi Bhavnani 6-Sep-14 14:31pm    
Great. Please mark the solution as accepted so that it's easier for others to find.

/ravi
Engineer khalid 6-Sep-14 14:59pm    
i only changed this
//f2.ShowDialog(this);
f2.Show(this);

  1. TurnOn_RestOfToolStripItems_22() should be an instance method, not static.
  2. Form2 needs a reference (e.g. _form1) to a Form1 instance.
  3. Given that reference, SomeCalculation() should be rewritten as:
    private void SomeCalculation()
    {
      //few codes then
      this._form1.TurnOn_RestOfToolStripItems_22();
    }
    

That being said, a Form shouldn't directly modify the UI of another Form.  It's better to raise an event to indicate a specific situation; other objects can then react to that event and modify themselves in any way the deem fit.  This "separation of concerns" allows for loose coupling (and therefore more easily maintained) classes.

/ravi
 
Share this answer
 
Comments
Engineer khalid 5-Sep-14 8:17am    
Dear ravi. before i comment in your answer i created a textbox in the program not by toolBox and declared it as Public static,then in Form2 i could call it. i tried to do same thing for the menu but could not do that,i am a mechanical engineer, forgive me if i could not understand what u have wrote. How could i refrence Form2 to Form1 ? . i tried to declare the methode like this
Public instance void TurnOn_RestOfToolStripItems_22()
i wish if you give me full example
Ravi Bhavnani 5-Sep-14 9:55am    
I don't recommend your approach.  By creating a static TextBox, you're declaring a single instance for all instances of Form1, which is clearly incorrect.  Instead, create a TextBox using the toolbox, and follow the instructions in my solution.

To pass a Form1 reference to Form2, do the following:

(1) Add a public Form1 property to Form2.
(2) When you create an instance of Form2 (from Form1), set the value of that property.
(3) Perform step 3 as mentioned in my solution.

If what I've said doesn't make sense, I recommend you read up on the basics of C# and Windows Forms. That will help you implement your requirements.

/ravi
Engineer khalid 6-Sep-14 8:56am    
the 1st instruction has led me to find the solution
a breive solution is like this
in form1
public partial class Form1 : Form
{ public Form2 f2;
.
.
//in button click write
f2 = new Form2(this); // no, do so now
in form2
Form1 f1; // holds reference to parent form
public Form2()
{InitializeComponent();}
public Form2(Form1 ff)
{InitializeComponent();f1 = ff; // save reference to parent form}
private void button1_Click(object sender, EventArgs e)
{f1.textBox2.Text = textBox2.Text;}
To close f2 write
this.Close();
f1.f2.Dispose();
f1.f2 = null;
Ravi Bhavnani 6-Sep-14 9:33am    
Does Form1 display Form2?

/ravi
Engineer khalid 6-Sep-14 13:44pm    
yes it does

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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