Interaction between forms - winforms

I am using visual studio 2012 (windows form application) and I have two forms, one with a label and the other with a button. I want it so that when you click the button the label on the other form goes up by one. I already have:
Label1 = Label1 + 1
I just need to know how to make the connection with the two forms. Maybe call a function?
Btw I am new to the program and script so in simple terms plz.

Here is a sample I create for you. Add Fomr2 Like this:
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form1.Instance.Controls.Find("label1", true).First().Text = "Some thing";
}
}
And Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Instance = this;
}
private static Form1 _Instance;
public static Form1 Instance
{
get { return _Instance; }
set { Instance = value; }
}
private void button1_Click(object sender, EventArgs e)
{
new Form2().Show();
}

Related

Defining eventhandlers outside MainWindow?

I am trying to define some event handlers inside auser class but im not sure how. The MainWindow needs the handler definitions.
public abstract partial class MainWindow: Window {
public User user = new User();
}
public class User {
internal void ExampleMouseEventDown(object sender, MouseEventArgs e) {
// Does stuff;
}
}
Isnt there a way to somehow make this work? Such as:
mainWindow.AddHandleDefinition += ExampleMouseEventDown();
Also tried this:
public partial class MainWindow : Window {
public MainWindow() {
User user = new User(this);
}
}
public class User
{
internal User(MainWindow w)
{
w.AddObserverButton.MouseUp += ExampleMouseEventDown;
}
internal void ExampleMouseEventDown(object sender, MouseEventArgs e)
{
// Does stuff;
}
}

Visual Studio 2012 Windows Form application

I am working on Windows Form application on Visual Studio 2012. I have 2 forms.
Add_Item_to_DB1
Add_Item_to_DB2
Both of these forms call a third form called SUBMIT. Now, based on where this form is being called from, it has to submit information to a different database. Everything else in the SUBMIT form is EXACTLY the same except, data is inserted to a different database.
Is there a way to find out where the form is being called from? Kinda new to Form applications.
Thank you
If you open the SUBMIT form with the ShowDialog() method you will be able to determine the form that opened the SUBMIT form via the Owner property. For example:
public partial class Add_Owner_To_Db_1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
var submitForm = new SUBMIT();
submitForm.ShowDialog(this);
}
}
public partial class SUBMIT : Form
{
private void SUBMIT_Load(object sender, EventArgs e)
{
//label1.Text will equal "Add_Owner_To_Db_1"
label1.Text = this.Owner.Text;
}
}
Alternatively you can expose a public property on your SUBMIT form that can be populated from the parent form. For example:
public partial class Add_Owner_To_Db_1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
var submitForm = new SUBMIT();
submitForm.ParentName = "Add_Owner_To_Db_1";
submitForm.Show();
}
}
public partial class SUBMIT : Form
{
public string ParentName { get; set; }
private void SUBMIT_Load(object sender, EventArgs e)
{
//label1.Text will equal "Add_Owner_To_Db_1"
label1.Text = ParentName;
}
}
HTH

Cannot set the value of a global variable

Im pretty new to programming, but im very eager to get more into this stuff, and in particular, c#. I have a made some code for an autotyper (spam bot if i may), only to be used as a goal for me to create. Essentially, what i want the program to do, is as following:
When i start my Form1, the global variable "_timerValue" is set to
1000
When i hit Start button, the text from the textbox on will be sent at
the interval of "_timerValue"
When i hit the Speed button, Form2 will show.
When i hit very fast, "_timerValue" is set to 5000 (testing purposes)
Form1 code:
public partial class Form1 : Form
{
static class TimerIntervalValue
{
Form2 f2 = new Form2();
TimerIntervalValue = f2._timerValue;
}
public Form1()
{
InitializeComponent();
f2._timerValue = "1000";
}
public void timer1_Tick(object sender, EventArgs e)
{
SendKeys.Send(textBox1.Text);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show(f2._timerValue);
timer1.Interval = Convert.ToInt32(f2._timerValue);
if (timer1.Enabled == false)
{
timer1.Enabled = true;
textBox1.Enabled = false;
button1.Text = ("Stop");
}
else if (timer1.Enabled == true)
{
timer1.Enabled = false;
textBox1.Enabled = true;
button1.Text = ("Start");
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
Form2 code:
public partial class Form2 : Form
{
public string TimerValue;
public string _timerValue
{
get { return TimerValue; }
set { TimerValue = value; }
}
public Form2()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2._timerValue = "5000";
}
}
I originally tried to create a Form2 instance just under "InitializeComponent();" in Form1, but that didnt seem to be accessible through the other funtions.
I just know its something very simple like im using the wrong class to create the Form2 instance or something like that ...
Anyway, thank you in advance
Just mark TimerValue and _timerValue as static. Then you don't need to use
Form2 f2 = new Form2(); or Form2 frm2 = new Form2();
anymore. In Form 1, just use Form2._timerValue instead of f2._timerValue. In Form 2, just change:
public void button1_Click(object sender, EventArgs e)
{
_timerValue = "5000";
}

Access Form1 controls from Form2

Have two forms-Form1 & Form2.Form1 has a button(btnNew),which opens Form2 on click, and is disabled.I need to enable the button again, only when Form2 is closed.User needs to use Form1 also simultaneously.This code is not enabling the button again. Where am I missing.
In Form1:
private void btnNew_Click_1(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
btnNew.Enabled = false;
}
public void EnableButton()
{
btnNew.Enabled = true;
}
In Form2:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f1 = new Form1();
f1.EnableButton();
}
You code creates a new Form1 which will be different from the one already running in your application.
You could try adding a reference to Form1 in your Form2 and operate on it's controls that way.
Give form2 a property like:
public Form ParentForm {get; set;}
And assign it form1 in your button click:
Form2 f2 = new Form2()
f2.ParentForm = this;
f2.show();
Then in your closing you should be able to do something like:
this.ParentForm.EnableButton();
Subscribe to your Form2 closing event from within the class that is instantiating it (Form1).
private void btnNew_Click_1(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Closing += f2_Closing;
f2.Show();
btnNew.Enabled = false;
}
public void f2_Closing(object sender, FormClosingEventArgs e)
{
this.EnableButton();
}
public void EnableButton()
{
btnNew.Enabled = true;
}

C# Send text from Form2 textbox to Form1 Textbox

I need some help, i want to set text from a textbox from Form2.cs to the another textbox in Form1.cs but i keep getting this error:
Error 2 An object reference is required for the non-static field, method, or property.
I'm coding in c# and i cannot find it anywhere on the internet how to do this?
you have to provide an istance of Form2 to Form1. Fore example you can pass it in the constructor like:
public Form2(Form1 frm)
and then you can call something like this
this.TextBox1.Text = frm.TextBox1.Text
I'm writing this answer only based your error code.
You try to access a non-static method without using any instance of belongs it class object. For example; this code will fail.
class Program
{
public static void Main()
{
WriteMethod();
}
public void WriteMethod()
{
Console.Writeline("Succes!");
}
}
But this code works;
class Program
{
public static void Main()
{
Program p = new Program();
p.WriteMethod();
}
public void WriteMethod()
{
Console.Writeline("Succes!");
}
}
Hope you get the main point.
Well your case is too simple, you can do too many things in order to exchange data between classes
Why don't you try saving data in another static class, or a singleton one...
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}

Resources