i need your help.
I have two forms, Form1 and Form2. In Form1 i have a checkBox1 and in Form2 I have a checkBox2. All i want is the value of checkBox1 tranfering automatically in checkBox2.
Thanks in advance
First of all, in a multi-form application, form-form direct contact should not be permitted. However, I can think of a way which I present here considering yours is an exceptional scenario. So the method might violate usual best practices.
Here is the method
Make your checkboxes in your forms as Public. You can do that by clicking on the CheckBox in the design mode and then selecting Public under Modifiers in Properties window. This step makes your checkbox accessible from outside your form's instance.
Write the following code in CheckedChanged event of CheckBox in Form1.
((Form2)(Application.openForms["Form2"])).checkBox1.Checked = this.checkBox1.Checked;
However, I strongly recommend revisiting your strategy based on your application need.
On Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Form2 a = new Form2();
a.c = checkBox1.Checked;
a.ShowDialog();
}
}
On Form2:
public partial class Form2 : Form
{
public bool c;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = c;
}
}
All instances of Form2 within an MDI parent will reflect any change to the CustomCheckBox control Checked property placed on the Form. Of course, this would be true of when placing the CustomCheckBox on any MDI child form, just set up the proper events like Form2.
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 {
public Form2() {
InitializeComponent();
CustomCheckBox.CheckChanged += (object sender, EventArgs e) => {
if (sender != m_customCheckBox) { m_customCheckBox.Checked = CustomCheckBox.GetCheck(); }
};
FormClosed += (object _sender, FormClosedEventArgs _e) => {
CustomCheckBox.CheckChanged -= (object __sender, EventArgs __e) => { };
};
}
};
};
////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public class CustomCheckBox : CheckBox {
static private bool? m_checked = null;
static private object m_synchRoot = new object();
static public event EventHandler CheckChanged;
public CustomCheckBox() {
if (HasCheckValue) {
// Do this BEFORE we set up the CheckChanged event so that
// we do not needlessly kick off the CustomCheckBox.CheckChanged
// event for any other existing CustomCheckBoxes (as they already
// have their Checked property properly set)...
Checked = CustomCheckBox.GetCheck();
}
this.CheckedChanged += new EventHandler(OnCheckedChanged);
}
protected void OnCheckedChanged(object sender, EventArgs e) {
if (CustomCheckBox.HasCheckValue && this.Checked == CustomCheckBox.GetCheck()) {
return;
} else {
CustomCheckBox.SetCheck(this.Checked);
if (CustomCheckBox.CheckChanged != null) {
CustomCheckBox.CheckChanged(sender, e);
}
}
}
static public bool HasCheckValue {
get {
lock (m_synchRoot) {
return m_checked.HasValue;
}
}
}
static public bool GetCheck() {
lock (m_synchRoot) {
return m_checked.Value;
}
}
static private void SetCheck(bool _check) {
lock (m_synchRoot) {
m_checked = _check;
}
}
};
};
Related
Hey I got Code Written in Class Form1 And Form2. I want to call the method openkindForm() from Form2. I tried every soloution I found. I got this one at the moment which is not working it gives me a System.NullReferenceException. I do not know why it isnt working. I tried it so long but whatever I do it will not workout somehow. I would be thankfull for an answer.
Kind regards
First Class
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 FBDP00
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
submenupanel.Visible = false;
}
private void funktionenSM_Click(object sender, EventArgs e)
{
switch (submenupanel.Visible)
{
case true:
submenupanel.Visible = false;
break;
case false:
submenupanel.Visible = true;
break;
}
}
public void neuepruefungSm_Click(object sender, EventArgs e)
{
submenupanel.Visible = false;
openkindForm(new Form2());
}
public Form activeForm = null;
public void openkindForm(Form childForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
backgroundPanel.Controls.Add(childForm);
childForm.Show();
}
}
}
Class 2
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;
using static FBDP00.Form1;
namespace FBDP00
{
public partial class Form2 : Form
{
public Form1 testform;
public Form2()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void funktionenSM_Click(object sender, EventArgs e)
{
}
public void baukontrolleb_Click(object sender, EventArgs e)
{
testform.openkindForm(new Form3());
}
}
}
In Form2 you have defined a variable for Form1 (testform), but you don't set this anywhere. So this is why you get a Null reference error when you try to use it, because it is null!
So when you create your Form2 then you need to set this value. In your case, maybe in the constructor like this.
public Form1 testform;
private Form2(Form1 f1)
{
InitializeComponent();
testform = f1;
}
Then call it like this:
public void neuepruefungSm_Click(object sender, EventArgs e)
{
submenupanel.Visible = false;
openkindForm(new Form2(this));
}
However, looking at your openkindForm method, it seems to me that this really has nothing to do with Form1, and shares no variables, so should not be in this class.
You should either make this static (together with the activeForm variable), or make it a Singleton class instead. But certainly this should be a separate class.
When trying to write something to check whether a number is odd or even (C#), Visual Studio 2010 displays the error message I've mentioned above...
This is the code
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void chk_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(num.Text);
if (x%2 == 0)
{
chkd.Text = "Even";
}
else
{
chkd.Text = "Odd";
}
}
}
private void num_TextChanged(object sender, EventArgs e)
{
}
}
num_TextChanged is a method that is outside your class. The only things that can appear inside a namespace scope are, per the error message, a class, delegate, enum, interface or struct.
I expect you meant this to be inside the Form1 class, so you should move it:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void chk_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(num.Text);
if (x%2 == 0)
{
chkd.Text = "Even";
}
else
{
chkd.Text = "Odd";
}
}
private void num_TextChanged(object sender, EventArgs e)
{
}
}
}
Add one more
}
which close /end namespace of form class
I'm relatively new to C# and come a bit stuck.
I've got a Rich Textbox on a form, and I would like to update this from a different class to the Form itself.
I first tried
Form1.outputTextbox.AppendText(string);
but the text box was not accessible, made sense. So instead I tried to make a function. On Form1 I created the function
public void updateTextBox(string new_text)
{
outputTextBox.AppendText(new_text);
}
and in the class I used.
Form1.updateTextBox("apple");
The problem I'm having is the only way my class can see the function is if I make it the function static, but when I do that get an error "An object reference is required for the nonstatic field, method, or property 'member'"
Am I close or going to wrong way about this completely? Any help would be appricated.
Alternatively, you can do something like the following. This takes advantage of custom arguments and events.
namespace WindowsFormsApplication3
{
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;
public partial class Form1 : Form
{
TextBox textBox;
SomeClass someClass;
public Form1()
{
InitializeComponent();
Initialize();
BindComponents();
}
private void BindComponents()
{
//EventHandlers
this.Load += new EventHandler(Form1_Load);
this.someClass.TextUpdatedEvent += new EventHandler(someClass_TextUpdatedEvent);
}
void someClass_TextUpdatedEvent(object sender, EventArgs e)
{
this.textBox.Text = (e as FormArgs).Text;
}
private void Initialize()
{
this.textBox = new TextBox();
this.someClass = new SomeClass();
}
void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(textBox);
}
}
public class SomeClass
{
public event EventHandler TextUpdatedEvent = delegate { };
public void UpdateText(string text)
{
if (TextUpdatedEvent != null)
{
TextUpdatedEvent(this, new FormArgs() { Text = text });
}
}
}
public class FormArgs : EventArgs
{
public string Text { get; set; }
}
}
If you do it this way, you can update the form text like this:
someClass.UpdateText("changing the text on the form");
You are trying to access the function from the class instead of the object.
Use
Form1 myForm = new Form1();
...
myForm.updateTextBox("whatever");
Also be aware of thread issues here. What triggers the outside code? Is it another ui action, then all is well. Does it come from another thread, then you´ll have to handle this.
Pass Form1 instance to the other class via constructor or property. Then you can access outputTextbox from the other class.
public class OtherClass
{
private Form1 form1;
public OtherClass(Form1 form1)
{
this.form1 = form1;
}
private void ChangeText()
{
form1.outputTextBox.AppendText("hello world");
}
}
Instantiate OtherClass from Form1.cs and pass its instance.
In Form1.cs:
OtherClass obj = new OtherClass(this);
I know it is very late, but maybe someone need the solution...
you can access to all controller from another form without create object by pass it as parameter to constructor...
for Example
public partial class Form2 : Form
{
MainForm mainForm;
public Form2(MainForm mainForm)
{
InitializeComponent();
this.mainForm = mainForm;
txtRecive00.TextChanged += new EventHandler(txtRecive8changed);
}
void txtRecive8changed(object sender, EventArgs e)
{
mainForm.txtRecive1.Text += txtRecive00.Text;
}
in my case I can update the text in mainForm.txtRecive1.Text from Form2...
and in MineForm we creat object from Form2 like that:
Form2 f2 = new FormMeasure(this);
for more Info show this short video https://www.youtube.com/watch?v=CdH8z_JNi_U
I have two forms. form1 calls starts a background running thread during its loading.
once it stars running. form 2 will popup having two buttons (start&stop).
when i press stop button the thread should pause and when i press start, the pause thread should start it execution from where it stopped.
I tried to use this code.
myResetEvent.WaitOne();// to pause the thread
myResetEvent.Set(); // to resume the thread.
as these events are defined in the form1but I'm wanting it to work from form2.
Finally i got the answer, it worked for my case,posting it, might it will help others..
Form 1 code..
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;
using System.Threading;
namespace thread_example
{
public partial class Form1 : Form
{
private int i = 0;
public Thread Run_thread = null, run1 = null; // thread definition
public static AutoResetEvent myResetEvent = new AutoResetEvent(false);//intially set to false..
public Form1()
{
InitializeComponent();
run1 = new Thread(new ThreadStart(run_tab));
run1.IsBackground = true;
run1.Start();
}
private void run_tab()
{
//do something.
}
private void button1_Click(object sender, EventArgs e)
{
form2 f2 = new form2();
f2.Show();
}
}
}
// Form 2 code...
namespace thread_example
{
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private void btn_stop_Click(object sender, EventArgs e)
{
Form1.myResetEvent.WaitOne();
}
private void button2_Click(object sender, EventArgs e)
{
Form1.myResetEvent.Set();
}
}
}
You can pass the Event instance to the second form as form parameters.
class Form1
{
ManualEvent myResetEvent;
void ShowForm2()
{
var form2 = new Form2(myResetEvent);
form.ShowDialog();
}
}
class Form2
{
ManualEvent myResetEvent;
Form2(ManualEvent event)
{
myResetEvent = event;
}
void StopButtonClick()
{
myResetEvent.Reset();
}
void ContinueButtonClick()
{
myResetEvent.Set();
}
}
I am really stuck with this so I hope someone can help. I have 2 winforms, one has a listview and another has a textbox. I want to check which item is checked in the listview and copy this text into the second form. I have tried this code but it won't work, any help is greatly appreciated!
// 1st form
private void button5_Click(object sender, EventArgs e) // Brings up the second form
{
Form4 editItem = new Form4();
editItem.Show();
}
public string GetItemValue()
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
return listView1.Items[i].Text;
}
}
return "Error";
}
// 2nd form
private void Form4_Load(object sender, EventArgs e)
{
Form1 main = new Form1();
textBox1.Text = main.GetItemValue();
}
You are creating a new Form1 inside of Form4 after it has been loaded. You need a reference to the original Form1. This can be accomplished in several ways, probably the easiest is passing a reference into the Form4 constructor.
// Form 1
// This button creates a new "Form4" and shows it
private void button5_Click(object sender, EventArgs e)
{
Form4 editItem = new Form4(this);
editItem.Show();
}
public string GetItemValue()
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
return listView1.Items[i].Text;
}
}
return "Error";
}
--
// Form 2 (Form4)
// Private member variable / reference to a Form1
private Form1 _form;
// Form4 Constructor: Assign the passed-in "Form1" to the member "Form1"
public Form4(Form1 form)
{
this._form = form;
}
// Take the member "Form1," get the item value, and write it in the text box
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = this._form.GetItemValue();
}
You only need a couple of changes. No need to add your own way of storing the owner form as this functionality already exists.
private void button5_Click(object sender, EventArgs e) // Brings up the second form
{
Form4 editItem = new Form4();
editItem.Show(this); //passes a reference to this form to be stored in owner
}
Then reference it on the other form.
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = ((Form1)owner).GetItemValue();
}
Try thisway
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
//Fields
public List<string> itemTexts;
public Form1()
{
InitializeComponent();
//Generate some items
for (int i = 0; i < 10; i++)
{
ListViewItem item = new ListViewItem();
item.Text = "item number #" + i;
listView1.Items.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
if (item.Checked)
{
itemTexts.Add(item.Text);
}
}
Form2 TextBoxForm = new Form2(itemTexts);
TextBoxForm.Show();
}
}
}
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 WindowsFormsApplication2
{
public partial class Form2 : Form
{
//Fields
List<string> itemTexts;
public Form2(List<string> itemTexts)
{
InitializeComponent();
this.itemTexts = itemTexts;
foreach (string text in itemTexts)
{
textBox1.Text += text + Environment.NewLine;
}
}
}
}