Reputation: 81
So I'm working on a ordering system, when order gets placed from form2, to show a panel with message on form1.. but It doesnt seem to work, please help
int chkComm1 = comm1.ExecuteNonQuery();
if(chkComm1 == 1)
{
//so basically if query call is successful (which it is) it should open a panel with message on my main program or form1, but It doesnt work? anyone help?
Main_Program mp = new Main_Program();
mp.orderALERT.Enabled = true;
mp.orderALERT.Visible = true;
mp.Refresh();
}
After this code it should show panel but nothing happens, if someone can help me please do! Thanks !
Upvotes: 0
Views: 939
Reputation: 39152
Pass in your instance of Main_Program to the Show()
method of Form2 like this:
public partial class Main_Program : Form
{
public Main_Program()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show(this); // pass in THIS instance of Main_Program as the "Owner"
}
}
Now, over in Form2, you can cast the Owner
back to Main_Program and manipulate. To access your "orderALERT" Panel, however, you must change its Modifiers
property to public:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.Owner != null && this.Owner is Main_Program)
{
Main_Program mp = (Main_Program)this.Owner;
mp.orderALERT.Enabled = true;
mp.orderALERT.Visible = true;
mp.orderALERT.Refresh();
}
}
}
Upvotes: 0
Reputation: 236318
Problem: Your code is not working, because you create new instance of form1 which is not related to main form which you already have in your application:
Main_Program mp = new Main_Program(); // here
Solution: You should use form1 which is already opened. That is easy to do with events. Create an event on form2:
public event EventHandler OrderPlaced;
And raise it when order is placed:
int chkComm1 = comm1.ExecuteNonQuery();
if(chkComm1 == 1)
{
OrderPlaced?.Invoke(this, EventArgs.Empty);
}
On form1 subscribe to event of form2:
form2.OrderPlaced += Form2_OrderPlaced;
And in event handler activate panel:
private void Form2_OrderPlaced(object sender, EventArgs e)
{
orderALERT.Enabled = true;
orderALERT.Visible = true;
}
Upvotes: 1