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.
Related
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 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;
}
}
i have a main form with a function that changes the text of a text box thats on the main form, the code is below:
main form function:
public void consoleLog(string message)
{
txtConsoleLog.Text += Environment.NewLine;
txtConsoleLog.Text += message;
txtConsoleLog.SelectionStart = txtConsoleLog.TextLength;
txtConsoleLog.ScrollToCaret();
txtConsoleLog.Refresh();
}
So now i open a new form called frm connect when i click a button like this:
private void connectToolStripMenuItem_Click(object sender, EventArgs e)
{
Form frmConnect = new FrmConnect(this);
frmConnect.Show();
}
this is the frmConnect below
public partial class FrmConnect : Form
{
private Form mainForm;
public FrmConnect(Form frmMain)
{
this.mainForm = frmMain;
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
FrmMain.Connected = true;
mainForm.consoleLog("Connected");
}
}
So when i click a button i want to call the function but its saying it doesnt contain a definition for it, also im trying to change the 'Connected' variable thats on the main form which works by just referencing the FrmMain but am i able to do that using mainForm.Connected = true?
If i change the function to public static, it will work by referencing FrmMain but then i get errors with the txtConsoleLog as i cant reference an object in a non static method or something like that, any help is appriciated
When you pass the form into your constructor, and store it as the private member variable, in both places you declare it of the base type Form. In order to use a method on the type that you defined, your parameter and variable should be of type FrmMain.
public partial class FrmConnect : Form
{
private FrmMain mainForm;
public FrmConnect(FrmMain frmMain)
{
this.mainForm = frmMain;
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
FrmMain.Connected = true;
mainForm.consoleLog("Connected");
}
}
You should change
private Form mainForm;
public FrmConnect(Form frmMain)
{
To
private FrmMain mainForm;
public FrmConnect(FrmMain frmMain)
{
which will later give you access to all of the public properties on FrmMain in your other methods in the FrmConnect class.
I hope that my question is relevant to be answered because I'm a newbie.
For example, I have two coding named with Class1.cs and Form1.cs. Basically, Class1.cs is the program where the process of image filtering is occured, meanwhile in Form1 is the program where I allow it to load an image from a file.
Is it possible for me to access Class1 from Form1 right after I click on the button to process it?
Class1.cs
public class Class1
{
public static class Class1Program
{
// My program for image filtering is starting here
}
}
Form1.cs
public partial class Form1 : Form
{
public Form1() => InitializeComponent();
private void LoadImageButton_Click(object sender, EventArgs e)
{
// My program here is to open up a file image right after I click on this button
}
private void ResultButton_Click(object sender, EventArgs e)
{
// Here is the part where I don't know how to access the program from "Class1.cs".
// I'm expecting the image that I've load before will be able to filter right after I clicked on this button
}
}
I'm wondering if there is a need to add or edit some program on the "Program.cs".
I'm hoping that my question can be answered. Thank you so much for your time.
Let me add one property and a method to the static class for make it understandable. Let the Class1Program looks like the following:
public static class Class1Program
{
public static int MyProperty{get; set;} // is a static Property
public static void DoSomething() // is a static method for performing the action
{
//my program for image filtering is starting at here
}
}
Now you can access the method and property inside the Form1 like this:
private void ResultButton_Click(object sender, EventArgs e)
{
Class1.Class1Program.MyProperty = 20; // assign 20 to MyProperty
int myint = Class1.Class1Program.MyProperty; // access value from MyProperty
Class1.Class1Program.DoSomething();// calling the method to perform the action
}
You can do one of these two things:
1.You can access data in Class1 by using static variables:
namespace MyProgram
{
class Class1
{
public static int Class1Variable;
}
}
namespace MyProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void LoadImageButton_Click(object sender, EventArgs e)
{
//Load image logic
}
private void ResultButton_Click(object sender, EventArgs e)
{
Class1.Class1Variable = 1;
}
}
}
2.You can create an instance of Class1 inside Form1:
namespace MyProgram
{
class Class1
{
public int Class1Variable;
public Class1()
{
}
}
}
namespace MyProgram
{
public partial class Form1 : Form
{
public Class1 c1;
public Form1()
{
InitializeComponent();
c1 = new Class1();
}
private void LoadImageButton_Click(object sender, EventArgs e)
{
//Load image logic
}
private void ResultButton_Click(object sender, EventArgs e)
{
c1.Class1Variable = 1;
}
}
}
I have two forms, Form1 and Form2. Form1 has a variable int x. When the program is executed, Form1 is hidden and Form2 is shown; however i need to call the variable from the existing Form1.
I know the method to call the variable by calling a new instance of Form1.
Form1 r = new Form1();
r.x = 20;
But I want to know how to do it for an already opened Form1.
Take several cases, like if there are multiple variables that are called from Form1, by several forms (Form2, Form3, Form4 etc...). Any variable can be called from Form1 by the forms. Also, forms can call variables from other forms (Like if Form1 and Form2 is open, then Form3 can call variables from Form1 AND Form2)
I know its a huge list, but would really appreciate if anyone can find a good way to implement it.
You will have to have a reference to the "already opened" form1 instance, so that you can reference the value of x on that form.
So, lets say that Form2 instantiates the hidden form1. You will have to have a reference in form2 to the form1, to reference the variable.
OK, Lets say this is the code for form2
public partial class Form2 : Form
{
private Form1 f;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
f = new Form1
{
Visible = false
};
int x = f.X;
}
}
and then code for form1
public partial class Form1 : Form
{
public int X { get; set; }
public Form1()
{
InitializeComponent();
X = 20;
}
}
and you need to ensure that the form luanched from the program class is
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
You can do it in following ways,
in my case form1 is form4 and form2 is form5. please consider, :)
//code on form4:
// this is by passing the reference of the form to other form
public partial class Form4 : Form
{
public int a { get; set; }
public int b { get; set; }
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
a = 5;
b = 6;
Form5 frm5 = new Form5();
frm5.frm4 = this;
this.Close();
frm5.Show();
}
}
// code on form5
public Form4 frm4 { get; set; }
private void Form5_Load(object sender, EventArgs e)
{
int x = frm4.a;
int y = frm4.b;
}
Also you can have a class file in which the instance of the form1 will be static, so that you can use that instance on form2 to refer to the form1's properties.
Let me know, if it does not solve your problem.
I hope it will help you. :)
Create a public property on the form itself. Have the get accessor return the form value. You can call it like this. Form1.MyProperty;
public string MyPrperty {
get {
return Form1.txtExample.text;
}
}
EDIT:
You can return a dictionary of all of those values if you have that many to return at a time. I would seriously consider rethinking your form if you have 20-40 values being filled. That sounds like a poor user experience to me.
i think there is a reason that you wouldn't try System.Properties.Settings.Default
accessible from Project Menu --> Properties in visual studio...
Thanks...
My guess is that you are looking for System.Windows.Forms.Application.OpenForms property which holds all the open forms in an array.
What you need to do is to check the type of each form and if it is equivalent to Form1 access the variable's value. Also, to access the variable outside the form you need to set its access modifier to either Public or make a corresponding property for it.
EDIT
Here is a sample code: (untested)
public partial class Form1 : Form
{
public int X;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
X = 100;
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
}
public partial class Form2 : Form
{
int local_X = 0;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
foreach(Form f in System.Windows.Forms.Application.OpenForms)
{
if(typeof(f) == typeof(Form1))
{
local_X = f.X; // access value here and set in local variable
}
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Value of X is : " + local_X); // Show alert for value of variable on button click
}
}
EDIT
Or you can use constructor overloading to accomplish this task:
public partial class Form1 : Form
{
public int X;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
X = 100;
Form2 frm = new Form2(x); // pass variable to form2, if multiple values pass int array
frm.Show();
this.Hide();
}
}
public partial class Form2 : Form
{
int local_X = 0;
public Form2()
{
InitializeComponent();
}
// Overloading of constructor
public Form2(int X) // if multiple values pass int array
{
InitializeComponent();
local_X = x; // capture value from constructor in class variable.
}
private void Form2_Load(object sender, EventArgs e)
{
// no need to iterate here for now due to overloading value get passed during initialization.
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Value of X is : " + local_X); // display value if alert box.
}
}
Let's look at the situation.You have multiple forms in your application and you want to do access several variables these forms.
My guess is,
public static class GlobalVariables
{
public static object MyVariable1 { get; set; }
public static object MyVariable2 { get; set; }
}
So you can access variables everywhere in your project.
It doesn't matter whether which form is opened or closed. Your ultimate goal is to access a member from Form1 in Form2 isn't it? If that is the case when you create an instance of your Form2, do it like this
Form1 objForm1 = new Form1();
Form2 obj = new Form2(objForm1);
so that in form 2 class looks like this
class Form2: Form
{
private Form1 form1Object;
public Form2(Form1 obj)
{
form1Object = obj;
}
private void SomeMethodInForm2()
{
//Here you can access the variable in form1 like
form1Object.PropertyNameYouWantToAccess;
}
}
The form 1 class can look like this
class Form1: Form
{
public int PropertyNameYouWantToAccess{get;}
}