Connecting Tech Pros Worldwide Help | Site Map

How to dynamically control mdi menu c#.Net

Newbie
 
Join Date: Oct 2009
Posts: 4
#1: Oct 1 '09
Hi Dear All,

I create a window application in c#.net. I want to hide menu options when particular user login for example: in mdi menu there is Report menu there is report1 and report2 now when user1 login I want to show all option report1 and report2 but when user2 login I want to show only report1.

In vb6 it is very essay but because I am new in c#.net I couldn’t find the solution.
Please help me to find the solution.

Thanks and Regards
Prakash
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#2: Oct 1 '09

re: How to dynamically control mdi menu c#.Net


You should just be able to set the tool strip item's visible property to false. I just tested it and it appears to work.
Newbie
 
Join Date: Oct 2009
Posts: 4
#3: Oct 1 '09

re: How to dynamically control mdi menu c#.Net


fine here is the code

foreach (ToolStripMenuItem tsmi in menuStrip.Items)
{
if (tsmi.Text == "Security")
{
/// some code
}
}

in mdi load i call user login form, now as per user permission i want to hide/unhide the menu options

problem
1. this code is work f9 when i put this in mdi load but in user form it will return null object reference.
2. it only return the mater menu like only file option not files child node

please help or provide some code

problem
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#4: Oct 1 '09

re: How to dynamically control mdi menu c#.Net


I don't understand what the problem is... is it that you can't hide/show menu selections, or that you're getting a null reference exception when you run?
Newbie
 
Join Date: Oct 2009
Posts: 4
#5: Oct 2 '09

re: How to dynamically control mdi menu c#.Net


I am getting null reference
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#6: Oct 2 '09

re: How to dynamically control mdi menu c#.Net


Where are you hitting the null reference? If you're using Visual Studio, you should be able to run it in debug mode (F5) and it will break where the exception is thrown.

From the code you posted, the only place I can see a null reference occurring is menuStrip, but you've given no context so I can't tell why it would be null.

You mentioned that it doesn't work when you put it in the user form... I don't know what that is with respect to your code, but make sure it knows what menuStrip is, or has it initialized.
Newbie
 
Join Date: Oct 2009
Posts: 4
#7: Oct 2 '09

re: How to dynamically control mdi menu c#.Net


/// Application start from here
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new MDIColts ());
}

/// MDI load code having no problem
/// menuStrip is private System.Windows.Forms.MenuStrip menuStrip;

private void MDIColts_Load(object sender, EventArgs e)
{
foreach (ToolStripMenuItem tsmi in menuStrip.Items)
{
if (tsmi.Text == "Security" || tsmi.Text == "Master" || tsmi.Text == "Transactions" || tsmi.Text == "Reports")
{
getChildNodes(tsmi);

}
}

MasterUI.frmLogin.GetInstance.ShowDialog();
}

///the time of MDI load I call MasterUI.frmLogin.GetInstance.ShowDialog();
/// and in the click of user login
private void btLogin_Click(object sender, EventArgs e)
{
try
{
if (UserLogIn.bActive == false)
{
MessageBox.Show(MessageResource.NotActive, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtUserId.Text = "";
txtPassword.Text = "";
txtUserId.Focus();
}
else
{
/// code where collecting data from database , security table and as per permissions want to show mdi menu items
///menuStrip.Items null ref problem
foreach (ToolStripMenuItem tsmi in menuStrip.Items)
{
if (tsmi.Text == "Security" || tsmi.Text == "Master" || tsmi.Text == "Transactions" || tsmi.Text == "Reports")
{

}
}


if you have any good suggestion or help please provide me
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#8: Oct 2 '09

re: How to dynamically control mdi menu c#.Net


In the future, can you please use code tags when you post? It makes it a lot easier to read. You can find some information here: http://bytes.com/misc.php?do=bbcode

Anyway, so the btLogin_Click method is in the user form, right? It looks like you're trying to access the main form's menuStrip from the child form... you shouldn't even be able to see that because it's private.

I realize this might be something to do with MDI (which I don't have any experience with), but I'll still try to help you out if I can.

Basically, how are you accessing menuStrip from the user form when it actually belongs to the main form? Is there some way to access the user form's parent so you can get a hold of the parent's menu strip?

That said, a better approach might be to have the user form return a login status and let the parent form show or hide it's own menu items. It would better avoid confusion like this.
Reply