Hi,
I just want to enable menus of Menu Strip on MdiParent after Load Mdi Form,
using a child form button click event.
I disabled (Enable = false) those menus by default. And in case of Child
form's condition comes true, and then I've to turn on/Enable those menus.
Problem is that once MdiParent form is loaded, then I provide true
value/enablev = true to MdiParent's menus from Child's click event using
Mdi's Public method. In result; menus got true value but didn't implement or
can say Mdi didn't get update. I also used MdiPraent.Refresh() and Many other
things but it couldn't solve. I just want assistance in this issue!
Thanks
Pasan RatnayakePosted Sep 5, 2010, 1:13 PM
So on to your question, it is a basic rule in C# that any variable must be initialized prior using it. I.e. if you create a variable, you need to initialize it to a value prior using it anywhere in your code. Now in this case, I have initialized the "mdiParent" variable in to 'null' which means nothing. Just think logically what would happen if you call a method like "show()" on nothing ? Doesn't make any sense even right? So you need to initialize the variable with a new "MdiParent1" object.
Now think about two methods which use this "mdiParent" variable. In the method 1, you have the initialize variable code. In the method 2, you don't have it because, method 2 will never be called before method 1 according to the way you code. But think you decide to work on your code after 2 years and you accidentally call method 2 before calling method 1. Now when the method 2 is called, is the variable initialized ? No, right ? So that's why you always check whether a variable is null before using it.
Hope this clarifies your doubt.
Mohammad SaadPosted Sep 5, 2010, 5:15 PM
may God keeps you high. ameen Thank you boss :)
Mohammad SaadPosted Sep 5, 2010, 12:48 PM
hope will be with connected
Pasan RatnayakePosted Sep 5, 2010, 12:04 PM
Regarding that exception you are getting, I have created a class variable of the type "MdiParent1" (if you check the code of "Form1" you'll see that). The problem with class variables is that they might not be initialized to a proper value when used within some other part of the code. So always just check whether it is null at the beginning of every method which calls it and initialize it if it is null.
E.g.: if (this.mdiParent == null) this.mdiParent = new MdiParent1();
Hope this helps you.
Mohammad SaadPosted Sep 5, 2010, 11:14 AM
But I did it with a new Application it really works. I trace every possible mistake but didn't get yet. Can you help me in this error too......
You're right i'm new in programming n working on a assignment. Thanks
Pasan RatnayakePosted Sep 5, 2010, 10:35 AM
Your requirement is simple but don't take the simple facts for granted. Often they cause you rework. Personally, I hate it. So please make sure you don't do that again.
Anyways, I've added a sample of what you might want. Hope it helps
Mohammad SaadPosted Sep 3, 2010, 2:10 PM
Perhaps Child form has to open after the Parent. And follow the way I just want to refresh
Parent's Menu Strip / Menu Items using Child's button click event.
Thanks for your will. But get to the point then you'll be able to solve this query.
Thanks again
Pasan RatnayakePosted Sep 3, 2010, 12:24 PM
You are doing a very fundamental programming mistake.
In the code you have given you have created a "MdiParent" object but you have not opened that form. So how can you see whether your MDI form's menus are getting disabled/enabled?
Nevertheless, I've attached a simple code on how to use a MDI form and enable/disable menus.
Hope you would try learning the basic programming concepts first and hope this would help you.
Regards
Mohammad SaadPosted Sep 3, 2010, 11:40 AM
\\
namespace Testing
{
public partial class MDIParent : Form
{
public MDIParent()
{
InitializeComponent();
}
public void LoadMDIParent()
{
//menuStrip.Enable = false; (by default)
}
public void ShowItem()
{
menuStrip.Enable = true;
this.refresh();
}
}
}
//
namespace Testing
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (condition==true)
{
MDIParent mdi = new MDIParent();
mdi.ShowItem(); //Not get required result with it
this.close();
}
}
}
Pasan RatnayakePosted Sep 3, 2010, 7:03 AM