How can I access a Radio Button in another form? - c#

I want to see if notch50hzbutton is checked in another form, something like: if (SettingsForm.notch50hzbutton.Checked == true) ..... How can I do this?
namespace ClassLibrary1
{
using GraficDisplay;
using GraphLib;
using PrecisionTimer;
public partial class SettingsForm : Form
{
public SettingsForm()
{
InitializeComponent();
notch50hzbutton.Checked = false;
notch60hzbutton.Checked = true;
}
private void notch50Hz_Checked(object sender, EventArgs e)
{
notch50hzbutton.Checked = true;
}
private void notch60Hz_Checked(object sender, EventArgs e)
{
notch60hzbutton.Checked = true;
}
}
}

public bool Notch50HzIsChecked
{
get { return notch50hzbutton.Checked; }
set { notch50hzbutton.Checked = value; }
}
You may then access it like a regular property from outside the class.

Make a public property on the form and pass through the value you want to access externally?

A way to check to see if the control is checked without exposing the control itself is to add a public method (or a public property with just the getter) that does that to your SettingsForm.
public bool IsNotch50hzbuttonChecked()
{
return notch50hzbutton.Checked;
}
and then you can check
if (settingsFormInstance.IsNotch50hzbutton())
{
...
}

I would make a corresponding public property that's accessible to the outside:
public partial class SettingsForm : Form
{
public bool Is60Hz {get; private set;}
...
private void notch50Hz_Checked(object sender, EventArgs e)
{
notch50hzbutton.Checked = true;
Is60Hz = false;
}
private void notch60Hz_Checked(object sender, EventArgs e)
{
notch60hzbutton.Checked = true;
Is60Hz = true;
}

Related

Use method from class in form

I have class in my project named Visibility.cs and I wanna use method named HospodaVisible() in Form1 but it doesnt work. It isn't showing usercontrol.
here is code of class and form1:
public static class Visibility
{
static Form1 f = new Form1();
public static void HospodaVisible()
{
f.hospoda1.Visible = true;
f.arena1.Visible = false;
f.podzemi1.Visible = false;
}
public static void ArenaVisible()
{
f.hospoda1.Visible = false;
f.arena1.Visible = true;
f.podzemi1.Visible = false;
}
public static void PodzemiVisible()
{
f.hospoda1.Visible = false;
f.arena1.Visible = false;
f.podzemi1.Visible = true;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
hospoda1.Visible = false;
arena1.Visible = false;
podzemi1.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
Visibility.HospodaVisible();
}
}
See if this works for you:
public static class Visibility
{
public static void HospodaVisible(Form1 f)
{
f.hospoda1.Visible = true;
f.arena1.Visible = false;
f.podzemi1.Visible = false;
}
}
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Visibility.HospodaVisible(this); // passes a reference to the "current" form, i.e. Form1 itself
}
}
this is a reference to the current instance of a class inside the class, in other words it's self.
In the code above you passes a reference to the active, current form to a static helper method which accepts a reference to a form (doesn't instantiates one) and makes the necessary changes to it. Basically, it says: "hey helper, do something with this form".

return null when use the backing field

i write this code for add in database .
public partial class Unit : UserControl
{
private ProductUnit _pu { get; set; }
public ProductUnit PU
{
get
{
_pu.UnitNicname = TxtUnitNicName.Text;
_pu.UnitFaName = TxtUnitName.Text;
return _pu;
}
set { }
}
public Unit()
{
InitializeComponent();
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
StructureMapDefenation.Container.GetInstance<IUnitService>().Add(_pu);
}
}
i need to get textbox.text value with backing field but when i need to add value in database this _pu is null .
whats the problem ???
You never initialize _pu, which should be a field and not a property by the way:
private ProductUnit _pu = new ProductUnit();
I don't really understand the purpose of the PU property though.
You might as well create a ProductUnit directly in the event handler:
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
var pu = new ProductUnit();
pu.UnitNicname = TxtUnitNicName.Text;
pu.UnitFaName = TxtUnitName.Text;
StructureMapDefenation.Container.GetInstance<IUnitService>().Add(pu);
}
Try this:
public partial class Unit : Form // <--------- Try this
{
private ProductUnit _pu= new ProductUnit();
public ProductUnit PU
{
get {return _pu;} // read only
}
public Unit()
{
InitializeComponent();
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
_pu.UnitNicname = TxtUnitNicName.Text;
_pu.UnitFaName = TxtUnitName.Text;
StructureMapDefenation.Container.GetInstance<IUnitService>().Add(_pu);
}
}

c# Many Parent forms one child

I have Parent_1, Parent_2, Parent_3 and etc. and have one Child form
in Parent_1, Parent_2, Parent_3 ... I have:
private string strText;
public string pubText {
get { return strText; }
set { strText = value; } }
on button:
private void btbutton1_Click(object sender, EventArgs e)
{
var form = new fChild(this);
form.ShowDialog();
}
in child form I have the code:
private Parent_1 logicalParent;
public fChild(Parent_1 parent)
{
InitializeComponent();
logicalParent = parent;
this.FormClosed += new FormClosedEventHandler(child_FormClosed);
}
and
void child_FormClosed(object sender, FormClosedEventArgs e)
{
logicalParent.pubText = this.textBox1.Text;
}
This works only for 1 parent form, How Can I use this for other Parent forms???
Please Help
You can create a common interface for the three parent forms:
public interface IParentForm
{
string PubText {get; set;}
}
public class Parent_1 : Form, IParentForm
{
public string PubText
{
get { return this.pubText; }
set { this.pubText = value; }
}
}
//same for Parent_2 and 3
then in your child form declare logicalParent to be of type IParentForm, and change the constructor of the child form to be public fChild(IParentForm parent)
At the button click do the following:
private void btbutton1_Click(object sender, EventArgs e)
{
var form = new fChild(this); // this is not more needed
form.ShowDialog();
pubText = form.pubText;
}

Returning the textbox value of another form after initializing it?

I'm trying to make my own custom input dialogue by designing a form. How would I initialize it so that once I press OK, I can receive the value of the textbox in it, back to where I initially called it?
You can create a form that exposes a property like this:
public class InputDialog:Form
{
public string Result { get; set; }
private void OK_Click(object sender, EventArgs e)
{
Result = txtResult.Text;
this.Close();
}
}
And in your base form you do:
var dialog = new InputDialog();
dialog.ShowDialog();
string Result = dialog.Result;
You can use events for communication between forms. This way InputForm hides logic, properties from outside world.
public class InputEventArgs : EventArgs
{
public string Input { get; private set; }
public InputEventArgs(string input)
{
Input = input;
}
}
public class InputDialog : Form
{
public EventHandler<InputEventArgs> InputSet;
private void OkClick(object sender, EventArgs e)
{
var ev = InputSet;
if (ev != null)
{
ev(this, new InputEventArgs(txtInput.Text));
}
}
}
and in your calling form:
private void ShowInputForm()
{
using (var frm = new InputDialog())
{
frm.InputSet += (s, e) =>
{
txtResult.Text = e.Input;
}
frm.ShowDialog();
}
}

Mistake with receiving bool variable between two forms

All.
I want to receive checkbox1 data from form1 to form2. I used get and set methods to receive meaning of variable. I used this code for this. But it didn't work. Why? Where is the problem?
form1.cs
...
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 test = new Form2();
test.checkBox1 = checkBox1.Checked;
test.Show();
}
}
}
form2.cs
...
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private bool data7;
public Form2()
{
InitializeComponent();
}
public bool checkBox1
{
get { return data7; }
set { value = data7; }
}
private void Form2_Load(object sender, EventArgs e)
{
if (data7 == true)
{
label1.Text = "true";
}
else
{
label1.Text = "false";
}
}
}
}
set { value = data7; }
should be
set { data7 = value; }
Your set method is wrong. It should be
set { data7 = value; }
Form2 apparently stores the value in the variable data7, but not in a CheckBox. You would have to do something like this to actually store it in a CheckBox
public bool checkBox1
{
get { return myCheckBox.Checked; }
set { myCheckBox.Checked = value; }
}
Another problem is returning the result of user input to Form1. Since you call Form2 with test.Show(); the code after this statment continues immediately, without wating for Form2 to close. Call test.ShowDialog(); instead.
Another option for returning a result while not blocking Form1 is to use an event. Using this definition
public class Form2ResultEventArgs : EventArgs
{
public Form2ResultEventArgs(bool checked)
{
this.Checked = checked;
}
public bool Checked { get; private set; }
}
In Form2 you would define an event like this.
public event EventHandler<Form2ResultEventArgs> Form2Result;
private OnForm2Result(bool checked)
{
var handler = Form2Result;
If (handler != null) {
handler(this, new Form2ResultEventArgs(checked));
}
}
// Assuming that you have a OK button on Form2
private OkButton_Click (...)
{
OnForm2Result(myCheckBox.Checked);
this.Close();
}
In Form1
var test = new Form2();
test.Form2Result += ProcessResult;
test.Show();
...
private void ProcessResult(object sender, Form2ResultEventArgs e)
{
bool result = e.Checked;
...
}
UPDATE
If you only want to set a label, why not just do this
In Form2
public void SetDisplay(bool value) {
label1.Text = value.ToString();
}
In Form1
var test = new Form2();
test.SetDisplay(checkBox1.Checked);
test.Show();
Note that InitializeComponent is called in the constructor of Form2 and therefore the label exists after new Form2(). No need to do that in Form2_Load.
I would like to suggest that when the bool data7 is created that is set a value of false, or true I don't care, cause when the form is first loaded if the user has not set CheckBox1 the Form will crash. And Yes I know the OP, in the Code given, did set the variable.
private bool data7 = false;
I have included Fur Dworetzkys Answer in my suggestion Below
...
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
//give the variable a default value
private bool data7=false;
public Form2()
{
InitializeComponent();
}
public bool checkBox1
{
get { return data7; }
//Here is Furs Correction
set { data7 = value; }
}
private void Form2_Load(object sender, EventArgs e)
{
if (data7 == true)
{
label1.Text = "true";
}
else
{
label1.Text = "false";
}
}
}
}
if you are not doing anything (e.g. validation) with data7 in get or set, then just get rid of them.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public bool checkBox1 { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
if (checkBox1)
{
label1.Text = "true";
}
else
{
label1.Text = "false";
}
}
}

Categories