I have 2 forms : Form1 and Form2
Form1 start running with Application.Run()
and Form1 have a datagridview if user Start Edit the DataGridView
i run Form2 like this in CellBeginEdit event
Form2 addStock = new Form2(productNo, stockString, this);
addStock.Activate();
addStock.Show();
After Form2 finish the task, When i click a button on Form 2 i want to get value return to Form1 and show in the DataGridView on Form1 and close the Form2
i tried to pass the Form1's object
Form2 addStock = new Form2(productNo, stockString, this);
but on Form2 i can't access the Form1's DataGridview with that object
how can i do to accomplish this?
what i suggest made a custom class and make some static members and when you are navigating to form2 jux check them.
class CustomClass
{
public static int ProductNo;
public static string StockString;
}
//these are call to form2 from form1
CustomClasss.ProductNo=12; //some value
CustomClass.StockString="Some Value";
Form2 addStock = new Form2();
addStock.Activate();
addStock.Show();
now call these custom class members in form 2 and validate according to ur need..
like
if(CustomClass.ProductNo==2 && CustomClass.StockString=="Some String")
{
//do something
}
Hope it helpx..
Add public properties to Form2. After finishing the computation whatever you are doing within Form2 set the values to those properties. Now these values are accessible within From1 with the same From2 instance you have created to show the Form2.
Now within From1 access those property values of Form2.
Form2 addStock = new Form2(productNo, stockString, this);
addStock.Activate();
addStock.Show();
//Reading the property value of Form2
string stockValue = addStock.StockStringProperty;
Related
In my StateMachine class I can use showForm("form2",null) to load a second form which loads the next form. In this next form I have a form2 class but if I try to do a showForm in this class the command is not recognized. So how do I call a form from a different class or does everything have to go through StateMachine?
Many thanks.
you can use show form2
Form form2 = new Form();
form2.show();
and you can pass an object of Statemachine (stateMachine) to Form1 then call
stateMachine.showForm("NewForm", null); // NewForm should be created in designer theme.
I have three forms in my application.
Form1, Form2, and Form3.
Form1 opens Form2 and they are both open. This is OK. I want this to happen.
Form2 has a button that opens Form3. Again, this is desired behavior.
Form3 has two buttons... One that closes Form3 and goes back to Form1 and Form2 being open. I can do this one.
The problem is the second button on Form3... I need that button to close Form3 and Form2 so that only Form1 is now open.
Any ideas on how to close multiple forms at a time without closing the application?
Build a list of all Forms in the Application.OpenForms collection that are not of type Form1; then close them:
private void btnCloseAllExceptForm1_Click(object sender, EventArgs e)
{
List<Form> formsToClose = new List<Form>();
foreach(Form frm in Application.OpenForms)
{
if (!(frm is Form1))
{
formsToClose.Add(frm);
}
}
foreach(Form frm in formsToClose)
{
frm.Close();
}
}
Alternatively, you can have a local reference to the Form2 that opened the specific Form3 and pass it in Form3's constructor:
//references the Form2 that opens me (this is Form3)
private Form2 MyForm2Instance;
//you can add this in your existing Form3's constructor, or create a new one
public Form3(Form2 frm2)
{
InitializeComponent();
MyForm2Instance = frm2;
}
Then in your Form3's button you can have:
private void button1_Click(object sender, EventArgs e)
{
//this will close only the Form2 that opened this Form3
//and it will also close this Form3
MyForm2Instance.Close();
this.Close();
}
Now all you need is to change the way you open your Form3, by using the new constructor from Form2:
Form3 frm3 = new Form3(this);
frm3.Show();
There are some controls in Form1 and a button named Filter. After the event Filter_Clicked is fired, the second form Form2 opens. There are some input controls in Form2 and a button named Search. After the Search_Clicked event is fired, the Form2 sends a query to one of the methods in Form1. Then the result is displayed in a GridView control of Form1.
My problem is that:
I defines row data for each column, but when the result is displayed in the gridview, there is only column's name to see and row are empty even not display as NULL.
I have tried calling Form2 from Form1 in two different ways and each one causes a different result and warning:
Variant 1:
/* Result: Display only column name in Form1 gridview */
public Form2()
{
InitializeComponent();
var way1 = Application.OpenForms.OfType<Form1>().Single();
}
The Search_Clicked() event looks like this:
String searchExam = comboBoxForExamFilter.SelectedItem.ToString();
String searchFilter = "select Exam_Name,Subject_Name,Marks,Status from ABC_SchoolDB.dbo.Sheet_Marks where Exam_Name= ' " + searchExam + " ' ";
way1.userSqlQueryExecutor(searchFilter);
Note:
when calling userSqlQueryExecutor(searchQuery) from Form1, it displays the table data
Don't worry about sql injection
Variant 2:
/* Result: NullReferenceException was unhandled */
private Form1 way2;
public Form2(Form callForm1)
{
InitializeComponent();
way2 = callForm1 as Form1;
}
The Search_Clicked() event looks like this:
way2.userSqlQueryExecutor(searchFilter);
How do i show all my row data in the GridView control?
I am a beginner in WPF and trying to learn,So Sorry for this type of query.
I have added a property named GetName in WPF Form1 as below:
public string GetName
{
get { return this.uname.Text; }
set { this.uname.Text = value; }
}
Trying to retrieve the GetName property in WPF Form2 as below:
public Home()
{
InitializeComponent();
Form1 mn = new Form1 ();
MessageBox.Show(mn.GetName.ToString());
}
But I am getting prompt with blank. What I am doing wrong here. Please help.
You create a new Form:
Form1 mn = new Form1 ();
Then you request the Text value of what I assume is a TextBox control to be shown in a MessageBox:
MessageBox.Show(mn.GetName.ToString());
I would not expect GetNameto return a value, as when a TextBox control is created, unless specified, it will not have a value.
Between creating the Form and showing the MessageBox, you do not show your Form, for example:
mn.Show();
You need to show your Form first, input a value into your uname control and then request its value.
Calling ToString() on GetName is redundant, just use:
MessageBox.Show(mn.GetName);
This is another question about well disposing objects from .NET. After having read a lot of different arcticles about dispose best practices (and people opinions), I was not able to get an answer for that one. I have 2 forms, Form1 and Form2.
Form1
void ShowFormButton_Click(object sender, eventargs e)
{
Form2 form = new Form2();
form.TextChanged += new eventhandler(form_TextChanged);
form.Show(this);
}
Form2
void CloseFormButton_Click(object sender, eventargs e)
{
Close();
}
When calling Close() in Form2, the Form2 should have is dispose() method call because it was opened by calling is Show() method but because Form1 has registered for the TextChanged event or Form2, will this keep Form2 from being disposed or make the process of disposing by the GC less efficient?
Thanks in advance
Form2 form = new Form2();
form.TextChanged += new eventhandler(form_TextChanged);
This means, that Form2 instance has reference to Form1 form_TextChanged method. When Form2 is closed, this doesn't prevent it to be collected, so in this case unsubscribing is not obligatory.
Let's say that Form2 subscribes to Form1 event. In this case, when Form2 is closed, Form1 still has active reference to Form2, and Form2 cannot be collected, creating memory leak.
So, the answer depends on subscription direction and event source/subscriber life time. In any case, if something is not clear, it is better to unregister events.