Can't retrieve user's TextBox input - c#

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.

Related

How do I pass DataGridView selected row value to TextBox in another form? [duplicate]

This question already has an answer here:
Interaction between forms — How to change a control of a form from another form?
(1 answer)
Closed 3 years ago.
I am using Windows Forms on .NET 4.5.2. I have 2 Forms. Form1 has a DataGridView that contains field from database and a button that shows Form2 when clicked. Form2 has a TextBox. I want to fill it with text from one of Form1 DataGridView fields when I click a button in Form1. Is it possible? The Form2 Textbox modifier is set to public but it is still not working.
I have tried:
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Form2 fr = new Form2();
int row = DataGridView1.CurrentRow.Index;
fr.Textbox1.Text = Convert.ToString(DataGridView1[0, row].Value);
fr.Textbox2.Text = Convert.ToString(DataGridView1[1, row].Value);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 fr = new Form2();
fr.ShowDialog();
}
Your problem exists because you are using ShowDialog() method instead of Show(). From this answer on Stack Overflow:
The Show function shows the form in a non modal form. This means that you can click on the parent form.
ShowDialog shows the form modally, meaning you cannot go to the parent form.
Somehow that interacts with passing the value from one form to another, I think it happens because the first form gets blocked (paused) after ShowDialog() method therefore preventing you from copying the value from DataGridView.
If you used ShowDialog() method on purpose, then you can try to bypass this limitation somehow. For example, I managed to pass the required value from DataGridView to TextBox in a newly created Form (lets call it Form2) by using the Owner property (check this answer too) and Form.Shown event. You can try replacing your code with this piece in your button1_Click event handler (or probably just creating Shown event handler in your file with Form2 class):
Form2 fr = new Form2();
int row = DataGridView1.CurrentRow.Index;
fr.Shown += (senderfr, efr) =>
{
// I did null check because I used the same form as Form2 :)
// You can probably omit this check.
if (fr.Owner == null) return;
var ownerForm = (Form1)fr.Owner;
fr.Textbox1.Text = ownerForm.DataGridView1[0, row].Value.ToString();
fr.Textbox2.Text = ownerForm.DataGridView1[1, row].Value.ToString();
};
fr.ShowDialog(this);
P.S. Why would you use Convert.ToString() instead of simply calling ToString() method on Value property like I did in the example?
First you should do the Form1's datagridview modifiers public.
When you click the button from Form1, open the Form2 and write this code to Form2_Load().
Form1 frm = (Form1)Application.OpenForms["Form1"];
int row = frm.DataGridView1.CurrentRow.Index;
Textbox1.Text = Convert.ToString(frm.DataGridView1[0, row].Value);
Textbox2.Text = Convert.ToString(frm.DataGridView1[1, row].Value);
This should work.
Form2 fr = new Form2();
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int row = DataGridView1.CurrentRow.Index;
fr.Textbox1.Text = Convert.ToString(DataGridView1[0, row].Value);
fr.Textbox2.Text = Convert.ToString(DataGridView1[1, row].Value);
}
private void button1_Click(object sender, EventArgs e)
{
fr.ShowDialog();
}

how to getting datagridview datas for another windows form in c#

I have two windows from. Form-1 has a data_grid_view and Add button. Form-2 has text boxes. I want that when I select a data from data_grid_view and click Add button then this data come to the text box in Form-2 form with the help of c#.
You could fill the textboxes directly before you show the form:
Form2 NewForm2 = new Form2();
NewForm2.TextBox0.Text = DataGridView1.CurrentRow.Cells[0].Value.ToString();
NewForm2.TextBox1.Text = DataGridView1.CurrentRow.Cells[1].Value.ToString();
NewForm2.Show();
one option is to pass the information into Form2 constructor from Form1
in Form 1 you will add the below code in the button click event. from the designer you you can double click the button to create the even for you automatically, usually named the name of you button with "_Click" appended to it:
protected void btnInFormOne_Click(object sender, EventArgs e)
{
data1 = dataGridView1.Rows[1].Cells[2].Value.ToString();
data2 = dataGridView1.Rows[1].Cells[3].Value.ToString();
Form2 frm2 = new Form2(data1,data2);
frm2.show();
}
in Form 2:
public Form2(string data1, string data2) //this is your constructor
{
txtBox.Text = data1;
txtBoxName.Text = data2;
}
i am assuming that you have already parsed the data in your datagridview into variables.
you need to tell exactly where is not working and post what you have done so far. if this helps, mark as answer.

Open form2 from two buttons (form1) and matching button that was clicked

I have a little issue, I have form1 in which I got button1 and button2 and I got form2 which I'm able to open with both buttons. Button1 serves as opening form2 and inserting details into SQL DB which can be seen in form1 datagridview. Button2 opens the same form2 but it selects data from form1 and automatically is filling them into textboxes in form2 - it is edit-like.
When I created the button2 (edit button) a problem occured, because form2 didn't knew from which button the was opened.
I thought that everytime I open the form2 I should pass integer so when form2 is loaded it should decide from which button it was opened and act according to that.
Would someone help me solve this thing out?
Thanks
You need to change the constructor of your form 2 to open your form in a different "mode"
Just like this :
Form2.cs
public Form2(bool fromButton2)
{
InitializeComponent();
//Do whatever with that bool
}
And you open your form like this :
Form1.cs
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(false);
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(true);
frm.Show();
}
Then you can apply your logic with the fromButton2 bool
personally, rather than passing the button or text or a bool I would be explicit and create an enum - pass this to the constructor so you know if you're in editing or display mode. (This covers you if new 'modes' become a requirement) E.g.
public enum EditingType
{
Display,
Editing
}
public class Form2
{
private EditingType _editingType;
public Form2(EditingType editingType)
{
_editingType = editingType;
}
public void DoSomething()
{
if (_editingType == EditingType.Display)
{
// display mode
}
if (_editingType == EditingType.Editing)
{
// editing mode
}
}
}
and to call - Form2 form2 = new Form2(EditingType.Editing);
(passing in editing or display depending on which button click you're handling)
You should create a) a new constructor, which takes button reference ( better name, or whatever You can put into their unused property "Tag" to identify them )
or b ) a public method, which You call before opening the form ( but after instantiating it ) or c) a property in form2, which can take anything You would decide to use as "thing to diffentiate.
Ok ?
Define a new constructor in form2 that takes a string, name of the calling button, as a paremeter and from buttons send the name of the button to the form2 as a parameter and in form2 check the name ButtonName pararmeter for detecting the caller button.

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

Append text in textbox on another form?

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

Categories

Resources