Append text in textbox on another form? - c#

I have 2 forms. On Form2, I have a textbox and a button. When I press the button, I want it to append the text in the textbox to a textbox on Form1. Here's my code:
On Form2:
private void button1_Click(object sender, EventArgs e)
{
frm1.AppendTxt(textBox1.Text);
this.Close();
}
On Form1:
public void AppendTxt(string text)
{
this.body.AppendText(text);
MessageBox.Show(body.Text);
}
For some reason, the text isn't showing up in the textbox on Form1. However, the message box that pops up shows the text I input on Form2.

Try body.Text += " " + text;

Most likely you're changing the textbox inside a hidden copy of Form1, not the instance that's displayed on the screen.
Try passing the Form1 handle to Form2's constructor, and saving it in a class member variable (also called field) to use inside the event handler.

Passing values from form to form.
You can try this. first you can declare a variable that all your forms can access
like this one,
string yourValue;
PS: declare that your class
From form1, you can pass a value to that class.
like this,
YourClass.yourValue = textbox1.text;
Once have a value. pass it to your second form like.
textbox2.text = YourClass.yourValue;
Hope it helps you. :D

You have two form: form1 and form2. Two form is opening. In form2 you have one textbox(txt2) and one button(btn2). In form1 you have one textbox(txt1). When user type something on txt2 and click btn2, the text in txt2 will be append to txt1. This is what you want?
If yes:
In form2, you should create a delegate:
// Declare a delegate
public delegate void GetValue(string value);
// Declare event with delegate
public event GetValue btn2_Clicked;
Then, in form2, you create a function to handle that event: Name of function is the same with name of event you just declare and add prefix "On" in it. Like this:
public void Onbtn2_Clicked(string value)
{
if (btn2_Clicked != null)
{
btn2_Clicked(value);
}
}
After that, in btn2 click event, rise your event just created:
private void btn2_Click(object sender, EventArgs e)
{
Onbtn2_Clicked(txt2.Text);
}
Ok, that is done in form2. Comeback form1 to finish:
I assume form2 is opened when user click a button(btn1) in form1, so in btn1 click event:
private void btn1_Click(object sender, EventArgs e)
{
// Create form2
Form2 frm2 = new Form2();
// Handle btn2 click
frm2.btn2_Clicked += new Form2.GetValue(frm2_btn2_Clicked);
// Show form2
frm2.Show();
}
void frm2_btn2_Clicked(string value)
{
// When btn2 is clicked, the text in txt2 will be assign to txt1
txt1.Text = value;
}
And the text will be assign to txt1 in form1

I think you have to create getters and setters for your textbox so it will be exposed as part of your form class and you can access it as a form object
first forn
public string sometext
{
get { return sometext.Text; }
set { sometext.Text = value; }
}
second form
Form1 form= new Form1();
form.sometext = "some name";
here is another short solution i found
In Form1 initiate form 2
Form2 form2 = new Form2();
form2.Owner = (Form)this;
form2.Show();
and in Form2's
this.Owner.Controls.Find("TextBox1",true).First().Text="Hi!!!!"

How about just change the txtBox in the Form2.designer.cs from private to public?
private System.Windows.Forms.TextBox textBox1;
to
public System.Windows.Forms.TextBox textBox1;
This way you can just send it like this in Form2:
Form1 frm1 = new Form1();
private void button1_Click (object sender, EventArgs e)
frm1.textBox1.AppendText(textBox2);
textBox2 being the one where you enter your text in Form2

Related

How to Focus to another form in C#

I have a program which opens two forms
and I want when I click on Form1
then Focus on Form2.
private void Form1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Focus();
}
But this doesn't work, what's wrong in my code?
EDIT:
I found already answered Here by #moguzalp at the comments
First of all that Form2 is never visible.
private void Form1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.Focus();
}
If that Form is visible though with your code, that means you need to get same reference and call Focus() against it.
EDIT:
Then you need to have a reference to that Form.
At some point you created that Form and assigned it to a vairable/field or anything like that.
You need to call Focus or Activate against it.
Example:
Inside Form1 when you create a Form2 instance:
public class Form1 : Form
{
private Form _frm2;
//That code you probably have somewhere. You need to make sure that this Form instance is accessible inside the handler to use it.
public void Stuff() {
_frm2 = new Form2();
_frm2.Show();
}
private void Form1_Click(object sender, EventArgs e)
{
_frm2.Focus(); //or _frm2.Activate();
}
}
If you can have a form opened, try finding it:
using System.Linq;
...
// Do we have any Form2 instances opened?
Form2 frm2 = Application
.OpenForms
.OfType<Form2>()
.LastOrDefault(); // <- If we have many Form2 instances, let's take the last one
// ...No. We have to create and show Form2 instance
if (null == frm2) {
frm2 = new Form2();
frm2.Show();
}
else { // ...Yes. We have to activate it (i.e. bring to front, restore if minimized, focus)
frm2.Activate();
}
If you want to Show your frm2, you should call frm2.Show(); or frm2.ShowDialog();.
Also, before 'Show' call you can set frm2.TopMost = true; if you want this form to be on the top.
So it could be:
private void Form1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.TopMost = true;
frm2.Show();
}
If you already opened the form elsewhere, then this code will not work as it's a new instance of Form2 and not the one that is opened.
You will have to keep a reference to the form that is opened, and then use Focus or might be better Activate on it.
if the form is opened from within Form1 then:
Add a field for holding current reference of Form2
Save it when showing the form.
Use it when focusing
private Form2 currentForm2;
....
this.currentForm2 = new Form2();
this.currentForm2.Show();
...
...
this.currentForm2.Activate();

Can't retrieve user's TextBox input

I have a program with two Forms:
Form 1 has a TextBox and a Button.
Form2 has a DataGridView.
Then I have a class with a constructor that accepts a string as parameter:
public SymbolData(string symbol)
{ /* Do stuff */ }
The DataGridView displays the data from a table defined in SymbolData when user clicks the button.
My problem is that when I click the button the string retrieved from the textbox is the one I inserted in its Text property regardless of what is inserted in the TextBox enter code herewhen the program is running
Here is where I create SymbolData instance:
public Form2()
{
InitializeComponent();
SymbolData sd = new SymbolData(f1.textButton1.Text);
dataGridView1.DataSource = sd.Table;
}
Can anyone help me to pass the user's input from the TextBox in my SymbolData object's constructor when I call it?
If you really want to instantiate SymbolData in Form2 then have your form2 constructor accept a string parameter and pass that argument to SymbolData constructor like below
public Form2(string form1data)
{
InitializeComponent();
SymbolData sd = new SymbolData(form1data);
dataGridView1.DataSource = sd.Table;
}
Then in your Form1 button click event get a instance of Form2 and pass textbox data
protected void btn1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this.textButton1.Text.Trim());
frm2.Showdialog();
}
You can do it multiple ways but most of the ways will require an event to be created. You can create a button on the form, when you double click it a click event is created. You then can pass your text in that way.
private void button1_Click(object sender, EventArgs e)
{
SymbolData sd = new SymbolData(f1.textButton1.Text);
}
You can also create an event on the textbox, such as Keypress, keyup, keydown, you can find those events on the properties of the text box and click the lightning bolt on the top of the panel to see the events.

C# WindowsForms - Send textbox from form1 to form2 on visual studio

i have 2 forms on visual studio,
form1 have textbox1.text
form2, have textbox2.text and btnSave
obs: form2 open when i click on another button on form 1:
Form new = new form2();
nova.Show();
how can i send textbox2 content from form2 to form1 (textbox1) clicking on btnSave ?
What code will be necessary inside this click button event.
Thanks
Try this please:
Step1: Create a constructor for form2 class as below:
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text = strTextBox;
}
Step2: Instantiate form2 class in form1’s button click event handler as below:
private void button1_Click(object sender, EventArgs e)
{
Form2 obj1 = new Form2(textBox1.Text);
obj1.Show();
this.Hide();
}
Create an event on your second form that can be fired when the form is saved:
public event Action Saved;
Then create a property on that form that allows the textbox's text to be accessed:
public string SomeTextValue //TODO: rename to meaningful name
{ get{ return textbox2.Text;} }
Then you need to fire off the Saved event when you save your form:
if(Saved != null)
Saved();
Then when you first create the form in Form1 attach an event handler to that event:
Form2 child = new Form2();
child.Saved += () => textbox1.Text = child.SomeTextValue;
child.Show();
Note that if you are also closing the second form right when you save it then you don't need a custom event, you can just utilize FormClosing instead.
researching i was able to make it work, lost some hours but now all is perfect, this is code that worked for me:
On form2:
public partial class form2 : Form
{
private string nome;
public string passvalue
{
get { return nome; }
set { nome = value; }
}
form2, button save:
private void btnSalvar_Click(object sender, EventArgs e)
{
passvalue = txtMetragemcubica.Text;
this.Hide();
}
on form1 (this button open form2):
private void btnMetragemcubica_Click(object sender, EventArgs e)
{
form2 n = new form2();
n.ShowDialog();
txtMetragem.Text = n.passvalue
}
Now it work this way: Open on form 1, then i click on button btnMetragemcubica and form2 open, then i insert values on different textbox and have result on txtMetragemcubica, when i click on save button (btnSalvar) it close form2 and send value to form1 in txtMetragem textbox.
Working perfect here, hope help another persons too.
Anyway thanks for all help

retrieving the updated value of variable A on form2 closing? C#

This is a very simple question yet I couldn't find a solution for it (I am not a pro programmer sorry if this is primitive!). In Form1 I have a variable called "A" and it's value is 1. I send this to Form2 and change the value to 2. And on Form2 closing I need to send the updated value to Form1. This last part I don't know how to do that and I need your help. How can I retrieve the updated value of variable A on form2 closing?
If you have a value that is changed by Form2, and that value is managed by Form2, you can expose it as a property of Form2, e.g.
public class Form2
{
public string MyValue
{
get { return myValue; }
}
}
and then you can retrieve it like
Form2 f2 = new Form2();
f2.ShowDialog();
string theValue = f2.MyValue;
In general you may want to check the DialogResult returned by ShowDialog() to see if the user pressed e.g. the OK or Cancel button. I'm not sure if you need that in this particular case.
UPDATE
If Form2 is not a dialog, you can instead use a callback pattern to inform Form1 that Form2 is closing to allow Form1 to retrieve any values that it needs from Form2. Alternatively you can have the callback directly supply the value you need.
Specifically, you could pass a Func<T> to Form2 that points to a callback method in Form1. Form2 would then call that Func<T> when it determines that it is closing. Here, T represents the type of variable that you want passed back to Form1.
Here's an example that assumes T is a string:
public Form2 : Form
{
public void MyCallback(string value) { /* Do something with value */
}
public Form1 : Form
{
Func<string> callback;
public Form1(Func<string> callback)
{
this.callback = callback;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (callback != null) callback(myValue);
}
}
WinForm has an Event called FormClosing. Right click on the form and choose properties, on right side of IDE you will get properties. There will be an icon like "lightning" in yellow color. You will find the FormClosing event there. Now add the code you want when form is closing
You could handle the the form2.FormClosing event on form1. There you can retrieve your value form2.B (provided that it is publicly accessible) on form1 as form2 is closing.
form2.FormClosing += OnFormBClosing;
private void OnFormBClosing(object sender, FormClosingEventArgs eventArgs)
{
A = form2.B;
}
Use from closing event
private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//update
}
MSDN Library
It would probably better if you can post your actual source code.
I suppose that you open Form2 from Form1, is it right? If true, I think it would be probably better to write something like this that trying to update form1 from form2 closing event.
Form2 form2 = new Form2();
form2.A = this.A; // here this = form1
if (DialogResult.OK == form2.ShowDialog())
{
// So here, retrieve the property from form2.
this.A = form2.A;
}

Passing values between form-With constructor-Error comes!-C#-Winforms

When i am passing values from the one form(Form1) to other form(Form2) Via constructor
and when i click button to pass value error comes NULLRefrence error was unhadled!
Please tell me what do.i search on internet same codes comes ,but the error comes.
Form2 Constructor
public Form2(string ab)
{
textBox1.Text = ab;
InitializeComponent();
}
Form1 ,On button click event
private void button1_Click(object sender, EventArgs e)
{
Form2 obj = new Form2(textBox1.Text);
obj.ShowDialog();
this.Hide();
}
textBox1 is only created by InitializeComponent.
Before you call InitializeComponent, it's null.

Categories

Resources