For some reason my text box wont clear in c# - c#

To start of I am validating if the user wants to clear the textbox:
public void CheckSure()
{
Form2 f2 = new Form2();
f2.Visible = true;
}
Then Form2 opens and I have a selection between yes and no, I pick yes :
private void YesButton_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
this.Hide();
f1.Clear();
}
then it calls the clear method which should clear the textbox:
public void Clear()
{
TextSpace.Text = string.Empty;
}
Using breakpoints I have determined it is definitely getting to the point where it runs the line TextSpace.Text = string.Empty; but for some reason the text box does not clear?
Any help would be much appreciated.

You're creating a new instance of Form1. You need to use the current instance.
You can take advantage of the Form.Owner property when instantiating Form2:
var form2 = new Form2();
form2.Owner = this;
Then in Form2, to access Form1 you can call this.Owner.TextSpace.Clear()

Related

How can I move back and forth between two forms without loosing entered data

i have two forms which i want to move back and forth with without loosing the data that i have entered on both form, when i go back to form1 from form2 the data remains in form1, but when i go to form2 in which i have entered data before, the data are all gone, is there a solution to this?
first form:
public userform1()
{
InitializeComponent();
}
private void jThinButton1_Click(object sender, EventArgs e)
{
userform2 form2 = new userform2();
form2.Show();
this.Hide();
form2.Hide();
form2.ShowDialog();
this.Show();
second form:
private void jThinButton3_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
going back to form2 from form1 works fine, but the problem is I loose the data I have entered in form2 when I click next in form1, I want to keep the entered data in form 2, is it possible?
Encapsulate your userform2 instance in a readonly property which creates a new one if it's not created already
private userform2 _form2;
private userform2 form2
{
get
{
if (_form2 == null)
_form2 = new userform2();
return _form2;
}
}
Then use it like this
this.Hide();
form2.ShowDialog();
this.Show();
now whenever you access form2 its the same instance of userform2.
Or simply if you want to use the field only, but the instance is created when userform1 is constructed.
private userform2 form2 = new userform2();

C# Geting null value from other form [duplicate]

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.

Invoke method from another form

I'm trying to write a program that can display sql databases. I have 2 forms and i want to invoke the displaytable method(which opens a new tabpage on the main form(Form1) for every selected table in the sql database) on Form1.The 2 forms are open at the same time and the second form (From2) is supposed to be closed after the displaytable method has been invoked.
Form1:
private void openDatabaseToolStripMenuItem1_Click(object sender, EventArgs e)//File/Database/Open Database
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
data = openFileDialog1.FileName;
}
cn = "Provider=Microsoft.JET.OLEDB.4.0; Data Source =" + data;
try
{
connection = new OleDbConnection(cn);
connection.Open();
Form2 DataSelect = new Form2();
DataSelect.Show();
}
catch (Exception exceptcion)
{
MessageBox.Show("Such Error! Very Problem: "+exceptcion);
}
}
public void displaytable() // displays selected table on new tabpage (and dgv)
{
for (int i = 0; i < Form2.selectedtabscount; i++)
{
string a = database.ElementAt(i);
TabPage page = new TabPage(a);
tabControl1.TabPages.Add(page);
}
}
Fomr2(doesn't work):
private void bt_select_Click(object sender, EventArgs e)
{
selectedtabscount = checkedListBox1.CheckedItems.Count;
Form1.displaytable();
this.Close();
}
I have no idea about how to invoke the displaytable method on Form1.
Form1.displaytable(); does not work, because displaytable is an instance method. Remember that Form1 is a class, i.e. a type. You cannot call it on the type Form1, instead you must call it on an instance of it.
You can pass an instance of Form1 to Form2 through constructor injection. Add a parameter to the constructor of Form2
private Form1 _form1;
public Form2(Form1 form1)
{
InitializeComponent();
_form1 = form1;
}
private void bt_select_Click(object sender, EventArgs e)
{
selectedtabscount = checkedListBox1.CheckedItems.Count;
_form1.displaytable();
this.Close();
}
In Form1 you would create an instance of Form2 like this:
Form2 DataSelect = new Form2(this);
Form1 passes its current instance to Form2 with the this keyword.
I also noted that you have the same problem with Form2.selectedtabscount. It would make much more sense if you added a parameter to the method displaytable
public void displaytable(int selectedtabscount)
{
for (int i = 0; i < selectedtabscount; i++) {
...
}
}
and then call it like this:
_form1.displaytable(checkedListBox1.CheckedItems.Count);
You are just creating a new instance of Form1. You are not showing it, you need to call the form1.Show () or form1.ShowDialog() to show the other form.
It's nearly the same problem as in this question: I need to access a form control from another class (C#)
You have two options:
pass a reference from Form1 to Form2 and use it in your bt_select_Click method
or (the better one): define an event in Form2 and subscribe to it from Form1 (use this sample)

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();

Relationship between the two forms

Methods opening forms :
form1 --> form2 --> form3
ChecklistBox on the form1 there. How to know the form3 That is active or not?
If the forms you are referencing are MDI child forms, you could use
Form activeChild = this.ActiveMdiChild;
else you could use the following code if not using MDI child forms.
Form currentForm = Form.ActiveForm;
I understand that you are asking if form 3 is opened. If that is incorrect, please enlighten me.
There are probably dozens of ways to do so, it all depends on what you want to do.
One simple way would be to leave a flag somewhere, say in your Program.cs file:
public static bool Form3IsOpen = false;
Then:
private void Form3_Load(sender object, EventArgs e)
{
Program.Form3IsOpen = true;
}
And:
private void Form3_Close(sender object, EventArgs e)
{
Program.Form3IsOpen = false;
}
Supplemental:
You can also keep a reference to your subform:
In form1.cs:
private Form2 FormChild;
//In the function that opens the Form2:
FormChild = new Form2();
FormChild.Show();
Form2 will have something similar to retain Form3. If one form can open several, just use an array or collection.
When i usually have many different forms and only one instance to be created, i put them in dictonary and check it if there is a form.
Something like this:
public static Dictonary<string, Form> act_forms_in_app = new Dictonary<string, Form>();
now in every forms creation i do it like this
Form1 frm = new Form1();
frm.Name = "Myformname"
//set its properties etc.
frm.Load => (s,ev) { act_forms_in_app.Add(frm.Name, frm);};
frm.Load += new EventHandler(frm_Load);
frm.Disposed => (s, ev) { act_forms_in_app.Remove(frm.Name)};
//your usual form load event handler
public void frm_Load(object sender, EventArguments e)
{
...
}
somewhere where you want to check
Form frm = //Your form object
if(act_forms_in_app.ContainsKey(frm.Name))
{
//Perform as required
}

Categories