(C#) How to enable or disable Menu Strip in another form

4,018 views
Skip to first unread message

sahm

unread,
May 3, 2009, 9:31:01 AM5/3/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi every one
How to can I enable or disable Menu Strip in Parent Form from chiliad
form
Best
Salim

Benj Nunez

unread,
May 5, 2009, 6:02:43 AM5/5/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Here's how:

To begin with, let's assume you already have Form1 which has the
menustrip. Make sure
the modifier of your menustrip is set to public. We will construct
Form2 (the child form):

1) Create a New form (the child form)
2) declare a Form1 variable in your child form like:

private static new Form1 ParentForm = null;

3) define a second constructor, with Form1 as a parameter, like:

public Form2(Form1 AParent): this()
{
ParentForm = AParent;
}


4) drag a button in your child form.
5) double click your button and type this:

private void btnEnable_Click(object sender, EventArgs e)
{
ParentForm.menuStrip1.Enabled = !
ParentForm.menuStrip1.Enabled;
}

Now go back to your Form1 class to instantiate Form2. In this example,
put
a button in Form1. Double-click the button in Form1 and add this code:


Form2 form2 = new Form2(this);
form2.ShowDialog();


Pass "this" to Form2. "this" refers to the Form1 instance. It allows
you
to access any controls in Form2. Provided that the controls in Form1
have
their modifiers set to public.


Hope this helps.



Benj

sahm

unread,
May 13, 2009, 5:03:38 AM5/13/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Benj Nunez

thank you for your help

Salim

Benj Nunez

unread,
May 13, 2009, 8:55:11 PM5/13/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You're welcome. :-)

sahm

unread,
May 14, 2009, 4:01:03 AM5/14/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Benj Nunez

Good Day

I try to do that but some error show up
and this is my code

the child form
////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Archive_System
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private static new MainForm ParentForm = null;

public Login(MainForm APrent):this()
{
ParentForm = APrent;
}

private void OKbutton_Click(object sender, EventArgs e)
{
string un = userNametextBox.Text;
string ps = PasswardMaskedTextBox.Text;

if (un == "sahm" || ps == "java")
{
MessageBox.Show(this, "Login Work", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
ParentForm.menuStrip1.Enabled = !
ParentForm.menuStrip1.Enabled;


}
else
{
MessageBox.Show(this, "Login not work", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}

private void CancelButton_Click(object sender, EventArgs e)
{
this.Close();
}



}
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
the main form
//////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;


namespace Archive_System
{
public partial class Main : Form
{
public Main()
{
try
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Closing += new CancelEventHandler(Form_Closing);

this.menuStrip1.Enabled = false;
//this.Enabled = false;
checkUser();
}
catch (Exception exp)
{
string ms = exp.Message;
MessageBox.Show(this, ms, "System Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);

}
}

System.Data.SqlClient.SqlConnection con;

private void userToolStripMenuItem_Click(object sender,
EventArgs e)
{
try
{
createUser cu = new createUser();
cu.MdiParent = this;
cu.Show();


}
catch (Exception exp)
{
string ms = exp.Message;
MessageBox.Show(this, ms, "System Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void aboutToolStripMenuItem_Click(object sender,
EventArgs e)
{
About ab = new About();
ab.MdiParent = this;
ab.Show();
}
/*public void setPreviousForm(frmMain mainFrm)
{
prevForm = mainForm;
}*/
public void checkUser()
{
Login log = new Login();
//log.setPreviousForm(this);
log.ShowDialog(this);

}

private void Main_Load(object sender, EventArgs e)
{
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=mr-x\
\SQLExpress2008;Initial Catalog=archive;Integrated Security=True";
con.Open();

}

public void Form_Closing(object sender, CancelEventArgs cArgs)
{
con.Close();

}

private void menuStrip1_ItemClicked(object sender,
ToolStripItemClickedEventArgs e)
{

}


}
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

and this is the error message

///////////////////////////////////////////
Error 1 The type or namespace name 'MainForm' could not be found (are
you missing a using directive or an assembly reference?) E:\Projects\C#
\Archive System\Archive System\Login.cs 18 28 Archive System


Error 2 The type or namespace name 'MainForm' could not be found (are
you missing a using directive or an assembly reference?) E:\Projects\C#
\Archive System\Archive System\Login.cs 20 22 Archive System

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

thank you for your help
Bes
Salim

CallMeLaNN

unread,
May 15, 2009, 4:23:21 AM5/15/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting

Because you use:

Login log = new Login();

In MainForm but you declare:

public Login(MainForm APrent):this()

Which LoginForm need APrent value that is MainForm.
To solve change to this:

Login log = new Login(this);


But, I recommend u to use MdiParent instead.

1. Remove the "private static new MainForm ParentForm = null;" no need
to use this.
2. login constructor: let it default constructor (no parameter):
"public Login()"
3. To disable the menu use like this: (Im not sure your main form type
is Main or MainForm based on your code, I assume Main)

Main ParentForm = (Main) this.MdiParent
ParentForm.menuStrip1.Enabled = False

dont use ! for toggle it, it can be wrong toggle, instead, assign true
or false if you sure.

sahm

unread,
May 16, 2009, 3:45:04 AM5/16/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi CallMeLann

Thank for your help I'll try this

Best
Salim

sahm

unread,
May 17, 2009, 2:10:56 AM5/17/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi every One

Good Day

I'm still having problem with this
I write the code like this

////////////////////////////////////
Login log = new Login(this);
log.ShowDialog();
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

this error is showing
///////////////////////////////////////
Error 1 'Archive_System.Login' does not contain a constructor that
takes '1' arguments E:\Projects\C#\Archive System\Archive System
\MainForm.cs 68 25 Archive System

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

and if I writ is like this
//////////////////////////////
Login log = new Login();
log.ShowDialog(this);
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Run time error is showing after I Enter the user name and the password
///////////////////////////////////
Object reference not set to an instance of an object
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

and this is my code

MainForm
Login
/////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Archive_System
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
//private static new MainForm ParentForm = null;

/*public Login(MainForm APrent):this()
{
Main ParentForm =(Main) this.MdiParent;
}*/

private void OKbutton_Click(object sender, EventArgs e)
{
string un = userNametextBox.Text;
string ps = PasswardMaskedTextBox.Text;

if (un == "sahm" || ps == "java")
{
MessageBox.Show(this, "Login Work", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
//ParentForm.menuStrip1.Enabled = !
ParentForm.menuStrip1.Enabled;
Main ParentForm = (Main)this.MdiParent;
ParentForm.menuStrip1.Enabled = false;


}
else
{
MessageBox.Show(this, "Login not work", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}

private void CancelButton_Click(object sender, EventArgs e)
{
this.Close();
}



}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Thank every one

Best

Salim

CallMeLaNN

unread,
May 17, 2009, 9:28:58 PM5/17/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I thought you want to use MDI for login form.

If so, you need to add log.MdiParent = this; here:

Login log = new Login();
log.MdiParent = this;
log.Show();

Note: to make your login visible, place the checkUser(); not in
"public Main()" constructor but in Main_Load().

The "Object reference not set to an instance of an object" is because
of your
ParentForm or this.MdiParent is null at here:

Main ParentForm = (Main)this.MdiParent;

and sure will throw the error when you use it:
ParentForm.menuStrip1.Enabled = false;
> ...
>
> read more »

sahm

unread,
May 28, 2009, 5:04:31 AM5/28/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi every one

Good Day

I still get this error
/////////////////////////////////////
Object reference not set to an instance of an object
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Best

Salim
> ...
>
> read more »

Benj Nunez

unread,
May 28, 2009, 9:10:10 PM5/28/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
It means that either:

1) A class needs to be instantiated with "new", but was not done so.

or

2) A class has been instantiated BUT you assigned Nothing (VB) or null
(C#) to it,
and then you attempted to use the methods from it.

Kindly look at the code again.


Cheers!


Benj
> ...
>
> read more »

sahm

unread,
Jun 7, 2009, 10:35:24 AM6/7/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi every one

thank you for you help
I solve the problem it was this error

////////////////////////////////////
Login log = new Login();
log.Show;
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Best
Salim
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages