A new form in a main form - c#

in C# forms I need code to add a second form to my existing. this is what I've tried:
First form:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
frmMain fM = new frmMain();
fM.KeyPress += new KeyPressEventHandler(MMForm);
}
private void MMForm(object sender, KeyPressEventArgs e)
{
Keys KP; KP = (Keys)sender;
if (KP == Keys.Escape) { frm2 fM2 = new frm2(); fM2.Show(); }
}
}
And this is Second form:
public class frm2 : Form
{
public frm2()
{
frm2 fM2 = new frm2();
fM2.Height = 200; fM2.Width = 200;
Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
}
}
What am I missing?
EDIT: forget all this for a moment. Even if I do it as suggested down there I get an error when I press the key.
An unhandled exception of type 'System.InvalidCastException' occurred in Project 09.exe
Additional information: Specified cast is not valid.

You could do this:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
this.private void MMForm(object sender, KeyPressEventArgs e)
}
private void MMForm(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(((int)Keys.Escape)))
{
frm2 fM2 = new frm2(); fm2.Height=200; fm2.Width=200; fM2.Show();
}
}
public class frm2 : Form
{
public frm2()
{
InitializeComponent();
}
}

frm2 does not use InitializeComponent() command. so add it to your code.
secondly you try to add frm2 object to itself, so it will not work.
you should use the code belove for your exiting form (and set please its weight from properties, if you will not resize the form.
public class frm2 : Form
{
public frm2()
{
InitializeComponent(); ,
this.Width = 200; this.Height = 200;
}
}
And after a special key if you want to display frm2 :
frm2 secondFrom = new frm2();
frm2.Show(); // frm2.ShowDialog(); works too but they are working differently.

private void frmMain_Load(object sender, EventArgs e)
{
frmMain fM = new frmMain();
fM.KeyPress += new KeyPressEventHandler(MMForm);
}
Replace with this:
private void frmMain_Load(object sender, EventArgs e)
{
this.KeyPress += new KeyPressEventHandler(MMForm);
}
Or you can just register to your own KeyPress via designer, directly to MMForm...
And also, it is unclear what you are trying to do here:
public frm2()
{
frm2 fM2 = new frm2();
fM2.Height = 200; fM2.Width = 200;
Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
}
It should probably look more like this:
public frm2()
{
InitializeComponents();
this.Height = 200;
this.Width = 200;
}
Even if you don't want to InitializeComponents, you should edit your own (this) properties, not a new frm2 properties.
You've had the same problem in frmMain_Load, when you created a new frmMain, and subscribed to it's KeyPress, when really you should've subscribed to your own KeyPress.
Also, you can shorten your MMForm just to beautify, like so:
private void MMForm(object sender, KeyPressEventArgs e)
{
if ((Keys)sender == Keys.Escape)
{
new frm2().Show();
}
}

If you are trying to open frm2 when escape key is pressed on the main form do the following:
public frmMain()
{
InitializeComponent();
this.KeyPress += new KeyPressEventHandler(MMForm);
}
//You don't need to put anything in form load
private void frmMain_Load(object sender, EventArgs e)
{
}
//This is fine
private void MMForm(object sender, KeyPressEventArgs e)
{
Keys KP; KP = (Keys)sender;
if (KP == Keys.Escape) { frm2 fM2 = new frm2(); fM2.Show(); }
}
In frm2 do:
public class frm2 : Form
{
public frm2()
{
InitializeComponent();
this.Height = 200; this.Width = 200;
Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
}
}

Related

Activate treeview again

I have two forms. The main form contains a treeview. After I show the second form, the treeview loses focus. That's okay, but I want to activate the treeview when the second form closes.
Form1.cs
namespace ex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
form2.StartPosition = FormStartPosition.CenterParent;
form2.ShowDialog();
}
}
internal void example()
{
treeView1.SelectedNode = treeView1.Nodes[1];
}
private void Form1_Load(object sender, EventArgs e)
{
TreeNode node = new TreeNode("aaaa");
treeView1.Nodes.Add(node);
node = new TreeNode("bbbb");
treeView1.Nodes.Add(node);
node = new TreeNode("cccc");
treeView1.Nodes.Add(node);
}
}
}
Form2.cs
namespace ex
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
form1.example();
//not working
form1.treeView1.Focus();
form1.treeView1.Select();
}
}
}
Form2 really shouldn't get so intimate with Form1. Try turning your code around like this:
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
if (form2.ShowDialog(this) == DialogResult.OK) {
treeView1.Select();
example();
}
}
}
If Form2 is supposed to supply any information to add to your TreeView control, you would set up a property on Form2 and access it from within this same code block.

Why is the empty result turning?

Firstly, my English is bad, I'm sorry.
I want textbox (in Form2.cs) text to displaying MainForm.cs
When I apply the following codes, displaying blank message.
MainForm.cs
private void btnFilitre_ItemClick(object sender,DevExpress.XtraBars.ItemClickEventArgs e)
{
...
Form2 f2 = new Form2();
f2.Show();
}
private void workingFunction()
{
CommClass com = new CommClass();
MessageBox.Show(comm.Sorgu );
}
Form2.cs
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
CommClass comm = new CommClass();
comm.Sorgu = textBox1.Text;
f1.workingFunction();
Hide();
}
CommClass.cs
public string Sorgu { get; set; }
What is the problem?
You need to pass the argument in. In Form1 make this change:
public void workingFunction(CommClass comm)
{
MessageBox.Show(comm.Sorgu );
}
And in Form2, you will need to keep track of your Form1 reference, instead of creating a new one, and then pass in your CommClass object:
private void button1_Click(object sender, EventArgs e)
{
CommClass comm = new CommClass();
comm.Sorgu = textBox1.Text;
f1.workingFunction(comm);
Hide();
}

winform textBox not updating

i've two winforms form1 and form2 as the following:
public partial class Form1 : Form
{
Form2 frm2;
int count = 0;
public bool fromForm2;
public Form1(bool fromForm2 = false)
{
InitializeComponent();
this.fromForm2 = fromForm2;
MessageBox.Show(fromForm2.ToString());
if (fromForm2 == true) {
test();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (frm2 == null)
{
frm2 = new Form2(); //Create form if not created
frm2.FormClosed += frm2_FormClosed; //Add eventhandler to cleanup after form closes
}
frm2.Show(this); //Show Form assigning this form as the forms owner
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
frm2 = null; //If form is closed make sure reference is set to null
Show();
}
public void test ()
{
textBox1.Text = "ABCDFGHIJKLM";
MessageBox.Show(textBox1.Text);
}
}
Form 2
public partial class Form2 : Form
{
Form1 f1;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objj = new Form1(true);
objj = (Form1)Application.OpenForms["Form1"];
objj.fromForm2 = true;
objj = null;
}
}
i want click the button in form2, will run test(), but i've found that, if i use if (fromForm2 == true) { } the if statement, the test() function will not update (redraw?) the text value to display to the textBox1, anyone know what is the reason of this?

Generalizing the form calling in a button event

I usually call this piece of code to show a form whenever a button is clicked.
private frmSelection _frmSelection;`
private void _frmSelection_FormClosing(object sender, FormClosingEventArgs e)
{
_frmSelection = null;
}
private void changeFeedOrderToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_frmSelection == null)
{
_frmSelection = new frmSelection();
_frmSelection.FormClosing += _frmSelection_FormClosing;
_frmSelection.WindowState = FormWindowState.Minimized;
_frmSelection.Show();
_frmSelection.WindowState = FormWindowState.Normal;
}
else
{
_frmSelection.WindowState = FormWindowState.Minimized;
_frmSelection.WindowState = FormWindowState.Normal;
}
}
If the form is already open it will show the already opened instance instead of creating new instance. It is working fine.
But my problem is i need to copy paste and change the form name whenever i am adding a new form.
How it can be generalized and added to
Helper
class?
Something like this:
public sealed class ReusableFormContainer<T> : IDisposable
where T : Form, new()
{
private bool isDisposed;
private void HandleFormClosing(object sender, FormClosingEventArgs e)
{
Form = null;
}
public T Form { get; private set; }
public void Show()
{
if (isDisposed)
{
throw new ObjectDisposedException(null);
}
if (Form == null)
{
Form = new T { WindowState = FormWindowState.Minimized };
Form.FormClosing += HandleFormClosing;
Form.Show();
}
else
{
Form.WindowState = FormWindowState.Minimized;
}
Form.WindowState = FormWindowState.Normal;
}
public void Dispose()
{
// IDisposable.Dispose is implemented to handle cases, when you want to close
// wrapped form using code
if (!isDisposed)
{
Form?.Dispose();
isDisposed = true;
}
}
}
Usage:
// must be initialized somewhere in constructors
private readonly ReusableFormContainer<FormA> container_A;
private readonly ReusableFormContainer<FormB> container_B;
private void button1_Click(object sender, EventArgs e)
{
container_A.Show();
}
private void button2_Click(object sender, EventArgs e)
{
container_B.Show();
}
The following code might do what you want. Just thought about to use the Application functions because those can cover all forms that are on thread.
public partial class Form1 : Form
{
public int i;
private Form1 _frmSelection;
public Form1()
{
InitializeComponent();
i = Application.OpenForms.Count;
}
private void _frmSelection_FormClosing(object sender, FormClosingEventArgs e)
{
_frmSelection = null;
}
private void button1_Click(object sender, EventArgs e)
{
if (_frmSelection == null)
{
_frmSelection = new Form1();
_frmSelection.FormClosing += _frmSelection_FormClosing;
_frmSelection.WindowState = FormWindowState.Minimized;
_frmSelection.WindowState = FormWindowState.Normal;
_frmSelection.Show();
if (Application.OpenForms.Count > 1)
{
_frmSelection.Text = Application.OpenForms[i].Text + " and going";
}
}
else
{
_frmSelection.WindowState = FormWindowState.Minimized;
_frmSelection.WindowState = FormWindowState.Normal;
}
}
}

How can I make that approach's opposite

How can I passing values from a Form to another one directly? A receiver Form will be showing itself on screen and listening passing values which will be sending from a main form.
I know a way to do that with delegate and event but mine is not my desired one.
I need that with opposite way. Below is what I can do those code lines. This able to do only Form2 passes value to Form1 (main form). I need this approach's opposite. So, Form1 will be sender Form2 will be receiver, and transmit in real-time while they are showing itself on the screen.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.IdentityUpdated += new Form2.IdentityUpdateHandler(Form2_ButtonClicked);
f.Show();
}
private void Form2_ButtonClicked(object sender, IdentityUpdateEventArgs e)
{
textBox1.Text = e.FirstName;
}
}
public partial class Form2 : Form
{
public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);
public event IdentityUpdateHandler IdentityUpdated;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string sFirstName = txtFirstName.Text;
IdentityUpdateEventArgs args = new IdentityUpdateEventArgs(sFirstName);
IdentityUpdated(this, args);
}
}
public class IdentityUpdateEventArgs : System.EventArgs
{
private string mFirstName;
public IdentityUpdateEventArgs(string sFirstName)
{
this.mFirstName = sFirstName;
}
public string FirstName
{
get { return mFirstName; }
}
}
try this way
public partial class Form1 : Form
{
public delegate void IdentityUpdateHandler(object sender, EventArgs e);
public event IdentityUpdateHandler IdentityUpdated;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
IdentityUpdated(this, new EventArgs());
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
Form1 form1 = (Form1)Application.OpenForms["Form1"];
form1.IdentityUpdated += Form1OnIdentityUpdated;
}
private void Form1OnIdentityUpdated(object sender, EventArgs eventArgs)
{
MessageBox.Show("received");
}
}

Categories

Resources