I was not able to locate an answer on stackoverflow so here it goes. I am attempting to change the text of a MenuStrip subitem when clicking a button on a sub form. Below is the code from my Submit button on my sub form. when clicked it should change the text of “Log In” to “Log Out”. Code seems fine and no errors but does not update the text.
public AccessForm()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if (txtUser.Text == "admin" && txtPass.Text == "1234")
{
MessageBox.Show("Access granted.", "Access");
playgroundPlannersForm mainForm = new playgroundPlannersForm();
mainForm.logInToolStripMenuItem.Text = "Log Out";
this.Close();
}
else
{
MessageBox.Show("Incorrect Username or Password.", "Warning");
txtUser.Clear();
txtPass.Clear();
txtUser.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show("Message: " + ex, "Error");
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
You’re creating a new instance of the main form and changing that; you need to be passing along the reference to the original form and using that to update it.
Here’s one way to do it. In your sub form.. add this property:
..then, in your code above, use this:
In your main form, before you show your subform.. do this:
That sets the parent to the form that’s creating it (which, according to your code, is the correct form). You may also need to go into your form designer code and make the loginToolStripMenuItem public (if it isn’t already).