C# forms and textboxes - c#

How to access a textBox in form1 when I click a button from form2?
I want to write a specific text in textBox in form1 after I click a button from form2 and it closes itself.

As I guessed you can solve your issue as
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LaunchForm2();
}
private void LaunchForm2()
{
using (var form2 = new Form2())
{
form2.OnTextEnteredHandler += Form2_OnTextEnteredHandler;
form2.ShowDialog();
}
}
private void Form2_OnTextEnteredHandler(string text)
{
//This event will be fire when you click on button on form2
textBox1.Text = text;
}
}
Form2.cs
public partial class Form2 : Form
{
public delegate void TextEnteredHandler(string text);
public event TextEnteredHandler OnTextEnteredHandler;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (OnTextEnteredHandler != null)
{
OnTextEnteredHandler(textBox1.Text);
Close();
}
}
}
You need to be add textbox in form 2 as well, put text into it from form 2 then click button as shown in code.

Related

Change the form.text programmatically with a button of other form

I can't update the first form of my application. when it opens it loads all the elements, then through a button I open a second form and from that, with a button I should reload all the controls of the first form including form1.text but this does not happen. despite the marker I saw that the text variable is updated correctly, however on a graphic level it does not change.
form1:
public partial class Form1 : Form
{
public string mail { get; private set; }
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
this.Text = "table - Last Update: " + DateTime.Now.ToString();
...some other code...
}
public void updateform()
{
this.Controls.Clear();
InitializeComponent();
Form1_Load(null, null);
this.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
mail = lbl1.Text;
Form2 form2 = new Form2(mail);
form2.Show();
}
}
form2:
public partial class Form2 : Form
{
public Form2(String stringa)
{
InitializeComponent();
email = stringa;
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.updateform();
this.Close();
}
You can pass a reference to Form1, into Form2 using the Show() command. The reference can be accessed using the .Owner property.
In Form1:
private void button1_Click(object sender, EventArgs e)
{
mail = lbl1.Text;
Form2 form2 = new Form2(mail);
form2.Show(this); // <-- pass reference to Form1
}
In Form2:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = this.Owner as Form1; // <-- attempt to convert .Owner to Form1
if (f1 != null)
{
f1.updateform();
}
this.Close();
}

Refresh Form on Visual Studio 2013 [C#]

How can I refresh an open form from another form?
For example:
Form 1
Label (Modifiers = Public)
Button (To show Form 2)
Form 2
Text Box (Enter value for Label and display it on label)
Button (Sends value to Label)
I've notice that after I entered value in text box, the label is not updating after I closed form 2.
For C# Winforms, this is how I'd do it.
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 oForm = new Form2();
oForm.ChangeLabelText += ChangeLabelText;
oForm.Show();
}
private void ChangeLabelText(object sender, EventArgs e)
{
string sText = sender as string;
label1.Text = sText;
}
Form 2:
public partial class Form2 : Form
{
public event EventHandler ChangeLabelText;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sText = textBox1.Text;
ChangeLabelText(sText, null);
}
}
what if I use this method? Is there any disadvantage? or it's fine?
Form 1: Modifiers of label1 is Public
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 oForm = new Form2();
oForm.Owner = this;
oForm.Show();
}
Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
(this.Owner as Form1).label1.Text = textBox1.Text;
}
}

Sending variables from Form2 to Form1 [duplicate]

This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 1 year ago.
I wanna know how I can send variables from Form2 to Form1. I have one textbox and button in Form1 and one textbox and button in Form2. My application starts at Form1, textbox1 is empty and by clicking button Form2 will appear. In Form2 I want to write number and by clicking on the button send it to Form1 textbox.
I was trying this code, but I dont know how to solve it.
Form1 code:
public static int number;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
Form2 code
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1.number = textBox1.Text;
this.Visible = false;
}
Now I have variable called number in Form1, which contains value of Form2 Textbox, right? But how do I say: textbox1.text(Form1) = number after that action? Do I need refresh Form1 somehow?
Thanks!
I'd say a nice easy way to do this kind of thing, is via making a public event:
In form two, add an event:
public partial Class Form2
{
public event Action<string> SomethingHappened;
...
We need to fire the event on Form2 - to notify subscribers:
//On Form2
private void button1_Click(object sender, EventArgs e)
{
if(SomethingHappened != null)
SomethingHappened (textBox1.Text);
}
Then, upon creation 'subscribe' the parent form Form1 to action on the sub-form:
Form2 form = new Form2();
//Here, we assign an event handler
form.SomethingHappened += (string valueFromForm2) =>
{
//Event handled on Form1
this.Number = valueFromForm2;
};
The setup sounds kinda like a settings dialog where you can't continue in Form1 until Form2 is closed.
If this is the case, then something more like his would be appropriate in Form1:
public partial class Form1 : Form
{
private int number = 411;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Enabled = false;
this.textBox1.Text = number.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this.number);
if (f2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.number = f2.Number;
this.textBox1.Text = this.number.ToString();
}
}
}
With Form2 looking something like:
public partial class Form2 : Form
{
public Form2(int number)
{
InitializeComponent();
this.textBox1.Text = number.ToString();
}
private int number = 0;
public int Number
{
get { return this.number; }
}
private void btnOK_Click(object sender, EventArgs e)
{
int value;
if (int.TryParse(this.textBox1.Text, out value))
{
this.number = value;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
else
{
MessageBox.Show(textBox1.Text, "Invalid Integer");
}
}
}

How to get string data in main form from second from, when button on second form is clicked in C# .net?

At first I thought that it won't be a problem for me, but now I can't figure it out. So,
when I click Button1 in main form, form2 opens. Form2 is simple numeric keyboard, that user can enter some data. On form2 is also Save. When user clicks it, entered value should pass to main form and from that moment some event must happen in main form, which contains data from form2. Could you please give me some example or any kind of help? Thanks!
// code from main form to create form2
private void button1_Click(object sender, EventArgs e)
{
// Create a new instance of the Form2 class
Form2 settingsForm = new Form2();
// Show the settings form
settingsForm.Show();
string val = settingsForm.ReturnValue1;
MessageBox.Show(val);
}
//button save on form2
private void button13_Click(object sender, EventArgs e)
{
this.ReturnValue1 = "Something";
this.ReturnValue2 = DateTime.Now.ToString(); //example
this.Close();
//after this, some event should happen in main form !
}
There is a lot of solutions to do what you want; but I think one of these will resolve your problem.
1- Simple and easy: use public properties in Form2, initialize them when buttonSave get clicked, and access them in Form1:
Form2:
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
YourDate = "something";
Close();
}
public object YourDate { get; private set; }
}
Form1:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
var f2 = new Form2();
f2.ShowDialog();
var data = f2.YourDate;
}
}
2- A better way, is using events which is more flexible and professional programming friendly:
Form2:
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
}
// create an event of Action<T> which T is your data-type. e.g. in this example I use an object.
public event Action<object> SaveClicked;
// create an event invocator, to invoke event whenever you want
protected virtual void OnSaveClicked(object data){
var handler = SaveClicked;
if (handler != null)
handler(data);
}
private void button1_Click(object sender, EventArgs e){
// prepare your data here, -object, or string, or int, or whatever it is
var data = PrepareYourDataHere;
// invoke the event
OnSaveClicked(data);
Close();
}
}
Form1:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
// create an instance of Form2
var f2 = new Form2();
// add an event listener to SaveClicked event -which we have declared it in Form2
f2.SaveClicked += f2_SaveClicked;
f2.Show();
// or: f2.ShowDialog();
}
void f2_SaveClicked(object obj) {
// get data and use it here...
// any data which you pass in Form2.OnSaveClicked method, will be accessible here
}
}
UPDATE:
If you want to fire some events in form1, just after form2 closed, you can simply add a listener to Form2.FormClosed event:
// code from main form to create form2
private void button1_Click(object sender, EventArgs e) {
// Create a new instance of the Form2 class
Form2 settingsForm = new Form2();
settingsForm.FormClosed += SettingFormClosed;
// Show the settings form
settingsForm.Show();
string val = settingsForm.ReturnValue1;
MessageBox.Show(val);
}
void SettingFormClosed(object sender, FormClosedEventArgs e) {
// this method will be called automatically when form2 closed
}
here a sample how you can achieve this
//here I suppose that form1 is the mainform
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void UpdateMainForm(string updatedString)
{
//here you can update and invoke methods
//Once called you could raise events in your mainform
}
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
form2.ShowDialog();
}
}
}
Form2
public partial class Form2 : Form
{
private Form1 _mainForm1;
public Form2(Form1 mainForm1)
{
InitializeComponent();
_mainForm1 = mainForm1;
}
private void button1_Click(object sender, EventArgs e)
{
_mainForm1.UpdateMainForm( DateTime.Now.ToString());
}
}

Add listbox items in form 1 from form 2 button pushed in c#

I have a Form (form1) which has a ListBox and a Button. On clicking the button, it opens another Form (form2). In this Form I have a TexBox and a Button. On clicking this button, whatever I entered in this Textbox, should have to go into the ListBox in form1. Please help me to find out the solution.
step 1 : Set the Modifiers property of Listbox as Public
step 2 : in button click of Form1, put
Form2 fm2 = new Form2(this);
fm2.ShowDialog();
step 3: in Form2, put the following declaration at top level
private Form1 _fm1;
Also add a constructor :
public Form2(Form1 fm1)
{
_fm1 = fm1;
InitializeComponent();
}
step 4: in button click of Form2, put the following lines:
_fm1.ListBox1.Items.Add(Textbox1.Text);
this.Close(); //close the Form2
Hope this helps.
Make a static instance of form1 public static Form1 _Form1;,then you have access it from other forms with out make an instance:
I write an example:
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Form1 = this;
}
public static Form1 _Form1;
public void AddItem(object value)
{
listBox1.Items.Add(value);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 _Form2 = new Form2();
_Form2.Show();
}
}
Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1._Form1.AddItem("your item");
}
}
Make Form1 the Owner of Form 2 and then you can access Form1 from Form2:
Form1:
private void Form1Button_Clicked(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Owner = this;
f2.Show();
}
public void AddListItem(object text)
{
YourListBox.Items.Add(text);
}
Form2:
private void Form2Button_Clicked(object sender, EventArgs e)
{
Form1 f1 = (Form1)Owner;
f1.AddListItem(YourTextBox.Text;);
Close();
}
When you open the second form pass a reference to the current form:
var form2 = new Form2(this);
form2.Show();
In the constructor of form2 save the reference and use the reference in the click handler
class Form2
{
private Form1 _form1;
public Form2()
{
// ...
}
public Form2(Form1 form1):this()
{
_form1 = form1;
}
void Button_Click(object sender, EventArgs e)
{
if(_form1 != null)
{
_form1.AddValue(textBox1.Text);
}
}
}
It would be way better to define an interface that contains the method 'AddValue' and instead of using the type Form1 use the interface:
interface IAddValues
{
void AddValue(string value);
}
class Form1: IAddValues
{
// ...
public void AddValue(string value)
{
// ...
}
}
// somewhere in form1:
var form2 = new Form2(this as IAddValues);
form2.Show();
In the constructor of form2 save the reference and use the reference in the click handler
class Form2
{
private IAddValues _valueAdder;
public Form2()
{
// ...
}
public Form2(IAddValues valueAdder):this()
{
_valueAdder = valueAdder;
}
void Button_Click(object sender, EventArgs e)
{
if(_valueAdder != null)
{
_valueAdder.AddValue(textBox1.Text);
}
}
}

Categories