how to pass parameters to forms c# - c#

I seem to be able to pass the parameter to the form from the program, but then how do i access the variable in the buttons routines. I have put the two sub routines Main() and Start up which would run the form1. and then I have put the namespace of the form.
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
StartUp();
}
static void StartUp()
{
bool mode = false;
Application.Run(new Form1());
//bool playermode = GetPlayerMode();
}
namespace PencilProject
{
public partial class Form1 : Form
{
private static bool modebool;
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
modebool = true;
Close();
}
private void button2_Click(object sender, EventArgs e)
{
modebool = false;
Close();
}
}
}

You can use DialogResult to have a "return value", some sort:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
StartUp();
}
static void StartUp()
{
Form1 frm = new Form1()
Application.Run(frm);
bool mode = frm.DialogResult == DialogResult.Yes;
//bool playermode = GetPlayerMode();
}
namespace PencilProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Yes;
Close();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.No;
Close();
}
}
}
You even can assign the DialogResult value to the buttons in the designer.

Try this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var f = new Form1();
f.Mode = false;
Application.Run(f);
bool playerMode = f.Mode;
}
}
And:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool Mode = false;
private void button1_Click(object sender, EventArgs e)
{
this.Mode = true;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.Mode = false;
this.Close();
}
}
This works fine for me getting bool playerMode set based on the button I click.

Related

Use method from class in form

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".

Hide form when main form run is hide also in start up

I have a small project about manage time use on computer.
Form1:
public partial class Form1 : Form
{
private Timer t = new Timer();
public static int counter = 60;
public Form1()
{
InitializeComponent();
t.Tick += new EventHandler(Timer_Tick);
t.Interval = 1000;
t.Enabled = true;
t.Start();
Form2 TheForm2 = new Form2();
TheForm2.ShowDialog();
}
void Timer_Tick(object sender, EventArgs e)
{
counter -= 1;
if (counter==20)
{
MessageBox.Show("Time remaining "+counter.ToString());
}
}
}
And Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int userVal = int.Parse(textBox2.Text);
Form1.counter += userVal;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = Form1.counter.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
Final program:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 TheForm = new Form1();
Application.Run();
}
}
I try to make this application run on Windows start up and the Form1 is hide. But I wanna make form2 invisible also. And it just show when user excute the application. How can I solve it?
I just put this exe to folder start up to make it run on start.(I'll try to make it with Registry)
If I understand correctly, you want to control the visibility of one form from another form. To do so, use the .Visible attribute of the form. For example:
public Form1()
{
InitializeComponent();
t.Tick += new EventHandler(Timer_Tick);
t.Interval = 1000;
t.Enabled = true;
t.Start();
Form2 TheForm2 = new Form2();
TheForm2.ShowDialog();
TheForm2.Visible = false;
}
There are other issues with the way you are doing this, but I presume you will sort those out over time, or post other questions :)
Edit:
OK, I have modified your code to demonstrate how to make this work. The code compiles and runs for me, and shows how to make form2 initially invisible, then make it visible after 10 seconds have elapsed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TwoForms
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// If you do it this way you'll have to stop the application yourself
// Form1 TheForm = new Form1();
// Application.Run();
Application.Run(new Form1()); // When Form1 is closed, the application will exit.
}
}
}
Form1:
using System;
using System.Windows.Forms;
namespace TwoForms
{
public partial class Form1 : Form
{
private Timer t = new Timer();
public static int counter = 60;
public Form TheForm2;
public Form1()
{
InitializeComponent();
t.Tick += new EventHandler(Timer_Tick);
t.Interval = 1000;
t.Enabled = true;
t.Start();
this.Show(); // show Form1 just so we know it's really there
TheForm2 = new Form2();
// TheForm2.ShowDialog(); // Don't do this unless you really want a modal dialog
TheForm2.Show();
TheForm2.Visible = false; // A timer tick will later set visible true
}
void Timer_Tick(object sender, EventArgs e)
{
counter -= 1;
if (counter == 50)
TheForm2.Visible = true;
if (counter == 40)
MessageBox.Show("Time remaining " + counter.ToString());
}
}
}
Form2:
using System;
using System.Windows.Forms;
namespace TwoForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int userVal = int.Parse(textBox2.Text);
Form1.counter += userVal;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = Form1.counter.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}

How to call a method from Form2 in Form 1

I have a method in Form 2
public void set_location(int lx, int ly)
{
this.Location = new Point(lx, ly);
}
Which need to be called from Form1, when this event happens
private void Form1_LocationChanged(object sender, EventArgs e)
{
loc_x = this.Location.X;
loc_y = this.Location.Y;
}
I Initialize Form2 as a new thread
private void Form1_Load(object sender, EventArgs e)
{
Thread newThread = new Thread((ThreadStart)delegate { Application.Run(new Form2()); });
newThread.Start();
}
How do i call method set_location(x,y); from Form1 ?
Firstly, there isn't any benefit by the second thread.
The easiest way would be to pass a reference to your form1 in form2's constructor
private void Form1_Load(object sender, EventArgs e)
{
Form2 form2 =new Form2();
form2.Show(this);
}
private Form1 Parent {get;set;}
public Form2(Form1 form)
{
this.Parent = form;
}
Then you can reference its properties as normal
this.Parent.Form1_LocationChanged(this, null);
In response to your comment, maintain a reference to Form2 in your form1 also and then just call that
private void Form1_LocationChanged(object sender, EventArgs e)
{
...
this.form2.set_location(loc_x, loc_y);
}
public static void set_location(int lx, int ly)
{
this.Location = new Point(lx, ly);
}
please tell me if this work
Program.cs
static class Program
{
public static Form GlobalMainForm;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
GlobalMainForm = new Form1();
Application.Run(GlobalMainForm);
}
}
Form1.cs
public partial class Form1 : Form
{
public Form2 Form2GlobalInstance { get; set; }
public Form1()
{
InitializeComponent();
Form2GlobalInstance = new Form2();
Thread newThread = new Thread((ThreadStart)delegate { Application.Run(Form2GlobalInstance); });
newThread.Start();
Load += delegate
{
var frm2Location = Form2GlobalInstance.GetLocation();
MessageBox.Show("location.x=" + frm2Location.X + " location.y=" + frm2Location.Y);
};
}
}
Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Point GetLocation()
{
return new Point(Location.X, Location.Y);
}
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show("location.x=" + Program.GlobalMainForm.Location.X + " location.y=" + Program.GlobalMainForm.Location.Y);
}
}
Update 1:
static class Program
{
public static Form MainForm;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new Form1();
Application.Run(MainForm);
}
}
From2:
public void Form1_LocationChanged(object sender, EventArgs e)
{
loc_x =Program.MainForm.Location.X;
loc_y = Program.MainForm.Location.Y;
}
OR
Ways to run a method in a new Thread
LINK

Passing Data form two different forms

My objective is to be able to enter a value in the textbox in form1 then press enter (when pressing the enter button the value will be pass to a method called setvalue. When the switch button is pressed then the form one will hide and open form2. Form2 has two buttons, show and exit. When show is clicked i need to display a messagebox that display the data in textbox in form1 by calling the getvalue method.
Please I'm open for ideas.
This is form one
public partial class Form1 : Form
{
private int secretValue;
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Visible = true;
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
this form two
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
Add a constructor value on your form 2. It should look like this:
public Form2(int secValue)
Then you can call it on your form 1 by passing the value
Form2 frm2 = new Form2(secretValue);
Maybe you can assign it to a global variable in your form 2, so that it can be referenced by your code in all of the form. Your form two can now be:
public partial class Form2 : Form
{
int passedValue = 0;
public Form2(int secValue)
{
InitializeComponent();
passedValue = secValue;
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
MessageBox.Show(string.Format(passedValue.ToString(), "My Application", MessageBoxButtons.OK));
}
}
In my opinion, there's no need for the property you created. Always remember, the values you assign to a class will be null if you change from form to form.
Following code will not work becuase you are creating new instance of form1 and trying to get the value from there, It will only have the default vaue of int.
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
}
Simply what you can do is to have a public property defined within Form2. and set the value of this property before showing Form2,
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//settting the value to Form2's property SecrectValueOfForm2, Now this value is available within Form2
frm2.SecrectValueOfForm2 = ValueInForm1;
frm2.Visible = true;
this.Hide();
}
Plz try code as shown below:
public partial class Form1 : Form
{
private int secretValue;
Form2 frm2 = new Form2();
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
frm2 .Show();
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
frm2.Owner = this;
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
And On Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = ((Form1 )this.Owner).GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
Form2
public Int32 _txtval { get; set; }
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(_txtval.ToString());
}
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2._txtval = Convert.ToInt32(textBox1.Text);
frm2.Visible = true;
this.Hide();
}

C# listView on two window

I have a listView1 in Form1 and in Form2 a method which adds elements to listView1 of Form1.
I am getting an error that listView1 does not exist. How can I remove this error.
My code is
Form2:
public static string s;
public void button1_Click(object sender, EventArgs e)
{
s = textBox1.Text;
ListViewItem lvi = new ListViewItem(DodajWindow.s);
listView1.Items.Add(lvi);
this.Close();
}
Please use this sample Code am using 2 Forms,
Code for Form1
public delegate void ListViewAddDelegate(string text);
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void AddItem(string item)
{
listView1.Items.Add(item);
}
private void button1_Click(object sender, EventArgs e)
{
ListViewAddDelegate Del = new ListViewAddDelegate(AddItem);
Form2 ob = new Form2(Del);
ob.Show();
}
}
}
Code for Form2
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public ListViewAddDelegate deleg;
public Form2()
{
InitializeComponent();
}
public Form2(ListViewAddDelegate delegObj)
{
this.deleg = delegObj;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!textBox1.Text.Equals(""))
{
deleg(textBox1.Text);
}
else
{
MessageBox.Show("Text can not be emopty");
}
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}

Categories