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";
}
}
}
Related
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.
Basically trying to learn a few things I havn't tried yet. I want to make a small program that has a Textbox and Button.
Whats typed into the textbox field will be added to the end of a url applied from the button.
http://website.com/stuff?things= +textboxValue
After you click the launch button, I want the url created with the entered text, to open in a second form.
I have everything working except for the text from form1 to carry over to form2. Just wondering how I can go about this.
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.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.LinkTarget = textBox1.Text;
form2.Show();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form3 newFrm = new Form3();
newFrm.Show();
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
}
}
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 WindowsFormsApplication8
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string LinkTarget {
get;
set;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string myUrl = "http://website.com/stuff?things=" + LinkTarget;
}
}
}
Example of what I am going for.
Form one has text box and button.
when you click the button it will open http://www.stackoverflow.com/questions/ in the second form. the second form is basically a browser.
but, in form one if I type 41090977 before I hit the button, it will open http://www.stackoverflow.com/questions/41090977
You can create a property in form2, which you set before you open it:
//... other form2 code
public string LinkTarget {
get;
set;
}
You can set the value like this (in button1_Click):
Form2 form2 = new Form2();
form2.LinkTarget = myTextBox.Text;
form2.Show();
Within form2 you can use the value of the property to create your link. In your current code you have just defined the property. You have to use its content to create your link. This is done by attaching the property value to your string containing the URL with the + operator:
string myUrl = "http://website.com/stuff?things=" + LinkTarget;
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";
}
}
}
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";
}
}
}
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; }
}
}