From not closing when calling 2nd form after this.Close() - c#

I have a form1 that opens a form2 that opens a form3. I want to return to form1 from form3 using a button.
form1
private void form2button_Click(object sender, EventArgs e)
{
this.Hide();
form2 f2 = new form2();
f2.ShowDialog();
this.Show();
}
form2
private void form3button_Click(object sender, EventArgs e)
{
this.Close();
form3 f3 = new form3();
f3.ShowDialog();
}
private void exitbutton_Click(object sender, EventArgs e)
{
this.Close();
}
form3
private void mainmenubutton_Click(object sender, EventArgs e)
{
this.Close();
}
private void backbutton_Click(object sender, EventArgs e)
{
this.Close();
form2 f2 = new form2();
f2.ShowDialog();
}
But when in form3, after clicking the back button it shows form2 but form3 is still in the background. I have fixed this by adding this.Hide(); before this.Close();
I would like to know the logic behind why this happening.

If I understand you right then this is what you need to in order to go back to the first form:
form1:
private void form2button_Click(object sender, EventArgs e)
{
this.Hide();
Account form2 = new Account();
form2.ShowDialog();
this.Show();
}
form2:
private void form3button_Click(object sender, EventArgs e)
{
this.Hide();
form2 f2 = new form2();
f2.ShowDialog();
this.Close();
}
private void exitbutton_Click(object sender, EventArgs e)
{
this.Close();
}
form3:
private void mainmenubutton_Click(object sender, EventArgs e)
{
this.Close();
}
private void backbutton_Click(object sender, EventArgs e)
{
this.Close();
}
Also is there a difference between this.Hide(); and Hide(); ?
No, there's no any difference

Is there a way to have it appear in the same position?
form2 f1 = new form2();
f1.Location = Location; //location of any form, it also clould be f3.Location if it is exists
f1.ShowDialog();
Also is there a difference between this.Hide(); and Hide(); ?
No, there isn't.

Related

Access to SelectedPath in Form2

I have two form, FolderBrowserDialog in Form1 and I want to use SelectedPath in Form 2.
I don't know why but when I click on Form1 Button to show Form2 ,This Error will be appear :
Invalid URI: The URI is empty
I set Form Control Modifiers: Public and FolderBrowseDialog Seletedpath not be transferred to Form2
Form 1 :
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog MyFolderBrowse = new FolderBrowserDialog();
if(MyFolderBrowse.ShowDialog()==DialogResult.OK)
{
txtpath.Text = MyFolderBrowse.SelectedPath;
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
Frm2.ShowDialog();
}
Form 2 :
private void Form2_Load(object sender, EventArgs e)
{
Form1 Frm1 = new Form1();
webBrowser1.Url = new Uri(Frm1.txtpath.Text);
}
any solution...?
Welcome to StackOverflow!
The problem is, if I understand it correctly from the code you post it, is that you create a new instance of Form2 and then in the Loadevent of the Form2 you create a new instance of Form1 and the information you need is the instance of Form1 you already have.
You already have an instance of Form1. I would do it by two one of the 2 options:
Create a public property in Form2 and assign it when creating the Form2 instance
public class Form2: Form
{
//{...}
public string SelectedPath { get; set;}
//{...}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(this.SelectedPath);
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
Frm2.SelectedPath = txtpath.Text;
Frm2.ShowDialog();
}
Create an argument in the Form2 constructor and pass the selected path from Form1
public class Form2: Form
{
//{...}
private string _selectedPath;
public Form2(string selectedPath)
{
_selectedPath = selectedPath;
}
//{...}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(_selectedPath);
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2(txtpath.Text);
Frm2.ShowDialog();
}

Use method after close form in C#

I want to call a method in my class form1.cs after closing form2.cs
form1:
private void button1_Click(object sender, EventArgs e)
{
var form2= new form2();
form2.Show();
}
public void Form1Refresh()
{
//some code
}
form2:
private void button1_Click(object sender, EventArgs e)
{
...
Close();
//call refresh from form1
}
How can i used the method Refresh() after closing the form ? I try it with new form1 and call the function but this used a new object and doesnt work.
Should i pass the object of the form1 to form2 and use it or is there another solution ?
When you create and show your Form2 handle its FormClosed event.
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.FormClosed += form2_FormClosed; // This Line
form2.Show();
}
void form2_FormClosed(object sender, FormClosedEventArgs e)
{
Debug.Print("Form 2 has been closed. Call the Refresh Action");
}
This question is probably a duplicate.
You could hook the FormClosed event when you're constructing Form2 on the button press in Form1
var form2 = new Form2();
form2.Show();
form2.FormClosed += (sender, eventArgs) => { Refresh(); };
Windows Forms already contain a method called Refresh, did you intend to hide the method from the baseclass? I'd suggest giving it a different name or at least call the base.Refresh(); from your method.
Try calling the MainWindow and parse it to Form1.
Window window = Application.Current.MainWindow;
((form1)window).Refresh();
try this code
FORM1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
public void Form1Refresh()
{
///////
}
FORM2:
public Form1 f1;
public Form2(Form1 m)
{
this.f1 = m;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
f1.Form1Refresh();
}

I am working on a C# program. I have about four different forms. Exiting out of the forms doesn't stop the process. What am I doing wrong?

This is how I open a new form
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//Sets the Add New Button to Color WhiteSmoke
this.button1.BackColor = Color.WhiteSmoke;
//Hides current form (Form 1)
this.Hide();
//Displays Form 2
f2.ShowDialog();
}
This is how I close my forms. What am I doing wrong?
private void Form1_Close(object sender, FormClosingEventArgs e)
{
foreach (Form form in Application.OpenForms)
{
Environment.Exit(0);
}
}
Instead of calling Environment.Exit(0), try calling Close():
foreach (Form form in Application.OpenForms)
{
form.Close();
}
Another option:
Application.Exit();
Try putting your code in the Form1_FormClosing() Method:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (Form form in Application.OpenForms)
{
// Make sure form is visible before closing //
form.Show();
form.Close();
}
Environment.Exit(0);
}
Form1's close event is not called if the form is hidden. So don't hide the Form. In fact - never hide the form :)
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//Sets the Add New Button to Color WhiteSmoke
this.button1.BackColor = Color.WhiteSmoke;
//Hides current form (Form 1)
this.Hide(); //<-- delete this
//Displays Form 2
f2.ShowDialog();
f2.Dispose(); //<-- necessary when using ShowDialog()
}
And delete all of this:
private void Form1_Close(object sender, FormClosingEventArgs e)
{
foreach (Form form in Application.OpenForms)
{
Environment.Exit(0);
}
}
That should make the process close then you close Form1
Here, simple solution is that pass your Form1 object reference in Form2 contructor and call Form1.Close in Form2_Closing Event.
public partial class Form1 : Window
{
public Form1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Form2 f2 = new Form2(this);
this.Hide();
f2.Show();
}
}
Call Form1 close event when Form2 is closed.
public partial class Form2 : Window
{
Form1 frm;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 frm)
{
InitializeComponent();
this.frm = frm;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
frm.Close();
}
}

Add an item to a combobox with code from a second form

My problem is very simple: i have a combobox in form1, i have a button that open form2 to write into a textbox the new item to add. Here my code:
Form1:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
}
Form2:
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
combobox.Items.Add(new_item);
this.Close();
}
But the new item is not added to my comobobox.
I tried to refresh th combobox but i have the same result.
Thank you.
You need to add the item to your ComboBox after closing Form2:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
comboBox1.Items.Add(new_item); //this is missing in your code
}
But a better way would be creating a public property in Form2 to pass the string back:
public string Value { get; set; }
private void btn1_Click(object sender, EventArgs e)
{
this.Value = textBox1.Text; //pass the TextBox value to the property
this.DialogResult = DialogResult.OK; // Cancel would mean you closed/canceled the
// form without pressing OK-Button (btn1)
this.Close();
}
Than in Form1 you can access the property and add the new item:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if(f2.ShowDialog() == DialogResult.OK) //check the result
{
comboBox1.Items.Add(f2.Value);//Add the new item
}
}
Assuming combo's name is combobox.
Form1:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if (f2.ShowDialog() == DialogResult.OK)
combobox.Items.Add(f2.ItemValue);
}
Form2:
public string ItemValue {get {return textBox1.Text;} };
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
this.DialogResult = DialogResult.OK;
}

how to pass listview row data into another existing form

i want to have 2 forms in which the first form has a button that will load up form2 in a dialog form. form2 will show a listview displaying the data of a student. now i need to extract the 1st index of the selected row. once i double click on the row, form2 would close and pass the data into a textbox in form1.
i have used the code below which closes my form1 and creates a new instance of form1 in form2.
from form2:
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
Form1 wa= new Form1();
wa.loadid(cl);
wa.Show();
this.Close();
}
from form1:
private void btnReq_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.Show();
this.Close();
}
public void loadid(String ms)
{
String newstring = ms;
studentid.Text = newstring;
}
I suggest using a Dialog, it makes it very easy:
This is Form1. You instantiate and open f2 as Dialog and then wait for its result.OK
private void Button1_Click(System.Object sender, System.EventArgs e)
{
var f2 = new Form2();
if (f2.ShowDialog() == DialogResult.OK) {
studentId.Text = f2.SelectedStudentId;
} else {
studentId.Text = "Select a Student!!!!";
}
}
This in Form2, where you have created your listview and a public property to expose:
public string SelectedStudentId { get; set; }
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
SelectedStudentId = cl;
DialogResult = DialogResult.OK; //will close this dialog (form2)
}
This should work for you
In Form1 create a public variable like this:
public partial class Form1: Form
{
//New variable
public static string StudentIDVal;
Then, change the button click on Form1 to be:
private void btnReq_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
studentid.Text = StudentIDVal;
}
Then, on Form2 on the item click you can simply have:
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
Form1.StudentIDVal = cl.ToString();
this.Close();
}

Categories

Resources