how to access winform components from another class? - c#

i have a form with one button and two labels
and i have a separate class called myCounter
i want the myCounter class to be able to access the labels in the form
through a method called changeColor..
how can make the labels available in this class
the form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Color colTurn
{
get { return lblp1Turn.BackColor; }
set { lblp1Turn.BackColor = value; }
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
the class
class myCounter
{
private readonly Form1 Board;
public myCounter(Form1 Board)
{
this.Board = Board;
}
public int turn = 0;
public void changeColor()
{
if (turn == 0)
{
turn = 1;
lbl
//change color code here
}
}
}

So it looks like you're passing the whole form into your second class anyway, So I'd do what LightStriker suggested. Make a public accessor for all of your items and then set it in your other class.
public partial class Form1 : Form
{
private myCounter _counterClass;
public Form1()
{
InitializeComponent();
}
public Label MyLabel1
{
get {return mylabel1;}
}
public Label MyLabel2
{
get {return mylabel2;}
}
private void Form1_Load(object sender, EventArgs e)
{
_counterClass = new myCounter(this);
}
protected void ButtonClick(object sender, EventArgs e)
{
_counterClass.changeColor();
}
}
Then in your second class you have access to your Label.
class myCounter
{
private readonly Form1 Board;
public myCounter(Form1 Board)
{
this.Board = Board;
}
public int turn = 0;
public void changeColor()
{
if (turn == 0)
{
turn = 1;
Board.MyLabel1.BackColor = Color.Red;
Board.MyLabel2.BackColor = Color.White;
}
else
{
turn = 0;
Board.MyLabel2.BackColor = Color.Yellow;
Board.MyLabel1.BackColor = Color.White;
}
}
}
Keep in mind this is code I have written in a wiki markup editor and is untested. This SHOULD work for you though.

Create a public method on your form for this.
public partial class Form1 : Form{
public void SetLabelColor(Color color){
mylabel.BackColor = color;
}
//... Other code
}

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".

Creating multiple instances of class

I have this code:
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Human
{
public string Name;
public Human(string name)
{
Name = name;
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Jane";
}
private void AddNewHuman_Click(object sender, EventArgs e)
{
Human h1 = new Human(textBox1.Text);
}
}
}
Is there a way, how to create a new instance of Human whenever I click the Button(AddNewHuman_Click)?
After clicking few times on the button, there will still be only one Human h1, right?
You will have to create list of object to store multiple object of human class.
I do changed here for you. I hope it will work for you.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Human
{
public string Name;
public Human(string name)
{
Name = name;
}
}
List<Human> objHumanList;
private void Form1_Load(object sender, EventArgs e)
{
objHumanList=new List<Human>();
textBox1.Text = "Jane";
}
private void AddNewHuman_Click(object sender, EventArgs e)
{
Human h1 = new Human(textBox1.Text);
objHumanList.add(h1);
/** Or
objHumanList.add (new Human(textBox1.Text))
**/
}
}
}
You can create a list of Humans and keep on adding a new Human to the list whenever the button is clicked.

Instantiate object of a class under form class

I am new to windows forms. I am trying to instantiate a object of a public class and calling a method drawBoard() when button1 is pressed. Method drawBoard() through which I want to set the properties of pictureBox2. But the code didn't work.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class gameBoard :Form1
{
public void drawBoard()
{
pictureBox2.ImageLocation = #"E:\My Data\DoCx\CS\3rd Sem\OOP\proj\images\a.png";
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
}
}
private void button1_Click(object sender, EventArgs e)
{
gameBoard a = new gameBoard();
a.drawBoard();
}
}
Also tried to implement this in other two ways:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox2.ImageLocation = #"E:\My Data\DoCx\CS\3rd Sem\OOP\proj\images\a.png";
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox2.BackColor = Color.Transparent;
}
}
and
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox2.ImageLocation = #"E:\My Data\DoCx\CS\3rd Sem\OOP\proj\images\a.png";
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
}
}
Both worked. Directly loads the image when the code runs. And also when the button1 pressed in second way. I wonder how to call the pictureBox properties when they defined in a method of a user-defined class.
If you observe carefully then your gameBoard is defined as a nested class inside Form1 and it also inherits from Form control, which doesn't make sense. You probably wan to have the class defined outside like (probably in a separate file)
public class gameBoard
{
private PictureBox _box;
public gameBoard(PictureBox box)
{
_box = box;
}
public void drawBoard()
{
_box.ImageLocation = #"E:\My Data\DoCx\CS\3rd Sem\OOP\proj\images\a.png";
_box.SizeMode = PictureBoxSizeMode.Zoom;
}
}

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";
}
}
}

How can I access a Radio Button in another form?

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;
}

Categories