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".
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!
This is my form:
namespace Secretary_1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
This is my class:
namespace Secretary_1._0
{
public partial class Client
{
public static Form1 formCall = new Form1();
public static void Clients_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.clientLabel.Visible = true;
formCall.addClientButton.Visible = true;
formCall.clientListPanel.Visible = true;
formCall.clientListPanel.BringToFront();
formCall.addClientLabel.Visible = false;
formCall.clientInfoPanel.Visible = false;
}
public static void addClientButton_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.addClientLabel.Visible = true;
formCall.clientInfoPanel.Visible = true;
formCall.clientInfoPanel.BringToFront();
formCall.addClientButton.Visible = false;
formCall.clientListPanel.Visible = false;
formCall.clientAddPropertyPanel.Visible = false;
}
}
}
Edited:
My question is how do I call the Button_Click Event from the Client Class?
When the Client button is clicked in Form1 I want to call the events located in the Client Class.
Is this possible? Am I missing something? Ive searched every where but I seem not to understand.
I plan to create a lot of buttons and would like to create a class for certain buttons so that my form1.cs isnt so long.
Any help would be appreciated. Thanks in advance.
Just do this
//Set Access Modifier of that button to public or internal for same namespace
namespace Secretary_1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void StartClient()
{
var client = new Client(this);
client.RequiredMethod(); //Call here method of client
}
}
}
Now
namespace Secretary_1._0
{
public partial class Client
{
public Form1 formCall;
//Constructor
public Client(Form1 form1)
{
formCall = form1;
formCall.someButton.Click += OnSomeButtonClick;
}
public void OnSomeButtonClick(object sender, EventArgs e)
{
//Code here to on form1 button click ...
}
public static void Clients_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.clientLabel.Visible = true;
formCall.addClientButton.Visible = true;
formCall.clientListPanel.Visible = true;
formCall.clientListPanel.BringToFront();
formCall.addClientLabel.Visible = false;
formCall.clientInfoPanel.Visible = false;
}
public static void addClientButton_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.addClientLabel.Visible = true;
formCall.clientInfoPanel.Visible = true;
formCall.clientInfoPanel.BringToFront();
formCall.addClientButton.Visible = false;
formCall.clientListPanel.Visible = false;
formCall.clientAddPropertyPanel.Visible = false;
}
}
}
Edit: Follow this post to resolve issue using event.
1-open Form1.Designer.Cs
2-search about your button generated code
3-the last line in button code must be like that
this.btn_insert.Click += new System.EventHandler(this.button1_Click);
Note : button1 = your button name
4-Replace The last line i just mentioned above and add this one instead
this.btn_insert.Click += new System.EventHandler(this.Clients_Click);
Hope This will Help You :)
i have to mention this process mean passing a method to the delegate(EventHandler) Which necessary to the event to work correctly.
I assume you are using Visual Studio. Go into the solution explorer. Click to expand Form1.cs. Find the Form1 class in the tree and expand it. Click into one of your components there to cause VS to open the code for your Form1.designer.cs. At the bottom of that page should be a list of your components. All of them are private. Change the ones you need from private to internal, and you will have access to them from other classes in your program. Be warned, though, I think you might get some undocumented behavior doing this. But it will let you get at those forms objects from another class in the program.
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
}
If a Superclass has a function A() which changes a Label to "Hello World". How can I get a subclass to call A() with the same result? As of now, I get no compile error, but the text won't change!
Example code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FunctionA("Hello");
}
public void FunctionA(string s)
{
label1.Text = s;
}
private void button2_Click(object sender, EventArgs e)
{
Test t = new Test();
}
}
class Test : Form1
{
public Test()
{
FunctionA("World");
}
}
Both the Forms must be having their own Label control to display messages. You might be using one Label to show the message which is not part of displaying Form.
I am not sure what are to trying to achieve but why don't you just pass the Label control to FunctionA to modify the message this way:
public void FunctionA(ref Label lbl, string s)
{
lbl.Text = s;
}
ADDED: You can do it this way:
First creating the instance of FormA.
static void Main()
{
//...
FormA frmA = new FormA();
Application.Run(frmA);
}
Passing the instance of FormA to FormB by exposing a parameterized constructor for any manipulation in FormA from within FormB.
FormB frmB = new FormB(frmA);
//...
public partial class FormB : Form
{
public FormB()
{
InitializeComponent();
}
//parameterized constructor
public FormB(FormA obj)
{
FormA = obj;
InitializeComponent();
}
public FormA FormA { get; set; }
}
Instantiate your forms before running the main form. Assign the form1 reference to form2
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 mainForm = new Form1();
new Form2() { Form1 = mainForm }.Show();
Application.Run(mainForm);
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form1 Form1 { get; set; }
private void button1_Click(object sender, EventArgs e)
{
this.Form1.Update("World");
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Update("Hello");
}
public void Update(string text)
{
this.label1.Text = text;
}
}
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;
}