I'm trying to update a Textbox from another form with this code:
private void Button2_Click(object sender, EventArgs e)
{
Variables.revenu += Variables.LAIR * 30;
Cartel_Form.Textbox_Revenu.Text = Variables.revenu.ToString();
}
But I get this error:
An object reference is required for the non-static field, method or property 'Cartel.Form.Textbox_Revenu'
Here is the content of the Textbox in the first form:
Textbox_Revenu.Text = Variables.revenu.ToString();
In the same form I can refresh/modify the Textbox but not in another form. The textbox modifier is set on public.
Here is an example using events where the child form contains the events while the main form listens for these events. Both forms have one TextBox and one NumericUpDown control. Or as #jimi mentioned use bindings.
Main form
using System;
using System.Windows.Forms;
namespace PassingDataBetweenFormsSimple
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void ShowChildButton_Click(object sender, EventArgs e)
{
ChildForm childForm = new ChildForm();
childForm.PassingData += ChildFormOnPassingData;
childForm.PassingNumber += ChildFormOnPassingNumber;
try
{
childForm.ShowDialog();
}
finally
{
childForm.Dispose();
}
}
private void ChildFormOnPassingNumber(int value)
{
numericUpDown1.Value = numericUpDown1.Value + value;
}
private void ChildFormOnPassingData(string text)
{
FirstNameTextBox.Text = string.IsNullOrWhiteSpace(text) ?
"(empty)" :
text;
}
}
}
Child form
using System;
using System.Windows.Forms;
namespace PassingDataBetweenFormsSimple
{
public partial class ChildForm : Form
{
public delegate void OnPassingText(string text);
public event OnPassingText PassingData;
public delegate void OnPassingNumber(int value);
public event OnPassingNumber PassingNumber;
public ChildForm()
{
InitializeComponent();
}
private void PassDataButton_Click(object sender, EventArgs e)
{
PassingData?.Invoke(FirstNameTextBox.Text);
PassingNumber?.Invoke((int)numericUpDown1.Value);
}
}
}
Related
i have a form with a panel in it and when a button is presed a usercontrol showes and the panel hides, now i need to hide a button if the textbox in the form1 contains "admin" in it.
this is the form1 code
public partial class Form1 : Form
{
public string a;
public Form1()
{
InitializeComponent();
a = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
}
}
and this is the usercontrol code
public partial class afterlogin : UserControl
{
public afterlogin()
{
InitializeComponent();
Form1 f = new Form1();
if (f.a.Contains("admin"))
{
button1.Hide();
}
}
}
You're creating a new form in the user control, it will not have the same values as the original form you created your user control in.
If you wish to take in values from the form, add a constructor parameter to the "afterlogin" class with text of the textbox, such as:
public afterlogin(string text)
{
InitializeComponent();
if (text.Contains("admin"))
{
button1.Hide();
}
}
and pass the text value to the constructor of the "afterLogin" class:
afterlogin v = new afterlogin(a);
Since Form1 creates the UserControl, just have the Form itself turn on or off the Button?
You can make a method in your UserControl that allows you to change the visibility of the control:
public partial class afterlogin : UserControl
{
public void setButton(bool state)
{
button1.Visible = state;
}
}
Now you can call setButton when you create the UserControl:
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
v.setButton(!textBox1.Text.Contains("admin"));
}
I've created a new form, in which I have a toolbox. When I press a button in that form, it should relay that information that has been entered by the user(toolboxbox value) to the main form, in which it should say that piece of information in a label.
Since the method to create that username from the toolbox is private, I cannot access it from any other way. Making it public does not seem to make a difference, neither does get,set (from the way I've been trying to atleast).
Picture that may help explaining it:
Code (in which to create user):
namespace WindowsFormsApplication3
{
public partial class Newuserform : Form
{
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
string uname = textboxUsername.ToString();
}
public void Unamecreate()
{
}
}
}
Form1 Code (To receive created user):
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Aboutform form2 = new Aboutform();
form2.Show();
}
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.Show();
}
}
}
you have a lot of options.
One way is to create an event and handle it in the main form.
public partial class Newuserform : Form
{
//the public property
public event EventHandler<string> UnameChanged;
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
if (UnameChanged != null)
UnameChanged(textboxUsername.ToString()); //fire the event
}
}
Now, to "handle" the event, do the following in your main form:
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.UnameChanged += Handler;
formnewuser.Show();
}
private void Handler (object sender, string Uname)
{
// do something wit the new Uname.
}
note: recreating the Newuserform will require to cleanup previous attached resources.
I am writing an application that passes gps data from a main form to a gps form at a constant interval (using a timer).
I've used the following tutorial to make a quick test:
http://www.codeproject.com/Articles/17371/Passing-Data-between-Windows-Forms
However, when I start the code no event is triggered. First I got a nullpointer. after adding the following lines I got rid of it:
if (GpsUpdated != null)
{
GpsUpdated(this, args);
}
Main form code:
public partial class Form1 : Form
{
// add a delegate
public delegate void GpsUpdateHandler(object sender, GpsUpdateEventArgs e);
// add an event of the delegate type
public event GpsUpdateHandler GpsUpdated;
int lat = 1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form_GPS form_gps = new Form_GPS();
form_gps.Show();
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
Debug.WriteLine("Timer Tick");
// instance the event args and pass it each value
GpsUpdateEventArgs args = new GpsUpdateEventArgs(lat);
// raise the event with the updated arguments
if (GpsUpdated != null)
{
GpsUpdated(this, args);
}
}
}
public class GpsUpdateEventArgs : EventArgs
{
private int lat;
// class constructor
public GpsUpdateEventArgs(int _lat)
{
this.lat = _lat;
}
// Properties - Viewable by each listener
public int Lat
{
get
{
return lat;
}
}
}
GPS form code:
public partial class Form_GPS : Form
{
public Form_GPS()
{
InitializeComponent();
}
private void Form_GPS_Load(object sender, EventArgs e)
{
Debug.WriteLine("GPS Form loaded");
Form1 f = new Form1();
// Add an event handler to update this form
// when the ID form is updated (when
// GPSUpdated fires).
f.GpsUpdated += new Form1.GpsUpdateHandler(gps_updated);
}
// handles the event from Form1
private void gps_updated(object sender,GpsUpdateEventArgs e)
{
Debug.WriteLine("Event fired");
Debug.WriteLine(e.Lat.ToString());
}
}
Can anyone point me in the right direction? What am I doing wrong?
Thanks in advance and with best regards.
You should pass an instance of Form1 to your Form_GPS for it to work properly. See the following changes:
public partial class Form_GPS : Form
{
public Form_GPS(Form1 owner)
{
InitializeComponent();
owner.GpsUpdated += new Form1.GpsUpdateHandler(gps_updated);
}
private void Form_GPS_Load(object sender, EventArgs e)
{
Debug.WriteLine("GPS Form loaded");
}
// handles the event from Form1
private void gps_updated(object sender,GpsUpdateEventArgs e)
{
Debug.WriteLine("Event fired");
Debug.WriteLine(e.Lat.ToString());
}
}
Now you need to a small change in Form1 as well:
private void Form1_Load(object sender, EventArgs e)
{
Form_GPS form_gps = new Form_GPS(this);
form_gps.Show();
timer1.Enabled = true;
}
Notice how you pass an instance of Form1 to Form_GPS in the constructor of Form_GPS using the self reference this.
Declaring the event as following solved the problem:
public static event GpsUpdateHandler GpsUpdated;
instead of:
public event GpsUpdateHandler GpsUpdated;
In this way the Form1 event can be called static, so no new instance is necessary.
I want to call a some combo-box items so i can make an if /else statements and output a form.The combo-box items are out side of my class(Form) how can i access them i tried this(below) but the error says does not exist in the current context.I also changed it
the method from private to public
public void buttonFinish_Click(object sender, EventArgs e)
{
if(comboBoxD.Text == "Alphabet" && comboBoxType.Text == "Numbers")
{
}
}
Send ComboBox from form1 to form2 using constructor. Here is example:
Form1 Class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2(comboBox1, comboBox2);
f2.Show();
}
}
Form2 Class:
public partial class Form2 : Form
{
ComboBox comboBoxD;
ComboBox comboBoxType;
public Form2(ComboBox cb, ComboBox cbType)
{
InitializeComponent();
comboBoxD = cb;
comboBoxType = cbType;
}
private void Form2_Load(object sender, EventArgs e)
{
}
protected void buttonFinish_Click(object sender, EventArgs e)
{
if(comboBoxD.Text == "Alphabet" && comboBoxType.Text == "Numbers")
{
}
}
}
UPDATE:
Here is another approach for accessing controls present in another form.
Default Modifiers of every control is private. For controls you want to access from another form you have change Modifiers property as Public.
Form1 Class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
}
Form2 Class:
public partial class Form2 : Form
{
private Form1 f1;
public Form2(Form1 f)
{
InitializeComponent();
f1 = f;
}
protected void buttonFinish_Click(object sender, EventArgs e)
{
if(f1.comboBoxD.Text == "Alphabet" && f1.comboBoxType.Text == "Numbers")
{
}
}
}
This is because your ComboBox is only available in the codebehind file of your Form.
One solution would be to store a reference to your combobox as a property in your codebehind.
like this:
public ComboBox myCmbBox { get; private set; }
and access it in the codebehind of form2.
You can write a public method in your ComboBox's class and then call it from where you have instance of that form.
like this:
in your main form:
using (var modal = new MyModal())
{
modal.ShowDialog();
modal.getSomething();
}
in your modal:
public string getSomething()
{
return yourComboBox.Text;
}
I have two buttons in Form 1, one is "ShowForm2" button and one is "button1" button.
Button 1 is disable by default. And when I click "ShowForm2" button, Form 2 will show.
So, what I want is, when I click the "button2" in form 2, it will enable the "button1" in form 1.
So, I try to code like this in my form2 class:
public partial class Form2 : Form
{
bool enable_form1_button1;
public bool Enable_form1_button1
{
get { return this.enable_form1_button1; }
set { this.enable_form1_button1 = value; }
}
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
enable_form1_button1 = true;
}
}
Then in my Form1 class, I am expecting to get the "enable_form1_button1 = true" to pass into form 1 and enable my form 1 button1. But how to do this?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btb_Showfrm2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
button1.Enabled = frm2.Enable_form1_button1; // I put it here, and it just does not seems right
}
}
Well what you can do is, expose the button as a property and send a reference of your current form to the form that needs to take control:
Form1
public partial class Form1 : Form
{
public Button BtnShowForm2
{
get { return btnShowForm2; }
set { btnShowForm2 = value; }
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
// pass the current form to form2
Form2 form = new Form2(this);
form.Show();
}
}
Then in Form2:
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
private void btnEnabler_Click(object sender, EventArgs e)
{
// access the exposed property here <-- in this case disable it
_form1.BtnShowForm2.Enabled = false;
}
}
I hope this is what you're looking for.
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
Form2 oFrm2 = new Form2();
oFrm2.evtFrm += new ShowFrm(oFrm2_evtFrm);
oFrm2.Show();
}
void oFrm2_evtFrm()
{
button1.Enabled = true;
}
}
Form2.cs
public delegate void ShowFrm();
public partial class Form2 : Form
{
public event ShowFrm evtFrm;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (evtFrm != null)
{
evtFrm();
}
}
}
you can send a refrence of your form1 to your form2 in the from2 constructor for example :
and then in button clicked event handler in form2 call a public method in form1 to enable the button :
this could be your form1 code :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public void enableButton()
{
button2.Enabled = true;
}
this could be your form2 code :
public partial class Form2 : Form
{
private Form1 frm;
public Form2(Form1 frm)
{
InitializeComponent();
this.frm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
frm.enableButton();
}
If you'll only have one instance of Form1 you can also get away with having a static Button member in Program which references the button you want to enable from Form2.
Form1.cs:
using System;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button2.Enabled = false;
Program.button = button2;
}
private void button1_Click(object sender, EventArgs e)
{ new Form2().Show(); }
private void button2_Click(object sender, EventArgs e) { }
}
}
Form2.cs:
using System;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
public partial class Form2 : Form
{
public Form2()
{ InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{ Program.button.Enabled = true; }
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
static class Program
{
public static Button button;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
I would recommend you to use event/delegates to perform this task. This is the standard way of doing so.
Again passing reference in constructor or making static func/var/properties are just workaround and Called as bad programming.