C# Geting null value from other form [duplicate] - c#

I have two forms. First one has one textbox.Second one has a devexpress data grid.
I want to achieve that:
first click a button and form2 opens.
if I click a row in the data grid in form2, this value should be shown inside the textbox in form1.(form1 is already opened.)
ı m a beginner. thanks for your help.
Form1 frm1 = new Form1();
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
frm1.Show();
when I do that, a new form opens. I dont want to open a new form. Form1 is already opened. I want to add values to its textbox.

Pass form1 as a reference to form2:
In your button click handler on form 1 to open form 2
private void Button_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
form2 code
private Form1 frm1;
// constructor (pass frm1 as reference)
public Form2(Form1 frm1) {
this.frm1 = frm1;
}
//put this in your event handler for a row click in the grid
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();

you can try something like this.
When you click on frm1.SimpleButton this function will be called a show your Form2. On Form2 you have to set InnerProperty (from your datagrid?) a when you close Form2 you can use this property.
private void SimpleButton_Click(object sender, EventArgs e)
{
using (Form2 frm = new Form2())
{
frm.InnerProperty = "default text";
DialogResult result = frm.ShowDialog();
SimpleButton.Text = frm.InnerProperty;
}
}

Finally, I solved the problem.
Application.OpenForms["form1"].Controls["textBox1"].Text =
gridView1.GetFocusedRowCellValue("ID").ToString();
Thanks for help.

Related

How to change 'Enable' property of tabcontrol from Form1 on Form2, C#, Visual studio

I have Form1 and Form2 simultaneously active, Form2 has a TabControl and I want button click event on Form1 to change 'Enable' property of tabControl on Form2
TabControl on Form2 is set to tabControl1.enabled = false; and Form1 acts as a login form for Form2, so I need a Login button on Form1 to enable 'tabControl1' on Form2
I can access the property by setting private System.Windows.Forms.TabControl tabControl1; to 'Public', still, using the following on button click event on Form1 doesn't do anything.
Form2 formnew2 = new Form2();
formnew2.tabControl1.Enabled = true;
Can someone please provide a simple example to help me understand or link to a previously answered question
It seems you are using the wrong reference of Form2. You probably have an open Form2, but you mistakenly create a new instance of Form2 again here in your Form1. So changing the new instance properties has no effect on previously opened instance.
You should pass an instance of Form2 to Form1 and use it.
Also you can find running instance of Form2 using Application.OpenForms
In Form1 have a reference to Form2.
In Form2 wrap the TabControl's Enable property in a public method and call the it from Form1
In Form1:
...
Form2 form2;
public Form1()
{
// initialize and show form2
this.form2 = new Form2();
this.form2.Show();
}
...
in Form2:
...
public void EnableTabControl()
{
this.tabControl1.Enabled = true;
}
...
then in Form1 when the button is clicked:
private void btnLogin_Click(object sender, EventArgs e)
{
// verify that it was initialized
if (form2 != null)
{
this.form2.EnableTabControl();
}
}

Switching between forms and sending variable correctly

I am wondering how can I correctly switch between forms by button click event.
I have Form1 and Form2.
Form1 have: -TextBoxForm1
-ButtonForm1
Form2 have: -TextBoxForm2
-ButtonForm2
I would like to on_click ButtonForm1 event go to the Form2. Then I want to write some message to TextBoxForm2 and press ButtonForm2 it will go to the Form1 again and message from TextBoxForm2 will appear in TextBoxForm1.
Everything works fine, but I have one problem. When I close application and I wanna debug and start it again, some errors appear like:"application is already running".
Form1:
public static string MSG;
public Form1()
{
InitializeComponent();
TextBoxForm1.Text = MSG;
}
private void ButtonForm1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Hide();
//There is probably my fault but when I was trying this.Close(); everything shutted down
form2.Show();
}
Form2:
private void ButtonForm2_Click(object sender, EventArgs e)
{
Form1.MSG = TextBoxForm2.Text;
Form1 form= new Form1();
form.Show();
this.Close();
}
How can I do this correctly please? :) I am beginner, thank you!
I would not go the route of using a STATIC for passing between forms as you mention you are a beginner, but lets get the closing to work for you.
In your main form create a new method to handle an event call as Hans mentioned in the comment. Then, once you create your second form, attach to its closing event to force form 1 to just become visible again.
// Inside your Form1's class..
void ReShowThisForm( object sender, CancelEventArgs e)
{
// since this will be done AFTER the 2nd form's click event, we can pull it
// into your form1's still active textbox control without recreating the form
TextBoxForm1.Text = MSG;
this.Show();
}
and where you are creating form2
private void ButtonForm1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Closing += ReShowThisForm;
this.Hide();
form2.Show();
}
and in your second form click, you should only need to set the static field and close the form
private void ButtonForm2_Click(object sender, EventArgs e)
{
Form1.MSG = TextBoxForm2.Text;
this.Close();
}
Easy solution is to use modal form. Display Form2 temporarily, when it is displayed Form1 is invisible.
var form2 = new Form2();
this.Visible = false; // or Hide();
form2.ShowDialog(this);
this.Visible = true;
And to pass data you can define property in Form2, to example:
public string SomeData {get; set;}
Form1 has to set SomeData, which you take in Shown and display. Same property can be used to get data from Form2 (before closing).
// form 1 click
var form2 = new Form2() { SomeData = TextBoxForm1.Text; }
this.Visible = false;
form2.ShowDialog(this);
this.Visible = true;
TextBoxForm1.Text = form2.SomeData;
// form 2 shown
TextBoxForm2.Text = SomeData;
// form 2 click
SomeData = TextBoxForm2.Text;
Close();

Writing from textbox in Form2 to Datagridview in Form1

I am still new to C# so please bear with me.
I have Form1 with a DataGridView and a Button. This button opens Form2.
Form2 contains a TextBox and a Button that closes Form2.
I need to write the text from the TextBox in Form2 into the first cell of the DataGridView in Form1. In the application I am developing, there is other data already in the DataGridView in Form1.
I have uploaded the Visual Studio 2010 file here.
EDIT:
Please look at this screenshot:
Here is the code I'm using:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.dataGridView1.Rows[0].Cells[0].Value = textBox1.Text;
this.Close();
}
}
I seem to instantiating a new Form1 when I don't want to.
Appreciate the help.
You don't need Form2 to instantiate (again) the main form (Form1).
A more appropriate approach is to open the auxiliary form containing the text-box as a modal-dialog window, and let the opener form (Form1) access the text entered by the user at Form2 instance.
Here below are described the changes needed:
Form2 changes:
1.- Add a new class member to store the string that is to be introduced in the text-box textBox1.
public String textFromTextBox = null;
2.- Add code to your OK button's clic event-handler, so that you store in the new class member textFromTextBox the value introduced in the text-box:
3.- Last, in the same clic event-handling code set the DialogResult property to DialogResult.OK.
The Form2 code would look like this:
public partial class Form2 : Form
{
[...]
// This class member will store the string value
// the user enters in the text-box
public String textFromTextBox = null;
[...]
// This is the event-handling code that you must place for
// the OK button.
private void button1_Click(object sender, EventArgs e)
{
this.textFromTextBox = this.textBox1.Text;
this.DialogResult = DialogResult.OK;
}
}
Form1 changes
1.- In your button with the label "Enter Text" (that is actually missing in your code), in the Click event-handler put the code necessary to open the Form2 as a modal Dialog.
2.- Set the cell value in your data-grid accordingly by recovering the value stored in the Form2's textFromTextBox member.
3.- Finally dispose your Form2 instance.
Form2 myFormWithATextBox = new Form2();
if (myFormWithATextBox.ShowDialog(this) == DialogResult.OK)
{
this.dataGridView1.Rows[0].Cells[0].Value = myFormWithATextBox.textFromTextBox;
}
myFormWithATextBox.Dispose();
Take into account that your main form is Form1 while Form2 it is just an auxiliary form control, it should not take much control on the flow of your application, and therefore not assume the responsibility of instantiating the main form.
You can pass variable from Form to another by create another contractor that accept parameters as the following:-
1) go to form1 then create another contractor:
public Form1(string myString)
{
InitializeComponent();
if (myString != null)
dataGridView1.Rows[0].Cells[0].Value = myString;
}
2) go to form2 and under the button write this code:
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1(textBox1.Text);
frm1.ShowDialog();
}
Here you are your application after modification

How do I "execute" another windows form

I have two forms. I have a button on the first form that I want to bring up a form called form2. How would I do this?
public void OnClick(object sender, EventArgs args)
{
var form = new Form2();
form.Show();
}
YourOtherFormClass form2 = new YourOtherFormClass();
form2.Show();
This can be put in your button's on_click event handler, where YourOtherFormClass is the class of your second form. You just need to instantiate it and then call the Show() method.
Double click on the button to create it's on-click handler, and simply do
Form2 f2 = new Form2(); // or whatever the name of the class is
f2.Show();

C# windows forms child window creation with VS

Let's say I have a basic form, there's a button and when I click it, it opens a new window.
How can I do this? I tried creating a new form instance on button click event, but it gives me exception, that something is wrong.
Form frm = new Form();
frm.ShowDialog();
//frm.Show();
Or please share your code..
//assuming that ur first form is named Form1 and ur second form is Form2
//assuming that ur button is button1
//inside form1 something like this is shown
Button1.Click += new EventHandler(this.Button1_Click);
void Button1_Click(Object sender, EventArgs e){
Form2 form = new Form2();
//you do either
Form2.Show();
//or focus remains on form2 do this
Form2.ShowDialog();
}
//hope this help

Categories