Instantiate object of a class under form class - c#

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

Related

Changing properties of a button via method from another class

Maybe you can help me with my problem.
My Class "Form1" calls the method setButtons();
but setButtons() ist not at Class "Form1", its in Class "Class1".
setButtons() in "Class1" does not recognice Button1 from Form1.
How do I let it know that Button1 exists in Form1 and I want the method to work on the Button1 from "Form1"? Class1 has already a using directory to Form1 and Form1 has one to Class1.
//this does not work
public static void setbuttons()
{
Form1.Button1.Location = new Point(40, 40);
}
I found out that if you declare a control public in the designer file like so
public Button button1;
Then you can access it from another class on the condition that you get the form object, for example as a extension
static class AnotherClass
{
public static void setButtons(this Form1 form)
{
form.button1.Text = "Hello";
}
}
A better way to change the properties of a button, in terms of design and code management, would be to make a method in your form that does it.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
...
}
public void ChangeButtonTextMethod(string text)
{
button1.Text = text;
}
}
Consider using an event where the method in Class1 passes a Point to the calling form which listens for the event, in this case SetButtons.
public class Class1
{
public delegate void OnSetLocation(Point point);
public static event OnSetLocation SetButtonLocation;
public static void SetButtons()
{
SetButtonLocation!(new Point(40, 40));
}
}
In the form subscribe to SetButtonLocation, invoke the method SetButtons which passes a Point to the caller in Form1 and in turn sets the button Location.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Class1.SetButtonLocation += OnSetButtonLocation;
Class1.SetButtons();
Class1.SetButtonLocation -= OnSetButtonLocation;
}
private void OnSetButtonLocation(Point point)
{
button1.Location = point;
}
}
Using this approach is better than changing the modifers of form to public as mentioned already.

get the Width of the textbox when opening the form in C#

I have a form as shown below, I want to save the width of the original textbox when the form loads, I have written code but not running, please help.
namespace DieuKhienMayTinh
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
float Sizetb = textBox1.Width;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Sizetb);
}
}
}
form
You are storing textbox width in a method-local variable Sizetb. This wont compile at all.
Move it out to class level:
namespace DieuKhienMayTinh
{
public partial class Form2 : Form
{
// class level member field available for each method in class
private float Sizetb;
public Form2()
{
InitializeComponent();
Sizetb = textBox1.Width;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Sizetb);
}
}
}

Pass Textbox Value From Windows Form To Class

I am learning C# and have run into an interesting issue to me. I have a class variable defined as public and I instantiate a new instance of my form in my class and access the value of the public variable it is always null.
To further explain my issue - this syntax prints the appropriate value
System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());
However, this syntax is always outputting a 0
System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
How do I need to alter my syntax so that the correct value is passed to the class Functions?
public partial class Form1 : Form
{
public double tboxvalue;
private string exportdata;
public Form1()
{
InitializeComponent();
}
private void btnClicker_Click(object sender, EventArgs e)
{
Functions.EE();
}
private void txtData_CheckedChanged(object sender, EventArgs e)
{
bool #checked = ((CheckBox)sender).Checked;
if (#checked.ToString() == "True")
{
exportdata = "Yes";
tboxvalue = Convert.ToDouble(this.txtData.Text);
System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());
}
else
exportdata = "No";
}
}
class Functions
{
public static void EE()
{
Form1 f1 = new Form1();
System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
}
}
To access properties of the form, you need to change two Things. First you have to pass the form to the 'EE' method, then you can access the form's properties. Second, don't create a new form in 'EE' method.
public partial class Form1 : Form
{
public double tboxvalue;
private string exportdata;
public Form1()
{
InitializeComponent();
}
private void btnClicker_Click(object sender, EventArgs e)
{
Functions.EE(this);
}
private void txtData_CheckedChanged(object sender, EventArgs e)
{
bool #checked = ((CheckBox)sender).Checked;
if (#checked.ToString() == "True")
{
exportdata = "Yes";
tboxvalue = Convert.ToDouble(this.txtData.Text);
System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());
}
else
exportdata = "No";
}
}
class Functions
{
public static void EE(Form1 f1)
{
System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
}
}
If i understood your question i guess you are recreated Form1 with own textbox or labels when you click btnClicker button. You can reassign your form objects where you created it.
You might add static Form1 object and Setter routine to Functions class:
private static Form1 _form;
public static void SetForm(Form1 form)
{
_form = form;
}
and pass the form to the class in Form_Load event-click on the form twice:
private void Form1_Load(object sender, EventArgs e)
{
Functions.SetForm(this);
}
Then you can play with the form in Functions class using the object _form
good luck!

I can not call my new form in my program after i created it

I have no idea about how this error was made. Here's my code and error.
I created a new form in solution explorer and wrote these codes in my main form. I'm sure that the hitbox which I used below is right.
public partial class HomePageForm : Form
{
OptionsPageForm frmOptions;
}
private void HomePageForm_MouseClick(object sender, MouseEventArgs e)
{
if (this.homePageOptionsButtonHitBox.Contains(e.Location))
{
this.Enabled = false;
frmOptions = new OptionsPageForm(this);
frmOptions.Show();
}
}
And these are codes i wrote in my "frmOptions" - which is the form i want to call.
public partial class OptionsPageForm : Form
{
OptionsPageForm frmHomePage;
public OptionsPageForm(HomePageForm frmCreator)
{
InitializeComponent();
frmHomePage =frmCreator;
}
}
The error given by visual studio is:
Cannot implicitly convert type "My Application_.MainPageForm" to "My Application_.OptionsPageForm".
And this is another form-calling I did in this application, it has the same structure as my call to frmOptionsPage, but it works perfectly.
public partial class HomePageForm : Form
{
GamePageForm frmGame;
}
private void HomePageForm_MouseClick(object sender, MouseEventArgs e)
{
if (this.homePageStartButtonHitBox.Contains(e.Location))
{
this.Hide();
frmGame = new GamePageForm(this);
frmGame.Show();
}
}
(In Gamepage form)
public partial class GamePageForm : Form
{
HomePageForm frmHomePage;
public GamePageForm(HomePageForm frmCreator)
{
InitializeComponent();
frmHomePage = frmCreator;
}
}
I now really want to get some help, please.
I think you need to move you Mouse Click event handler into the class like this
public partial class HomePageForm : Form
{
GamePageForm frmGame;
private void HomePageForm_MouseClick(object sender, MouseEventArgs e)
{
if (this.homePageStartButtonHitBox.Contains(e.Location))
{
this.Hide();
frmGame = new GamePageForm(this);
frmGame.Show();
}
}
}

how to access winform components from another class?

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
}

Categories