How to set position second form depend on first form? - c#

I have Form1 and Form2 and a button in my project. When i click button Form2 will show. What is the command to set position of Form2 at center of form1?

Set the forms StartPosition property to CenterParent. That way it will always pop up in the center.

You can manually set the location when you open it:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.StartPosition = FormStartPosition.Manual;
f2.Load += delegate(object s2, EventArgs e2)
{
f2.Location = new Point(this.Bounds.Location.X + this.Bounds.Width / 2 - f2.Width / 2,
this.Bounds.Location.Y + this.Bounds.Height / 2 - f2.Height / 2);
};
f2.Show();
}
The key here is setting the StartPosition to manual.
On my system, setting StartPosition to CenterParent and using Show(this) doesn't center on the "owner". Maybe something is broken on my system...it's always been like that for me.

You need to use an instance of the 2nd form. See example of my 2 form project
Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2(this);
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
string results = form2.GetData();
}
}
}
​
Form 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 nform1)
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
form1 = nform1;
form1.Hide();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//stops form from closing
e.Cancel = true;
this.Hide();
}
public string GetData()
{
return "The quick brown fox jumped over the lazy dog";
}
}
}
​

Related

How can I change the background of the a form from a second form?

I am using C# windows forms and I'm having a problem. I have a form which I want to change the background of, however, I want to do so from a second form. The second for has a button which when pressed, the background of the first form changes. Here is my code
First Form:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
Second Form:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.TransparencyKey = Color.Turquoise;
frm1.BackColor = Color.Turquoise;
}
}
}
The button is supposed to turn the first form transparent. This however, does not work. Am I missing something? Thank you!
You can set the Form 2's owner to Form1. Then access it's properties that way.
Form 1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog(this);
}
Form 2
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
frm1.TransparencyKey = Color.Turquoise;
frm1.BackColor = Color.Turquoise;
}
You can do it using delegate and events or by implementing singleton in parent form.

Keep checked radio button checked through forms C# Visual Studio 2010

I have a form with 10 radio buttons (Form2)for a user to check (all in same group). Then a button to go to the next form (Form3).
On Form3 I have a back button to go back to Form2 to change the radio button if needed.
When the back button is pressed, it goes to Form2 with all of the radio buttons, but it doesn't show the previously checked radio button.
Example code:
string SchoolName = "";
if (radioButton1.Checked)
{
SchoolName = radioButton1.Text;
}
if (radioButton2.Checked)
{
SchoolName = radioButton2.Text;
}
and then going back to the previous form using back button:
private void button3_Click(object sender, EventArgs e)
{
this.Close();
th = new Thread(opennewform);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
private void opennewform(object obj)
{
Application.Run(new Form2());
}
recreating the form like new Form2() will add a initialized form with default values, and that will result in loose of your changes.
to solve you can:
play with show/hide instead of close/new
save object state, when re-opening form2 for example you can call a constructor like
new form2(check1State,check2State,selectedDropItem,txtName...);
In your opennewform() method you are instantiating a new copy of Form2 and not going back to the one you came from originally. That's why your original radio button selection is not being saved. You need to somehow return to the original Form2 instance instead of creating a new instance.
For example, you can hide the Form2 when user closes it and re-Show it when user needs it again.
You are not going back to the same instance of the form, you are creating a new form. See my two form project below
Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2(this);
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
string results = form2.GetData();
}
}
}
Form 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 nform1)
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
form1 = nform1;
form1.Hide();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//stops form from closing
e.Cancel = true;
this.Hide();
}
public string GetData()
{
return "The quick brown fox jumped over the lazy dog";
}
}
}

how do i call method of active mdichild form from mdi parent form

I have mdi parent with split container control. I divided into two Panels.
Panel 1 contains child Form and Panel 2 contains some buttons like SAVE ,DELETE and UPDATE.
Panel 1 can be loaded with some Child forms.
I Want to Call Methods of active Child form in Panel 1 when SAVE button is clicked.
you can use ActiveMdiChild property to get active child of form.
You have to pass an instance of a form to use in a different class. See my two form project below :
Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2(this);
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
string results = form2.GetData();
}
}
}
Form 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 nform1)
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
form1 = nform1;
form1.Hide();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//stops form from closing
e.Cancel = true;
this.Hide();
}
public string GetData()
{
return "The quick brown fox jumped over the lazy dog";
}
}
}

Get/Set Variable

the goal of the program is to be able to use the get and set methods of variables.
I have this code in a project C#:
Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int c = 0;
public int a { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
a = 5;
Form2 f2 = new Form2();
f2.b = a;
f2.Show();
}
}
}
and in a Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
MessageBox.Show(Convert.ToString(b));
}
}
}
This code not work because the value of b should be 5, but during execution value 0;
any solution?
You can't expect values of one instance to magically propagate to every other instance.
So doing this:
Form1 f1 = new Form1();
int b = f1.a;
Is always going to be 0. You created a new instance, and nothing has happened to it! If you want to get the existing form's value (where the button was presumably clicked) you need to pass it to Form2 somehow.
You can:
Pass it on the constructor of Form2
Set up a service that holds the data instead
Probably about a million other approaches
private void button1_Click(object sender, EventArgs e)
{
a = 5;
Form2 f2 = new Form2();
f2.b=a;
f2.Show();
}
public partial class Form2 : Form
{
public int b;
public Form2()
{
InitializeComponent();
}
}
You want to pass the data before showing the second form instead of after the fact.

Change the state of a checkbox on Form2 and keep the state

All I am trying to do is to change the state of checkbox on form2, and keep the state after pression OK.
I have form1 which is my main form and it only has one Strip menu. The code for form1 is as follow:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
dialog.ShowDialog();
}
}
}
The Form2 only has one checkbox and one OK button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void OK_Click(object sender, EventArgs e)
{
if (cbxForm2.Checked == true)
{
cbxForm2.Checked = true;
}
}
}
}
How can I change my code so when I go back on the menu the state of the combo box is as I left it?
You are creating a new Form2 every time:
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
// the 'new' keyword means you are creating an entirely new instance
Form2 dialog = new Form2();
dialog.ShowDialog();
}
This new instance has no idea what any previous instances looked like, so you need to store the state of the CheckBox and assign the value when you open Form2.
public partial class Form1 : Form
{
// backing field to store the state
bool checkBoxChecked;
public Form1()
{
InitializeComponent();
}
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
// assign the state
dialog.CheckBoxChecked = this.checkBoxChecked;
dialog.ShowDialog();
// save the state
this.checkBoxChecked = dialog.CheckBoxChecked;
}
}
You also need to add a property on Form2 so you can retrieve the state:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public bool CheckBoxChecked
{
get { return cbxForm2.Checked; }
set { cbxForm2.Checked = value; }
}
}

Categories

Resources