Application Windows Form C# use menu (User control) - c#

I have an application with 2 Forms, for those forms I have create a Menu which I depose on the two forms.
There is only a menuStrip item on the menu, I just want when I click on "test1" to redirect to Form1 and when I click to "test2" I want to redirect to Form2.
But if test1 is already open/display I don't want to show him again and the same for test2.
My code in my Menu :
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f2.Hide();
f1.Hide();
f1.ShowDialog();
}
private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f1.Hide();
f2.Hide();
f2.ShowDialog();
}
}
My Form1 :
My Form2 :
I just want the same result like my Buttons in Form1 and Form2 :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f1 = new Form1();
f1.ShowDialog();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
I thought that the property Visible for forms could help me but not...
The problem is when I click on my buttons it's open a new window but when my form is already open I don't want to open it again.
Thanks for your reply, I hope that I am clear sorry for my english in advance.

You are currently creating a new form each time the click handler code is executed.
Here is one way, but its nasty and I wouldn't really recommend it. I've assumed that form1 is the entry to your application and that its also the exit of the application. This solution uses a singleton to hold the f1/f2 instances.
public static class Global
{
static Global()
{
f2 = new Form2();
}
public static Form f1;
public static Form f2;
}
Your menu altered:
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Global.f2.Hide();
Global.f1.Hide();
Global.f1.Show();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
Global.f1.Hide();
Global.f2.Hide();
Global.f2.Show();
}
public void SetForm1(Form form)
{
Global.f1 = form;
}
public void SetForm2(Form form)
{
Global.f2 = form;
}
}
And the forms:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Global.f1 = this;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Global.f2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
Global.f1.Show();
}
}
Hope this helps.

Your logic is wrong.
It seems to me that you want to display different content depending on the user selection on that 'menuStrip'. You need to look at dynamic controls loading not different forms loading.
You can have just a MainForm with that 'menuStrip' and a Panel. Define some User Controls and dynamically add them to that panel based on the user selection.
Snippet
public MainForm : Form
{
public MainForm()
{
// code
}
public void MenuStrip_OptionSelected(object sender, EventArgs e)
{
Panel1.Controls.Clear();
switch(MenuStrip.SelectedValue)
{
case "UserControl1" : Panel1.Controls.Add(new UserControl1()); break;
...
}
}
}
public UserControl1 : UserControl
{
// code
}

Related

Changing TextBox Text of Form1 From Form2 in C#

I'm new in C# programming. I have a beginner level question:
How do I change the text property of the textbox1 in my form 2 object using a button in my form1?
Here's my code in form1:
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
This is in form2:
namespace DoubleForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textBox1.Text = "Test";
}
}
}
When you add a text box or any control for that matter to a Winform using the controls toolbox the control gets added as private so it can't be accessed outside of the class it's created in. Easy enough to fix though just added a public property that lets you get and set the text box value as such
namespace DoubleForms
{
public partial class Form1 : Form
{
// NEW CODE
public string TextBoxText
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
Then from Form2 you can just call form1.TextBoxText = "blah blah" to set the value.
Code is creating new Form1 every-time you click the button, which is not you want I believe.
What you need to do is create an event in Form2 and then subscribe to that event in Form1, that way you can listen changes from Form2 and update Form1.
namespace DoubleForms
{
public partial class Form2 : Form
{
public event EventHandler Updated; // define an event handler
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(Updated != null)
{
Updated(sender, new EventArgs()); //Raise a change.
}
}
}
}
Now in Form1 subscribe to Form2 event.
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Updated += (se,ev)=> textBox1.Text = "Test"; // update textbox
frm2.Show();
}
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}

C# - Setting a Value in a Button then passing it into a ListView

I have two forms, form1 and form2.
In form1, there is are two buttons, button1 and button2.
In form2, there is a listview, ListView1.
button1 should hold a string value called "Vanilla".
When button2 is pressed it opens form2.
On form2, in listview1 it should show "Vanilla" in the first column.
Form1
public partial class form1 : Form
{
public static string buttonValue = "";
public form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
buttonValue = "Vanilla";
}
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2();
form2.Show();
this.Hide();
}
Form2
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can design the second form as bellow:
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private string _passedValue = "";
public form2(string passedValue)
{
InitializeComponent();
_passedValue = passedValue;
listView1.Items.Add(_passedValue);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can pass the value stored in the first button using the bellow code.
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2(buttonValue);
form2.Show();
this.Hide();
}

How to clear dataGridView rows from another form?

I have two forms Form1 and Form2, Form 1 with a dataGridView and a button where Form2 has one button.
When i click the button1 in Form1, it will open Form2 (overlapping).
Now i need to clear the values of the datagridview in Form1 when i press the
button in Form2 also the same button click event should close the
Form2.
Any idea how to do it?
Any help is appreciated
This is Form 2:
private DataGridView _dgv;
public Form2(DataGridView dgv)
{
_dgv = dgv;
InitializeComponent();
}
private void buttonClearRows_Click(object sender, EventArgs e)
{
_dgv.Rows.Clear();
Close();
}
This is Form1:
private void buttonOpenForm2_Click(object sender, EventArgs e)
{
new frm2(dataGridView1).ShowDialog();
}
You need to pass a reference to Form1 into Form2, either by consctructor or by a property. Sample uses constructor. Change the names of the controls to the ones you have. Consider this as a pseudo code sample.
Form1 (some logic removed for brewity)
public class Form1
{
...
public void Clear()
{
DataGridView1.Rows.Clear();
}
public void btnOpenForm2_Click(object sender, EventArgs e)
{
var form2 = new Form2(this); // create a new form2, and pass a reference to form1
form2.Show(); // show the form.
}
...
}
Form2 (some logic removed for brewity)
public class Form2
{
private Form1 _parent; // this will hold the parent until Form2 is disposed.
...
public void Form2(Form1 parent)
{
_parent = parent; // assign Form1 instance to a field.
}
public void btnClearGrid(object sender, EventArgs e)
{
_parent.Clear(); // clear the rows in the datagridview instance within form1.
}
...
}
If I understand what you're asking, I strongly suggest to hide access to DataGridView in Form1 from outside the class, to avoid unexpected behavior in future.
You could instead add a function in Form1 and manage button1 click event in this way:
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
dialog.ShowDialog(this);
}
public void ClearRows() { dataGridView1.Rows.Clear(); }
}
Then in Form2 you can easily handle click on button in this way:
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
((Form1)this.Owner).ClearRows();
this.Close();
}
}
It's very simply, in the form1 you can create an instance of Form2 and after the show you can set the reference of dataGridView in child form2.
Fundamental is who you set the dataGridProperty modifier=public(visual property F4) in the form1
This is Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.dataGridViewPassed = this.dataGridView1;
f.ShowDialog();
}
}
This is Form 2:
public partial class Form2 : Form
{
public DataGridView dataGridViewPassed = null;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridViewPassed.Rows.Clear();
}
}

How to reLoad a form from another form

I have two forms, Form1 and Form2. I use ShowDialog() on Form2 from Form1. How can I run Form1Load() from Form2? Specifically, I want to refresh Form1 from Form2.
Firstly, make sure you assigning the Owner property on Form2 before showing it. This allows you to access the current instance of Form1.
class Form1 : Form
{
public void Method()
{
var form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
}
}
From Form2 you can this use this.Owner to access the instance of Form1 and call any public methods or access any public properties. (Make sure the load event handler is public)
class Form2 : Form
{
public void Method()
{
this.Owner.form1_Load(null,null); //assuming you don't use these params.
}
}
In your form1_Load() I would recommend putting in a this.Refresh() to "refresh" the form. The refresh doesn't update some things that have data stored, it only repaints the form, so in the load event you will have to manually "refresh" things.
Create an instance of Form1 and then use the Refresh method or your Form1_Load method with that instance.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ShowMessage(string message)
{
MessageLabel.Text = message;
}
private void ShowForm2(object sender, EventArgs e)
{
Form2 Form2Copy = new Form2(this);
Form2Copy.ShowDialog();
}
}
and
public partial class Form2 : Form
{
Form1 Form1Copy;
public Form2(Form1 Parent)
{
InitializeComponent();
Form1Copy = Parent;
}
public void Button_Click(Object sender, EventArgs e)
{
Form1Copy.ShowMessage("Hello from Form2!");
}
}
Pass in Form1 to the ShowDialog() method of your Form2 instance:
private void Form1_Load(object sender, EventArgs e)
{
this.LoadEventCode();
}
public void LoadEventCode()
{
this.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass in Form1
}
Now over in Form2, cast the .Owner property to Form1 and do what you need:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
f1.LoadEventCode();
}

How to enable button in Form 1 from Form 2 using public get set?

I have two buttons in Form 1, one is "ShowForm2" button and one is "button1" button.
Button 1 is disable by default. And when I click "ShowForm2" button, Form 2 will show.
So, what I want is, when I click the "button2" in form 2, it will enable the "button1" in form 1.
So, I try to code like this in my form2 class:
public partial class Form2 : Form
{
bool enable_form1_button1;
public bool Enable_form1_button1
{
get { return this.enable_form1_button1; }
set { this.enable_form1_button1 = value; }
}
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
enable_form1_button1 = true;
}
}
Then in my Form1 class, I am expecting to get the "enable_form1_button1 = true" to pass into form 1 and enable my form 1 button1. But how to do this?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btb_Showfrm2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
button1.Enabled = frm2.Enable_form1_button1; // I put it here, and it just does not seems right
}
}
Well what you can do is, expose the button as a property and send a reference of your current form to the form that needs to take control:
Form1
public partial class Form1 : Form
{
public Button BtnShowForm2
{
get { return btnShowForm2; }
set { btnShowForm2 = value; }
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
// pass the current form to form2
Form2 form = new Form2(this);
form.Show();
}
}
Then in Form2:
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
private void btnEnabler_Click(object sender, EventArgs e)
{
// access the exposed property here <-- in this case disable it
_form1.BtnShowForm2.Enabled = false;
}
}
I hope this is what you're looking for.
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
Form2 oFrm2 = new Form2();
oFrm2.evtFrm += new ShowFrm(oFrm2_evtFrm);
oFrm2.Show();
}
void oFrm2_evtFrm()
{
button1.Enabled = true;
}
}
Form2.cs
public delegate void ShowFrm();
public partial class Form2 : Form
{
public event ShowFrm evtFrm;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (evtFrm != null)
{
evtFrm();
}
}
}
you can send a refrence of your form1 to your form2 in the from2 constructor for example :
and then in button clicked event handler in form2 call a public method in form1 to enable the button :
this could be your form1 code :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public void enableButton()
{
button2.Enabled = true;
}
this could be your form2 code :
public partial class Form2 : Form
{
private Form1 frm;
public Form2(Form1 frm)
{
InitializeComponent();
this.frm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
frm.enableButton();
}
If you'll only have one instance of Form1 you can also get away with having a static Button member in Program which references the button you want to enable from Form2.
Form1.cs:
using System;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button2.Enabled = false;
Program.button = button2;
}
private void button1_Click(object sender, EventArgs e)
{ new Form2().Show(); }
private void button2_Click(object sender, EventArgs e) { }
}
}
Form2.cs:
using System;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
public partial class Form2 : Form
{
public Form2()
{ InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{ Program.button.Enabled = true; }
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
static class Program
{
public static Button button;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
I would recommend you to use event/delegates to perform this task. This is the standard way of doing so.
Again passing reference in constructor or making static func/var/properties are just workaround and Called as bad programming.

Categories

Resources