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;
}
}
}
Related
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.
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);
}
}
}
firstly this is my first post here so sorry for bad format and yada yada yada. Now, I'm trying to learn C# for my uni classes and I have a problem with some basic windows forms and c# logic so if you could help me out I would really appreciate it!
So, I created my form and it has 2 text boxes and a button. Uppon pressing the button, I want the program to create a new class member for a list. (think i got the explanation a bit wrong in the end so I'll link my code here). So basically here we go:
Main program :
namespace AddPersonTest
{
public static class Program
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
List<Person> Persoane = new List<Person>();
}
}
}
Persons class:
namespace AddPersonTest
{
public class Person
{
public int cod;
public int sex;
Person (int nCod, int nSex)
{
cod = nCod;
sex = nSex;
}
}
}
Button and textboxes code:
namespace AddPersonTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void lblCod_Click(object sender, EventArgs e)
{
}
public void btnAdd_Click(object sender, EventArgs e)
{
Persoane.Add(txtCod, txtSex);
}
private void txtSex_TextChanged(object sender, EventArgs e)
{
}
private void txtCod_TextChanged(object sender, EventArgs e)
{
}
}
}
In case you wanted to have the persons added on click to be static throughout your application, you could do like the below. You have created the list inside the main method which is not the right place to maintain your application state. Use the below class and on buttonclick, just call PersonStore.AddPerson(....);
public static class PersonStore {
private static List<Person> persons = new List<Person>();
public static void AddPerson(Person p) {
persons.Add(p);
}
public static List<Person> GetAllPersons() {
return persons;
}
}
In case you wanted to have these handled with services you could just do that with the help of service classes and Data Access layer classes which can be written if your requirement is to store in DB.
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!
{
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
public Button ButtonName { get { return } }
public static int initFaggleCount;
private void button1_Click(object sender, EventArgs e)
{
int faggleCount = initFaggleCount++;
string finalCalc = faggleCount.ToString();
label1.Text = finalCalc;
/*
Console.WriteLine(faggleCount);
Console.ReadLine();*/
}
private void button2_Click(object sender, EventArgs e)
{
/*TextWriter tw = new StreamWriter("SavedFaggleCount.txt");
tw.WriteLine();
tw.Close();*/
Console.WriteLine(faggleCount);
Console.ReadLine();
}
}
}
I would like the integer faggleCount to be accessible from button2 so that I can successfully Console.WriteLine(fagleCount); from button2. I'm a noob and any help is appreciated. Thanks!
instead of declaring local variable inside method body you can declare instance variable so that all members of class can access it.
public partial class form1 : Form
{
int faggleCount; //declare instance variable.
public form1()
{
InitializeComponent();
}
public Button ButtonName { get { return } }
public static int initFaggleCount;
private void button1_Click(object sender, EventArgs e)
{
faggleCount = initFaggleCount++; //use instance variable
string finalCalc = faggleCount.ToString();
label1.Text = finalCalc;
/*
Console.WriteLine(faggleCount);
Console.ReadLine();*/
}
private void button2_Click(object sender, EventArgs e)
{
/*TextWriter tw = new StreamWriter("SavedFaggleCount.txt");
tw.WriteLine();
tw.Close();*/
Console.WriteLine(faggleCount); //use instance variable
Console.ReadLine();
}
}
In your code, faggleCount is local to the method i.e. only code in the method can access it. What you need to do is to move the variable to the class-level. like this:
public class form1 : Form {
int faggleCount;
//your other code here
}
As you can see, the variable is now in the class, not in the method. This way all the methods in the class can access it, and even a inner class can access it too!
This problem is very common among beginners. Understanding the scope of the variable is pretty hard. In short, A variable in a class can be accessed in the class, a variable in a method can be accessed in the method.