ok, I have 2 forms...On f1 is a flowlayoutPanel and a button that opens f2.
On f2 there are small panels, each is a diffrent color.
I want to do this:when I click on a panel from f2 a panel is created in FLP in f1 that has the same color and size. The problem is that when I click on the first panel on f2 nothing happens.
this is what I have so far:
f1
private void Add_Color_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
f2
Form1 f1 = new Form1();
private void panel1_Click(object sender, EventArgs e)
{
Panel pnl = new Panel();
pnl.BackColor = panel1.BackColor;
pnl.Size = panel1.Size;
f1.BackColor = panel1.BackColor;
f1.FLPMain.Controls.Add(pnl);
this.Close();
}
So your child form shouldn't need to know a thing about your first form. It sounds like you're creating something like a generic color picker tool. You should be able to use that same form somewhere else in your application where you need to pick a color as well, for example.
As a general rule it's best if a child form doesn't "know" about it's parent, it keep them decoupled, makes it easier to write each class separately without forcing the developer to be so knowledgeable about the other types in the project. It's actually not terribly hard.
So rather than having Form2 go and add a panel to Form1, it can just notify Form1 when it's chosen a color and a size. This is done through an event:
public class Form2 : Form
{
//define the event
public event Action<Color, Size> ColorChosen;
private void panel1_Click(object sender, EventArgs e)
{
//raise the event
var panel = (Panel)sender;
ColorChosen(panel.BackColor, panel.Size);
Close();
}
}
(Size note; by using sender here this same event handler can be added to all of the panels you want to be clickable, rather than having a ton of event handlers that do almost the same thing.)
Then on Form1 we just assign an event handler to this custom event where we create and add a new panel to the form:
Form2 child = new Form2();
child.ColorChosen += (color, size) =>
{
Panel panel = new Panel();
panel.BackColor = color;
panel.Size = size;
Controls.Add(panel);
};
child.Show();
You're creating a new instance of Form1 here:
Form1 f1 = new Form1();
But you want to add your panels on the existing one, so use:
Form f1 = Application.OpenForms['formname'];
Just do it this way
private void Add_Color_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
class Form2
{
private Form1 _frm;
public Form2(Form1 frm)
{
//initialize + other code if required
_frm = frm;
}
private void Panel_Click(object sender, EventArgs e)
{
_frm.CreatePanel(/*param you need*/); //name it what ever you want
}
}
bassicaly something like that should do the job
/e there might be some typos but the idea is there
Related
There are 2 different forms.
Form1 and Form2
Form1 has 6 different checkBoxes and one button.
If you press the button, form2 opens and form1 gets closed and and the special buttons get visible, which you checked in the checkboxes before.
There are 6 buttons on form2 but all are set as visible = false.
For example: CheckBox 2 and 6 are checked and you press the button.
Form2 opens and Button 2 and 6 are visible.
But I cant use the CheckBoxes and Buttons from the different forms. I already set the Checkboxes and Buttons as public in the designer.
Can anyone help? Sorry for my bad english, thanks in advance!
Add this EventHandler to the .Click event of the button on Form1:
//Click EventHandler for Button on Form1
private void button1_Click(object sender, EventArgs e)
{
//Create instance of Form2
Form2 f2 = new Form2();
//Check which CheckBoxes are checked
foreach(CheckBox checkbox in this.Controls.OfType<CheckBox>())
{
if (checkbox.Checked)
{
//Get number of Checkbox
string number = checkbox.Name.Replace("checkBox", string.Empty);
//Show the corresponding Button on Form2
Button button = f2.Controls.Find("button" + number, true).FirstOrDefault() as Button;
button.Visible = true;
}
}
//Show Form2
f2.Show();
//Close or hide Form1
this.Hide();
}
Note: Keep in mind that for this to work your CheckBoxes on Form1 have to be called:
checkBox1, checkBox2, checkBox3, ...
and the Buttons on Form2 have to be called:
button1, button2, button3, ...
you could also add or remove Buttons and CheckBoxes at any point. Just make sure they're named correctly.
Unrelated Note: Please take a look at this before you ask your next question. And welcome to SO! :)
I already set the Checkboxes and Buttons as public in the designer.
I assume this means you changed the Modifiers property of the Buttons on Form2 to Public?
If so, then when you create Form2 from Form1, simply do:
// ... from within Form1 ...
Form2 f2 = new Form2();
f2.button1.Visible = this.checkBox1.Checked;
f2.button2.Visible = this.checkBox2.Checked;
f2.button3.Visible = this.checkBox3.Checked;
f2.button4.Visible = this.checkBox4.Checked;
f2.button5.Visible = this.checkBox5.Checked;
f2.button6.Visible = this.checkBox6.Checked;
f2.Show();
As Klemikaze mentioned in the comments, a more correct approach would be to pass the states to an overloaded Form2 constructor:
// ... in Form1 ...
private void button1_Click(object sender, EventArgs e)
{
bool[] checkStates = new bool[] {
checkBox1.Checked, checkBox2.Checked, checkBox3.Checked,
checkBox4.Checked, checkBox5.Checked, checkBox6.Checked
};
Form2 f2 = new Form2(checkStates);
f2.Show();
}
Then in Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(bool[] btnVisibleStates)
{
InitializeComponent();
for(int i=0; i<btnVisibleStates.Length; i++)
{
String btnName = "button" + (i + 1);
Button btn = this.Controls.Find(btnName, true).FirstOrDefault() as Button;
if (btn != null)
{
btn.Visible = btnVisibleStates[i];
}
}
}
}
I have two forms. First one has one textbox.Second one has a devexpress data grid.
I want to achieve that:
first click a button and form2 opens.
if I click a row in the data grid in form2, this value should be shown inside the textbox in form1.(form1 is already opened.)
ı m a beginner. thanks for your help.
Form1 frm1 = new Form1();
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
frm1.Show();
when I do that, a new form opens. I dont want to open a new form. Form1 is already opened. I want to add values to its textbox.
Pass form1 as a reference to form2:
In your button click handler on form 1 to open form 2
private void Button_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
form2 code
private Form1 frm1;
// constructor (pass frm1 as reference)
public Form2(Form1 frm1) {
this.frm1 = frm1;
}
//put this in your event handler for a row click in the grid
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
you can try something like this.
When you click on frm1.SimpleButton this function will be called a show your Form2. On Form2 you have to set InnerProperty (from your datagrid?) a when you close Form2 you can use this property.
private void SimpleButton_Click(object sender, EventArgs e)
{
using (Form2 frm = new Form2())
{
frm.InnerProperty = "default text";
DialogResult result = frm.ShowDialog();
SimpleButton.Text = frm.InnerProperty;
}
}
Finally, I solved the problem.
Application.OpenForms["form1"].Controls["textBox1"].Text =
gridView1.GetFocusedRowCellValue("ID").ToString();
Thanks for help.
I am wondering how can I correctly switch between forms by button click event.
I have Form1 and Form2.
Form1 have: -TextBoxForm1
-ButtonForm1
Form2 have: -TextBoxForm2
-ButtonForm2
I would like to on_click ButtonForm1 event go to the Form2. Then I want to write some message to TextBoxForm2 and press ButtonForm2 it will go to the Form1 again and message from TextBoxForm2 will appear in TextBoxForm1.
Everything works fine, but I have one problem. When I close application and I wanna debug and start it again, some errors appear like:"application is already running".
Form1:
public static string MSG;
public Form1()
{
InitializeComponent();
TextBoxForm1.Text = MSG;
}
private void ButtonForm1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Hide();
//There is probably my fault but when I was trying this.Close(); everything shutted down
form2.Show();
}
Form2:
private void ButtonForm2_Click(object sender, EventArgs e)
{
Form1.MSG = TextBoxForm2.Text;
Form1 form= new Form1();
form.Show();
this.Close();
}
How can I do this correctly please? :) I am beginner, thank you!
I would not go the route of using a STATIC for passing between forms as you mention you are a beginner, but lets get the closing to work for you.
In your main form create a new method to handle an event call as Hans mentioned in the comment. Then, once you create your second form, attach to its closing event to force form 1 to just become visible again.
// Inside your Form1's class..
void ReShowThisForm( object sender, CancelEventArgs e)
{
// since this will be done AFTER the 2nd form's click event, we can pull it
// into your form1's still active textbox control without recreating the form
TextBoxForm1.Text = MSG;
this.Show();
}
and where you are creating form2
private void ButtonForm1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Closing += ReShowThisForm;
this.Hide();
form2.Show();
}
and in your second form click, you should only need to set the static field and close the form
private void ButtonForm2_Click(object sender, EventArgs e)
{
Form1.MSG = TextBoxForm2.Text;
this.Close();
}
Easy solution is to use modal form. Display Form2 temporarily, when it is displayed Form1 is invisible.
var form2 = new Form2();
this.Visible = false; // or Hide();
form2.ShowDialog(this);
this.Visible = true;
And to pass data you can define property in Form2, to example:
public string SomeData {get; set;}
Form1 has to set SomeData, which you take in Shown and display. Same property can be used to get data from Form2 (before closing).
// form 1 click
var form2 = new Form2() { SomeData = TextBoxForm1.Text; }
this.Visible = false;
form2.ShowDialog(this);
this.Visible = true;
TextBoxForm1.Text = form2.SomeData;
// form 2 shown
TextBoxForm2.Text = SomeData;
// form 2 click
SomeData = TextBoxForm2.Text;
Close();
I am working with windows form ASP.net C#
I have panel1 in which I am opening a form named form1.
form1 has a button. On this button clicke I want to close the form1 and open the new form named form2 in the same panel1
I have opened the form1 in panel1 as follows
Form1 frm = new Form1();
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
panel1.Controls.Clear();
panel1.Controls.Add(frm);
frm.Visible = true;
Please help
First - I suggest you to use User Controls, which are supposed to be used as reusable containers of other controls. Forms are supposed to represent windows. So, usage of form as a controls container hosted on another window is not very good idea. If you need to have ability to show same data both in your panel and in separate window, then use same user control both on your form with panel and on Form1.
So, with user control. Adding them to panel is really simple:
UserControl1 control1 = new UserControl1();
control1.Dock = DockStyle.Fill;
control1.SomethingHappened += UserControl1_SomethingHappened; // see below
panel1.Controls.Clear();
panel1.Controls.Add(control1);
To have ability to switch user controls you can add event to UserControl1 and raise it when button is clicked:
// UserControl1 code
public event EventHandler SomethingHappened;
private void Button1_Click(object sender, EventArgs e)
{
if (SomethingHappened != null) // notify listeners, if any
SomethingHappened(this, EventArgs.Empty);
}
Then handle this event on your main form:
// MainForm code
private void UserControl1_SomethingHappened(object sender, EventArgs e)
{
UserControl1 control1 = (UserControl1)sender;
sender.SomethingHappened -= UserControl1_SomethingHappened;
UserControl2 control2 = new UserControl2();
control2.Dock = DockStyle.Fill;
panel1.Controls.Clear();
panel1.Controls.Add(control2);
}
Methods opening forms :
form1 --> form2 --> form3
ChecklistBox on the form1 there. How to know the form3 That is active or not?
If the forms you are referencing are MDI child forms, you could use
Form activeChild = this.ActiveMdiChild;
else you could use the following code if not using MDI child forms.
Form currentForm = Form.ActiveForm;
I understand that you are asking if form 3 is opened. If that is incorrect, please enlighten me.
There are probably dozens of ways to do so, it all depends on what you want to do.
One simple way would be to leave a flag somewhere, say in your Program.cs file:
public static bool Form3IsOpen = false;
Then:
private void Form3_Load(sender object, EventArgs e)
{
Program.Form3IsOpen = true;
}
And:
private void Form3_Close(sender object, EventArgs e)
{
Program.Form3IsOpen = false;
}
Supplemental:
You can also keep a reference to your subform:
In form1.cs:
private Form2 FormChild;
//In the function that opens the Form2:
FormChild = new Form2();
FormChild.Show();
Form2 will have something similar to retain Form3. If one form can open several, just use an array or collection.
When i usually have many different forms and only one instance to be created, i put them in dictonary and check it if there is a form.
Something like this:
public static Dictonary<string, Form> act_forms_in_app = new Dictonary<string, Form>();
now in every forms creation i do it like this
Form1 frm = new Form1();
frm.Name = "Myformname"
//set its properties etc.
frm.Load => (s,ev) { act_forms_in_app.Add(frm.Name, frm);};
frm.Load += new EventHandler(frm_Load);
frm.Disposed => (s, ev) { act_forms_in_app.Remove(frm.Name)};
//your usual form load event handler
public void frm_Load(object sender, EventArguments e)
{
...
}
somewhere where you want to check
Form frm = //Your form object
if(act_forms_in_app.ContainsKey(frm.Name))
{
//Perform as required
}