I'm trying to open a Panel that is in the main form (form1) from a button that is inside a UserControl , but the code runs but does not enable the panel of the main form
Can you help me?
//UserControl code
private void BtnChangeStatusOrder_Click(object sender, EventArgs e)
{
Button seta = (Button)sender;
var form = new Form1();
form.EnabledPanel1(seta.Tag.ToString());
}
//main form code
public void EnabledPanel(string order)
{
panel1.Visible = true;
}
Assuming the UserControl is also contained by Form1, then you can use TopLevelControl to get a reference to the Form:
private void BtnChangeStatusOrder_Click(object sender, EventArgs e)
{
Button seta = (Button)sender;
Form1 f1 = (Form1)this.TopLevelControl;
f1.EnabledPanel1(seta.Tag.ToString());
}
*Why does EnabledPanel() receive a string?
Related
i have a binded textbox control (txtMtrlGrpNum) on form1, i can change text property from the same form:
// in frm1
public frm1()
{
InitializeComponent();
Da = new SqlDataAdapter("select * from mtrlGrp", cn);
Da.Fill(Dt);
txtMtrlGrpNum.DataBindings.Add("Text", Dt, "GrpNum");
matGrpCManager = (CurrencyManager)this.BindingContext[Dt];
}
public void btn_add1_Click(object sender, EventArgs e)
{
if (matGrpCManager.Position >= 0)
{
matGrpCManager.AddNew();
txtMtrlGrpNum.Text = "123";
}
}
but when i want to execute button click from another form (Form2) like this:
// in frm2
private void btn_add2_Click(object sender, EventArgs e)
{
frm1 = new Form1();
frm1.btn_add1_Click(sender, e);
frm1.ShowDialog();
{
its not changed, its shown empty (""), cause i am using databinding, how to remove binding temporary and re apply it again,... any idea?
Here's a short example describing the logic.
In the first form, I created the handler for the button click event. I also added a button to open the second form. When the second form is created, I set the reference to the first form.
public void button1_Click(object sender, EventArgs e) // set public // btn_add1_Click
{
textBox1.Text = "Clicked!"; // set text in text box
}
private void btnOpenForm_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(); // create second form
frm2.frm1 = this; // set reference to this form
frm2.Show(); // show second form
}
In the second form, add a reference to the first form along with a button to trigger the form1 button handler.
public Form1 frm1 = null; // reference to first form
private void button1_Click(object sender, EventArgs e)
{
frm1.button1_Click(sender, e); // call handler in first form
}
After the second form is launched, either button will trigger the form1 handler:
Im trying to add data to a listview from another form. I have tired doing it from the same form and that works like it should, but when I try to pass the info between the forms no data gets entered.
Form 1 Contains listview
private void button3_Click(object sender, EventArgs e)
{
newTask form = new newTask();
form.Show();
}
Form 2 Contains form to submit info to listview
public void button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
string size = textBox2.Text;
Form1 table = new Form1();
table.listView1.Items.Add(url);
table.listView1.Items.Add(size);
this.Hide();
}
With Form1 table = new Form1(); you are creating new object type Form1 and it doesn't reference to your old form.
What you need to do is change constructor of second form to
private Form1 myFirstForm;
public newTask(Form1 form1)
{
InitializeComponent();
myFirstForm = form1;
}
and your second part of code should then look like this:
public void button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
string size = textBox2.Text;
myFirstForm.listView1.Items.Add(url);
myFirstForm.listView1.Items.Add(size);
this.Hide();
}
My mainForm contains two buttons(btnLoad & btnChange) and a panel
When the btnLoad is clicked, it loads the other forms(there are 5 different froms with different controlers) into the panel. Let me assume one of it named Form2 which contains a label(labelMessage)
My problem is, when I click the btnChange the following statement won't work.
f2.labelMessage.Text = "Button Change Clicked";
My codes are
// codes on mainFrom
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = new From2();
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
private void btnLoad_Click(object sender, EventArgs e)
{
panelDock.Controls.Clear();
Form f2 = new Form2();
f2.TopLevel = false;
panelDock.Controls.Add(f2);
f2.Show();
}
is this wrong?
Since Form2 is already shown you should use Application.OpenForms instead of creating a new instance of Form2
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)Application.OpenForms["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
From your comment that Form2 is in a panel you can try
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)panel1.Controls["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
I use MDI in MainForm.cs
public Le_MainForm()
{
InitializeComponent();
this.IsMdiContainer = true;
this.Name = "MainUSER";
}
private void barButtonItem_ListeOrdres_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.MdiParent = this;
f_Liste.Show();
}
private void barButtonItem_CreatOrdreAller_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Program.AllerRetour = "Ordre Aller";
Fiche_Ordre f_Fiche = new Fiche_Ordre();
f_Fiche.MdiParent = this;
f_Fiche.Show();
}
the question now, when I am in other form Ex.:Liste_Ordres.cs or Fiche_Ordre.cs how can i redirect from Fiche_Ordre.cs into Liste_Ordres.cs and vice versa without loosing MDI ?
When I'm in Fiche_Ordres.cs to go to Liste_Ordres.cs I use:
private void simpleButton_Annluer_Click_1(object sender, EventArgs e)
{
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.Show();
this.Close();
}
But as you can see I lose the MDI, that mean when I click the menu on MainForm, the Liste_Ordres form will disappear.
as you can see in this Video that when i redirect From Liste to fiche, and then maximize the window i lose the menu that mean i lose Mdi.
You just need to set the MdiParent again:
f_Liste.MdiParent = this.MdiParent;
Here is a small code that will illustrate my problem:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Interval = 3000;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
using (Form form = new Form())
{
form.ShowInTaskbar = false;
form.ShowDialog();
}
}
}
If I press button1 (which calls button1_Click) and then click on another application in the taskbar so that it comes to the top, and then after 5 secs I go back to my application, the created form won't be visible and I won't have a way to bring it back to the top, while my Form1 will be unresponsive because of having an invisible dialog on top.
What's a workaround for this?
Make your main form an owner of your modal box.
Form form = new Form();
form.Owner = this;
form.ShowInTaskbar = false;
form.ShowDialog();