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)
Related
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()
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();
I have dataGridView1 in form1 but for inserting in it I'm using form2 which have save button (that provides insert into datagridview1) to which I'm trying to set - refresh or select * from so I dont need any refresh button in form1 whenever I want to show up just all the recors. Is there any way to achieve that?
Thanks much for your time.
You can use delegate.. For example :
Form1 :
private void btnForm1_Click(object sender, System.EventArgs e)
{
// Create an instance of form 2
Form2 form2 = new Form2();
// Create an instance of the delegate
form2.passControl = new Form2.PassControl(PassData);
// Show form 2
form2.Show();
}
private void PassData(object sender)
{
// Set de text of the textbox to the value of the textbox of form 2
txtForm1.Text = ((TextBox)sender).Text;
}
Form 2 :
public class Form2 : System.Windows.Forms.Form
{
// Define delegate
public delegate void PassControl(object sender);
// Create instance (null)
public PassControl passControl;
private void btnForm2_Click(object sender, System.EventArgs e)
{
if (passControl != null)
{
passControl(txtForm2);
{
this.Hide();
}
}
I hope this helps..
You can use Form.DialogResult to achieve this.
In your insert form, after saving data to database:
DbContext.SaveChanges();
MessageBox.Show("Successfully saved.");
DialogResult = System.Windows.Forms.DialogResult.Yes;
and In your grid form:
Frm_NewData newData = new Frm_NewData();
if (newData.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
{
RefreshGrid();
}
private void button1_Click(object sender, EventArgs e)
{
Form2.Show();
}
I have the code above which in my opinion contains no error but it won't execute by some reason.
It says the error "An object reference is required for the non-static field, method, or property" but what I have missed?
I have just two forms (Form1 and Form2) and one button nothing more. I used a registry cleaner but the error persists.
There exists another code for it which worked, but this code makes a copy of my form as a new variable, but I would like to show the original form like it Visual Basic did.
In addition to storing a reference to your Form at Class level, you need to check if it has been closed since the last time it was used. In that case you'd need to create a new instance (just as you do for the very first use). The below example also restores the form if it was minimized:
public partial class Form1 : Form
{
Form2 F2 = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (F2 == null || F2.IsDisposed)
{
F2 = new Form2();
F2.Show();
}
else
{
if (F2.WindowState == FormWindowState.Minimized)
{
F2.WindowState = FormWindowState.Normal;
}
F2.Activate();
}
}
}
Form2 or Form1 are just a names of classes. Before using of this classes you need to create an instance of them
Form2 secondaryForm = new Form2();
After this you can use all methods and properties of that class secondaryForm.Show();
So before using/showing your Form2, you need to create an instance. If you want show
your original form
, meens that instance are already created. You need check your code where that instance was created and put reference to that form in variable:
Create a variable in Form1:
private Form2 secondaryForm;
In code where you created already your original Form2 just use this variable:
this.secondaryForm = new Form2();
After this anywhere in Form1's code you can show a Form2 with next line:
this.secondaryFomr.Show();
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
}