Windows form opening other forms - c#

What I want
I am creating an application which has two functionalities. Both functionalities have their own form (called FactuurForm and VerhuurForm). I have another Form called Home, which has, among others, two buttons. Depending on which button is clicked, I wish to open one of the two forms, and complete close the Home-form.
What I have
Currently, I have the following code:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Home home = new Home();
home.ShowDialog();
if (home.kiesFactuur)
{
FactuurForm factuur = new FactuurForm();
home.Close();
factuur.ShowDialog();
}
else if (home.kiesVerhuur)
{
VerhuurForm verhuur = new VerhuurForm();
home.Close();
verhuur.ShowDialog();
}
}
}
kiesFactuur and kiesVerhuur are booleans which in my Home class, initialized as false. As soon as I click on of the buttons, the corresponding boolean will flip to true, triggering the if-statements to close the home-form and open the new form.
My question
Altough my current codes works, it seems a bit much for such a simple functionality. I feel like I wouldn't need the booleans and this go all be done easier. So is there an easier/better way to do this?
I've also thought about creating multiple Main functions. Clicking a button would activate the corresponding new Main function and terminate the current Main. Is this even possible and if so, is it a good solution?

I don't exactly understand the need to completely close the home form. I'd just place 2 eventhandlers for each of the buttons and call the following code on them. The first form will be hidden and closed when you close your subform.
private void ShowSubDialog(Form form)
{
this.Hide(); //makes your main form invisible before showing the subform
form.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
ShowSubDialog(new FactuurForm());
Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
ShowSubDialog(new VerhuurForm());
Dispose();
}

private void Factuur_Click(object sender, EventArgs e) {
LoadForm(new FactuurForm());
}
private void Verhuur_Click(object sender, EventArgs e) {
LoadForm(new VerhuurForm());
}
private void LoadForm(Form f) {
this.Hide();
f.ShowDialog();
this.Show();
}
Add this to your Home form, remove everything after home.ShowDialog() from Main, and make Facturr_Click and Verhurr_Click handle their respective button's click events. This will allow Home to hide/show automatically.

You should replace your code like this :
if (home.kiesFactuur)
{
FactuurForm factuur = new FactuurForm();
factuur.Show();
this.Hide();
}
else if (home.kiesVerhuur)
{
VerhuurForm verhuur = new VerhuurForm();
verhuur .Show();
this.Hide();
}
In the VerhuurForm and FactuurForm you may ovveride the event of closure like this :
public VerhuurForm()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(VerhuurForm_FormClosed);
}
void FormClosedEventHandler(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
To be sure that your application is closed if you close the form because the Home still active but hidden.

Related

C# Windows Forms application showing background when opening new form

I am currently making a full-screen application in C# but I have a bit of a problem. I have a overview form which shows buttons. When you click a button it will open a new form. (Overview form and new form are both full-screen). The problem is when the new form opens it will show nothing for a second (It doesnt really look to good in a full screen application). So does anyone know how to fix this? You can find the code here:
Code for opening new form:
void NewForm(Form form)
{
form.FormClosing += new FormClosingEventHandler(form_Closed);
form.Show();
Hide();
}
void form_Closed(Object sender, EventArgs e)
{
Show();
}
One of the opened forms:
public AdminPanelForm()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}
private void AdminPanelForm_Shown(object sender, EventArgs e)
{
List<User> users = User.GetAllUsers();
foreach(User user in users)
{
ListViewItem lvi = new ListViewItem(user.UserName);
lvi.SubItems.Add(user.Role);
listUsers.Items.Add(lvi);
}
}
private void btnBack_Click(object sender, EventArgs e)
{
Close();
}
Thanks in Advance!
After form.Show() add form.Update(). Otherwise, the real form redraw will be done with delay. It may be better to call Hide first.

How to reload form in c# when button submit in another form is click?

I have a combo box in my C# which is place in form named frmMain which is automatically fill when I add (using button button1_Click) a product in my settings form named frmSettings. When I click the button button1_Click I want to reload the frmMain for the new added product will be visible.
I tried using
frmMain main = new frmMain();
main.Close();
main.Show();
I know this code is so funny but it didn't work. :D
This is windows form!
EDIT
Please see this image of my program for better understanding.
This is my frmMain
Here is what my settings frmSettings form look like. So, as you can see when I click the submit button I want to make the frmMain to reload so that the updated value which I added to the settings will be visible to frmMain comboBox.
Update: Since you changed your question here is the updated version to update your products
This is your products form:
private frmMain main;
public frmSettings(frmMain mainForm)
{
main = mainForm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
main.AddProduct(textBox1.Text);
}
It will need the mainform in the constructor to pass the data to it.
And the main form:
private frmSettings settings;
private List<string> products = new List<string>();
public frmMain()
{
InitializeComponent();
//load products from somewhere
}
private void button1_Click(object sender, EventArgs e)
{
if (settings == null)
{
settings = new frmSettings(this);
}
settings.Show();
}
private void UpdateForm()
{
comboBoxProducts.Items.Clear();
comboBoxProducts.Items.AddRange(products.ToArray());
//Other updates
}
public void AddProduct(string product)
{
products.Add(product);
UpdateForm();
}
You then can call UpdateForm() from everywhere on you form, another button for example.
This example uses just a local variable to store your products. There are also missing certain checks for adding a product, but I guess you get the idea...
this.Close();
frmMain main = new frmMain();
main.Show();
There is no such built in method to set all your values as you desire. As i mentioned in the comment that you should create a method with your required settings of all controls, here is the sample code:
private void ReloadForm()
{
comboBox.ResetText();
dataGridView.Update();
//and how many controls or settings you want, just add them here
}
private void button1_Click(object sender, EventArgs e)
{
ReloadForm(); //and call that method on your button click
}
Try out this code.
this.Refresh();
Application.Doevents();
this.Refresh();
Refresh();
this.Hide();
frmScholars ss = new frmScholars();
ss.Show();
you want to Invalidate the form
http://msdn.microsoft.com/en-us/library/598t492a.aspx
IF you are looking to refresh page from usercontrol .Here is expample where i amrefreshing form from usercontrol
Find the form in which this reload button is.
Then Call invalidiate tab control and refresh it.
Dim myForm As Form = btnAuthorise.FindForm()
For Each c As Control In myForm.Controls
If c.Name = "tabControlName" Then
DirectCast(c, System.Windows.Forms.TabControl).Invalidate()
DirectCast(c, System.Windows.Forms.TabControl).Refresh() 'force the call to the drawitem event
End If
Next
Not required reload for entire form. Just create a function for form initialise.
you can call this function any time. This will refresh the form.
private void acc_Load(object sender, EventArgs e)
{
form_Load();
}
public void form_Load()
{
// write form initialise codes example listView1.Clear();...
}
private void button1_Click(object sender, EventArgs e) //edit account
{
//Do something then refresh form
form_Load();
}
If you want to automatically update the value of the other form when you click the button from another one you can use timer control. Just set the timer to 0.5s in order to update the form fast
I think that, by calling the frmMain_load(sender,e) when you are clicking the button should reload the form.
You may also try to Invalidate() the form just like #Nahum said.

How to close form

Ok, so a Windows Forms class, WindowSettings, and the form has a "Cancel"-button. When the user clicks the button, the dialog DialogSettingsCancel will pop-up up and ask the user if he is sure he wants to perform the action. The dialog has 2 buttons, a "Yes"-button and a "No"-button. If the user clicks the "Yes"-button, I want both DialogSettingsCancel and WindowSettings to be closed.
My button_Click event handler in DialogSettingsCancel:
private void button1_Click(object sender, EventArgs e)
{
//Code to trigger when the "Yes"-button is pressed.
WindowSettings settings = new WindowSettings();
this.Close();
settings.Close();
}
When I run my application, and go to the settings form, and click the "Cancel"-button, and then click the "Yes"-button, only DialogSettingsCancel closes without closing WindowSettings.
Why won't it work?
I've also tried changing
this.Close();
settings.Close();
to
settings.Close();
this.Close();
But still the same result.
You need the actual instance of the WindowSettings that's open, not a new one.
Currently, you are creating a new instance of WindowSettings and calling Close on that. That doesn't do anything because that new instance never has been shown.
Instead, when showing DialogSettingsCancel set the current instance of WindowSettings as the parent.
Something like this:
In WindowSettings:
private void showDialogSettings_Click(object sender, EventArgs e)
{
var dialogSettingsCancel = new DialogSettingsCancel();
dialogSettingsCancel.OwningWindowSettings = this;
dialogSettingsCancel.Show();
}
In DialogSettingsCancel:
public WindowSettings OwningWindowSettings { get; set; }
private void button1_Click(object sender, EventArgs e)
{
this.Close();
if(OwningWindowSettings != null)
OwningWindowSettings.Close();
}
This approach takes into account, that a DialogSettingsCancel could potentially be opened without a WindowsSettings as parent.
If the two are always connected, you should instead use a constructor parameter:
In WindowSettings:
private void showDialogSettings_Click(object sender, EventArgs e)
{
var dialogSettingsCancel = new DialogSettingsCancel(this);
dialogSettingsCancel.Show();
}
In DialogSettingsCancel:
WindowSettings _owningWindowSettings;
public DialogSettingsCancel(WindowSettings owningWindowSettings)
{
if(owningWindowSettings == null)
throw new ArgumentNullException("owningWindowSettings");
_owningWindowSettings = owningWindowSettings;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
_owningWindowSettings.Close();
}
You can also close the application:
Application.Exit();
It will end the processes.
new WindowSettings();
You just closed a brand new instance of the form that wasn't visible in the first place.
You need to close the original instance of the form by accepting it as a constructor parameter and storing it in a field.
Why not use the DialogResult method to close the form?
if(DialogSettingsCancel.ShowDialog() == DialogResult.Yes)
{
//this will close the form but will keep application open if your
//application type is "console" in the properties of the project
this.Close();
}
For this to work however you will need to do it inside your "WindowSettings" form while you call the DialogSettingsCancel form. Much the same way you would call the OpenFileDialog, or any other Dialog form.
Your closing your instance of the settings window right after you create it. You need to display the settings window first then wait for a dialog result. If it comes back as canceled then close the window. For Example:
private void button1_Click(object sender, EventArgs e)
{
Settings newSettingsWindow = new Settings();
if (newSettingsWindow.ShowDialog() == DialogResult.Cancel)
{
newSettingsWindow.Close();
}
}
send the WindowSettings as the parameter of the constructor of the DialogSettingsCancel and then on the button1_Click when yes is pressed call the close method of both of them.
public class DialogSettingsCancel
{
WindowSettings parent;
public DialogSettingsCancel(WindowSettings settings)
{
this.parent = settings;
}
private void button1_Click(object sender, EventArgs e)
{
//Code to trigger when the "Yes"-button is pressed.
this.parent.Close();
this.Close();
}
}
for example, if you want to close a windows form when an action is performed there are two methods to do it
1.To close it directly
Form1 f=new Form1();
f.close(); //u can use below comment also
//this.close();
2.We can also hide form without closing it
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
int flag = 0;
string u, p;
u = textBox1.Text;
p = textBox2.Text;
if(u=="username" && p=="pasword")
{
flag = 1;
}
else
{
MessageBox.Show("enter correct details");
}
if(flag==1)
{
f2.Show();
this.Hide();
}
}
There are different methods to open or close winform.
Form.Close() is one method in closing a winform.
When 'Form.Close()' execute , all resources created in that form are destroyed.
Resources means control and all its child controls (labels , buttons) , forms etc.
Some other methods to close winform
Form.Hide()
Application.Exit()
Some methods to Open/Start a form
Form.Show()
Form.ShowDialog()
Form.TopMost()
All of them act differently , Explore them !

How to open recently closed or hide form

I've researched thoroughly but still i cant find the best solution for this..
I have a 3 buttons, BACK - HOME - FORWARD ..
This is just like the buttons on the upper left of browser .. and im trying to do this on a form..
what i have is this ..
the back button code is
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
}
// simply hiding the form .. so that the previous form will be shown..
the home button code is this..
private void button2_Click(object sender, EventArgs e)
{
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "HomePage")
Application.OpenForms[i].Close();
}
}
// this will show the HomePage form and close other forms whos name is not "HomePage"
the Problem is when i press the Back Button , im hiding it .. how can a button will re open a previously closed or hided form ?
I hope you can Help me! Thanks!
private void button3_Click(object sender, EventArgs e)
{
???????
}
You would need to store a reference to the form that you wish to open again.
There are a couple of options to do this, but if you simply wanted to allow the user to go "Forward" once, you could just store a reference to the form like so:
internal class MyHistory {
internal static Form LastForm;
}
// ........
private void button1_Click(object sender, EventArgs e)
{
MyHistory.LastForm = this;
this.Hide();
}
// ........
private void button3_Click(object sender, EventArgs e)
{
MyHistory.LastForm.Show();
}
Of course, you could maintain a full stack of history items and traverse back/forwards through them if you wanted to be more comprehensive than this.
Note that, if you .Close() your form, you won't be able to reopen it as the reference will be disposed of once it is closed. This method would only work if you were to .Hide() it, which keeps the form instance valid, just hides the form from the user's view.
you could use a form list which holds all initialized forms. that way you can hide, show, add and remove forms dynamically.
List<Form> lstForms = new List<Form>();
then when you add a form:
Form newForm = new Form();
lstForms.Add(newForm);
Hiding a Form:
lstForms(x).Hide(); //x = index of Form you want to hide
Showing a Form
lstForms(x).Show(); //x = index of Form you want to hide
Removing a Form (when closing it for example)
lstForms.RemoveAt(x);
that way you can dynamically work with forms and it is much easier to keep an overview if you have many forms...

How to hide a form from the main form

I have been tweaking my program all day and I am having a problem hiding a form which will pop up saying "Please wait"
For example:
private void button12_Click(object sender, EventArgs e)
{
form2 wait = new form2();
pw.Show();
}
private void button13_Click(object sender, EventArgs e)
{
form2 wait = new form2();
pw.Hide();
}
This will not work, although I am sure this isn't news to the casual C# programmer. Is there a simple way to do what I am attempting? I have tried searching online and I did find something although I wasn't 100% sure what they were trying to do. I was going to find an example to show you but I closed page - Typical. However I think they were trying to overide the show and give you control over the .show with a bool?
The code isn't working as you expect it to because the form2 inside of button12_Click is different from the form2 inside of button13_click. Notice that you are using the new keyword twice. So in button13_click, you are creating a new form2, and then hiding it, even though you haven't even shown it yet!
Instead you can create a single form2 instance to share between your two methods:
//define this code outside both of the methods below
form2 _waitForm = new form2();
private void button12_Click(object sender, EventArgs e)
{
_waitForm.Show();
}
private void button13_Click(object sender, EventArgs e)
{
//this will hide the same form2 that was shown in button12_Click
_waitForm.Hide();
}

Categories