Getting data from one form into another form [duplicate] - c#

This question already has answers here:
Values between windows forms in C# .NET
(2 answers)
how to access winform components from another class?
(2 answers)
Closed 8 years ago.
I have a project that contains two forms, Form1 and Form2. Form1 creates a new instance of Form2. In Form2, I am entering a number for a timer, then in Form1, I want to pull that number, set and start that timer.
However, I am unable to pull that number from Form2 into Form1 during run time. Does anyone know if it is possible to do this. Thanks for the help!

Take a look at
DialogResult.
The idea is to open form like this :
Form myForm = new Form();
if(myForm.ShowDialog() == DialogResult.OK)
{
// Access the value;
Console.out.WriteLine(myForm.TheValue);
}
In myForm you'll have something like that
private string sTheValue = null;
public string TheValue
{
get { return this.sTheValue; }
}
private void Button_Click(object sender, EventArgs e)
{
this.sTheValue = "Hello World !";
this.DialogResult = DialogResult.OK;
this.Close();
}

var form2 = new Form2(this);
// OR
// var form2 = new Form2();
// form2.Form1 = this; // A property created by you, in form1
form2.ShowDialog();
Somewhere else in form2
this.Form1.No = int.Parse(txt.Text); // No is a property in Form1 which is listening for changes...

Related

How to check if form is called from another form C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have Form1 and in that form, i have this code
Form2 frm = new Form2();
frm.ShowDialog();
So now is my question: how to know if form2 is called by the way like that
in Button event in form2?
In button event in form2, I want to check if ShowDialog() is called FROM FORM1 (NOT FROM ANOTHER FORM), if button is clicked, form2 is closed!
You can use Form.Owner Property.
Form1:
Form2 frm = new Form2();
frm.ShowDialog(this); // owner parameter
Form2:
if (this.Owner != null)
{
// Owner is not null, there is a calling form
// Do something
if (this.Owner is Form1)
{
Form1 form1 = (Form1)this.Owner; // Form1 called this form!
}
}
Please try to make more precise what you are asking.
public partial class Form2: Form
{
public static bool wasCalledFromForm1 = false;
public Form2 (bool form1Called = false)
{
InitializeComponent();
wasCalledFromForm1 = form1Called;
}
private void Button1_Click(object sender, System.EventArgs e)
{
if (wasCalledFromForm1) this.Close;
}
}
Button 1 to open Form2:
Form2 form2 = new Form2(true);
form2.ShowDialog();
From what I understand you want to know if there is a way to check if the showDialog has been called successfully or not, for that you can use the following code snippet:-
Form2 frm = new Form2();
var result = frm.ShowDialog();
if (result == DialogResult.OK)
{
// apply your logic
}
PS:- In future please compose your questions properly and carefully so that its easier for us to answer :) :) :)
A example with constructor.
form2 frm2 = new Form2(calledByFrm1: true);
frm2.ShowDialog();
// ...
class Form2 ...
{
boolean calledByForm1;
public Form2(boolean calledByForm1)
{
this.calledByForm1 = calledByForm1;
}
public Form2_Onload ....
{
if (this.calledByForm1)
{
// your logic here
}
}
}

How i can open a form and close it by one button [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I open a form but I can't close it with the same button. How I can do this?
I try doing this:
...
{
var openform = new Form2();
if (openform != null) openform.Show();
else openform.Hide();
}
It seems you have to implement the following logic:
If there's an opened Form2 instance, close it
Otherwise create and show the new Form2 instance.
If it's your case we should look for the opened Form2 instance(s) first; and only then create it with new (if required)
using System.Linq;
...
// Search: do we have opened Form2 instances?
Form2 openform = Application
.OpenForms // among all opened forms
.OfType<Form2>() // of type Form2
.LastOrDefault(); // in case we have several instances, let's choose the last one
if (null == openform) { // no Form2 instance has been found
openform = new Form2();
openform.Show();
}
else { // Instance (openform) has been found
openform.Close(); // Or openform.Hide();
}
private Form2 form2;
private void button1_Click(object sender, EventArgs e)
{
if (form2 == null || form2.IsDisposed)
{
// Either no form has been created or the last one created has been closed.
form2 = new Form2();
form2.Show();
}
else
{
form2.Close();
}
}
If you like null propagation:
if (form2?.IsDisposed == false)
{
form2.Close();
}
else
{
// Either no form has been created or the last one created has been closed.
form2 = new Form2();
form2.Show();
}
I think it will be more intuitive to the operator if the other form would be closed like all other forms in windows: press a Close button, click the upper right cross, select ALT-F4, etc.
However, if you really want to close the other form from your main-form, you should not just close it, but you should ask the other form nicely if it would be kind enough to close itself. This way you give the other form the possibility to ask the operator some questions, for instance ask if the changed items need to be saved.
private Form myForm = null;
public void OnButton1_Clicked(object sender, ...)
{
if (this.myform == null)
{ // not shown yet. Show it now:
this.myForm = new Form2()
this.myForm. properties = ...
// make sure I get notified if the Form closes in any way:
this.myForm.Closed += onMyFormClosed;
// show the form
this.myform.Show(this);
}
else
{ // ask the form nicely to close itself
this.CloseForm();
// this might (or might not) lead to the event Form.Closed
}
}
private void OnMyFormClosed(object sender, ...)
{
if (!object.ReferenceEquals(sender, this.myForm))
{ // someone else is closed. I have nothing to do with this
return;
}
// if here, my Form is closed. Save to Dispose and assign Null
this.myForm.Dispose();
this.myForm = null;
}
}
public void ShowFo
Use bool variable as a switch on button just like following and make openform global.
var openform = new Form2(); // it must be defined out
of the method and within the class as global variable.
private static bool isOpen = true;
Within Button Click method
if (isOpen) openform.Show();
else openform.Hide();
isOpen = !isOpen;
Tested Code
public partial class Form2 : Form
{
public Form2() => InitializeComponent();
Form1 openForm = new Form1();
private static bool isOpen = true;
private void button1_Click(object sender, EventArgs e)
{
if (isOpen) openForm.Show();
else openForm.Hide();
isOpen = !isOpen;
}
}
Check this one tested.
https://youtu.be/o9I77dhEvYg
Maybe something like this will help you:
Form2 openform = new Form2();
if (Application.OpenForms.OfType<Form2>().Count() > 0)
openform.Hide();
openform.Show();
Although I didn't fully understand the context of your question - you might need to adapt the code. You can also check this How to check if a windows form is already open, and close it if it is?.

How to add an item to combobox with button that is in a different 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 4 years ago.
I'm trying to add an item to combobox that is in one form and the button I'm trying to do that with is in a different form.
Form1 is the form with the combobox and Form2 is the form with the button.
This is the code I tried:
private void dodajGumb_Click(object sender, EventArgs e)
{
var frm2 = new Form1();
frm2.comboFilmovi.Items.Add(imeText.Text);
}
I also tried to create a public method in form1.cs like this:
public void AddItem(string item)
{
comboFilmovi.Items.Add(item);
}
and this code in form2.cs:
var fr2 = new Form1();
fr2.AddItem(imeText.Text + " - " + datum.Value.ToString("dd-MM-yyyy") + " - " + vrijemeText.Text);
I don't get any errors, it's just that nothing happens, no new item in combobox. Any advice?
Issue in your code is that you're trying to create new instance of Form1. You have to have constructor for Form2 that accepts Form1 instance as param. Something like this:
private Form1 _form1;
public Form2(Form1 form)
{
InitializeComponent();
_form1 = form;
}
private void dodajGumb_Click(object sender, EventArgs e)
{
_form1.comboFilmovi.Items.Add(imeText.Text);
}
Hope this helped.

How can I pass data of datagridview to textboxes in other Form?

Datagridview is located in Form2, TextBoxes in Form1.
Call the Form 2 from Form1 with Show(); where is located dataGridView and then pass this information to textboxes in Form1.
Code Sample in Form2 :
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Form1 exportar = new Form1();
exportar.textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
exportar.comboBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
exportar.textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
exportar.textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
exportar.textBox4.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
exportar.dateTimePicker1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
exportar.dateTimePicker2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[6].Value.ToString();
exportar.textBox7.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[7].Value.ToString();
exportar.textBox8.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[8].Value.ToString();
exportar.textBox9.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[9].Value.ToString();
exportar.textBox10.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[11].Value.ToString();
}
This did not work, but when I place exportar.Show() passed the information. The problem is that doubles the Form1.
You need a reference of Form1 in Form2. You can pass it in the constructor of Form2
private Form1 _form1;
public Form2 (Form1 form1)
{
_form1 = form1;
}
You create and open Form2 like this from within Form1:
var form2 = new Form2(this);
form2.ShowDialog(this);
In order to be able to access the controls of the other form, you must change their Modifer to Internal in the properties window.
then you can set the values like this:
var row = dataGridView1.CurrentRow; // This is "the row".
// No detour through the index is necessary.
_form1.textBox1.Text = row.Cells[0].Value.ToString();
_form1.comboBox1.Text = row.Cells[1].Value.ToString();
But things get simpler if you use data binding. See: A Detailed Data Binding Tutorial
1.Pass it as cunstrctor parameter:
public Form2(string text){
Textbox1. text = text;
}
and
Form2 f = new Form2("something to send to the form");
f.Show();
2.Create a public property for Form2:
public string TheText {get{return TextBox1.Text;}; set {textBox1.Text = value;};}
and then from first form:
Form2 f = new Form2();
f.TheText = "Some text";
f.Show();
Either pass the data in the constructor of your other form, if it's mandatory. Or make a public method available in your other form that allows you to set the data separately.
E.g.
public void setTextBoxData(String text) { ,etc, etc }
You can then call that method on your second form, passing the value you require from the first form.

Passing a textbox value from one form to another in windows application [duplicate]

This question already has answers here:
Passing a value from one form to another form
(9 answers)
Closed 9 years ago.
in windows application I have 2 forms: form1 containing a TextBox. I just want to use the value of the form1's textbox in form2, so in form2 I just create a object of form1 and try to access the value of textbox by writing the code:
form1 f1=new form1();
string value=f1.textbox1.text
...but the value is not coming.....please help me
Creating a new instance of Form1 will not work as it a new instance, not the one you were using. As the comments above suggest, the best option would be to create a custom constructor for Form2 and accept the textbox value there.
public Form2(string form1TextBoxValue)
{
this.ValueFromForm1 = form1TextBoxValue;
}
Therefore in Form1 you can use the value in Form2 when you instantiate it:
Form2 form2 = new Form2(this.textBox.Text);
Alternatively, you could pass the Form1 instance to Form2. This way you can access all the Form1 values if needed. This does mean that Form2 has a direct dependence on Form1 which should be avoided.
// In Form2
public Form2(Form form1Instance)
{
this.Form1Instance = (Form1)form1Instance;
}
// In Form1
Form2 form2 = new Form2(this);

Categories

Resources