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.
Related
I'm not sure if focus is the right word but I have an on key up event on my form that will open a new form and close the current form, however after i enter a textbox or other such object i can't re select the form to be able to activate the key up event
This is code a I am using currently when i click on my form, to try select my currently open form however it does not close the current form when i active the key up event, when i do it this way
private void frmLevel1_Click(object sender, EventArgs e)
{
this.BackColor = GlobalClass.BG;
frmLevel1 lvl1 = new frmLevel1();
lvl1.Select();
}
I'm not 100% sure, but you have a few different questions. I hope it helps.
// Button click event
private void button1_Click(object sender, EventArgs e)
{
// Focus on textbox
this.ActiveControl = textBox1;
}
// Form load event
private void Form1_Load(object sender, EventArgs e)
{
// Focus on textbox
this.ActiveControl = textBox1;
}
// Close the current form and open another one. Use any event what you want
private void button1_Click(object sender, EventArgs e)
{
using (Form2 frm = new Form2())
{
// Hide the current form. If you close it it will dispose of all further events
this.Hide();
// Open the new form
frm.Show();
// Close the current form
this.Close();
}
}
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.
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 !
In windows form (c#), i am showing a form when user click on button, it is working fine form is visible to user, but if user click again on the same button the same form is opening again two forms are displaying. Is there any way to prevent this, please give me any reference for this thank you. This is my code....
private void button1_Click(object sender, EventArgs e)
{
Form2 obj = new Form2();
obj.Show();
}
You are most likely doing something like this:
void button1_OnClick(object sender, EventArgs e) {
var newForm = new MyForm();
newForm.Show();
}
So you are showing a new instance of the form every time it is clicked. You want to do something like this:
MyForm _form = new MyForm();
void button1_OnClick(object sender, EventArgs e) {
_form.Show();
}
Here you have just one instance of the form you wish to show, and just Show() it.
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(MyFormType))
{
form.Activate();
return;
}
}
Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();
i tried more than a ways to compare which one is better.
but i think this solution must be better than the answer.
You can try something like
private Form f;
private void button2_Click(object sender, EventArgs e)
{
if (f == null)
{
f = new Form();
f.Closed += f_Closed;
f.Show();
}
}
void f_Closed(object sender, EventArgs e)
{
f = null;
}
You are most probably creating a new instance of the form every time in the Click handler of the Button.
So you ill need to move the Form object creation outside the Button_Click.
Here's a good example of a proven solution
This will open the form if it is not already open.
If it is already open, it will place it in the foreground.
namespace MainProgram
{
public partial class Form1 : Form
{
private Form formNew = new FormToShowSomething();
private void button1_Click(object sender, EventArgs e)
{
formNew.Show();
formNew.Activate();
}
}
}
the easiest solution to your problem is replacing the Show command with ShowDialog, that way you won't any problem when it comes to preventing a form to show up twice
Form2 obj = new Form2();
obj.ShowDialog();
the code: .ShowDialog(); is what we are currently looking for that will solve the issue
10 years after , like the band :p
Thought to share the code that works for me. Nothing fancy, just checking if the form instance exists. Also, I don't prefer the ShowDialog, because the user is 'trapped' in that form and I find it annoying. The user might want to check other info from another source, for example when filling an online form and needs to copy paste a field info.
private void button1_Click(object sender, EventArgs e)
{
var obj = Application.OpenForms.OfType<Form2>().Select(t => t).FirstOrDefault();
if (obj != null)
{
obj.BringToFront();
}
else
{
obj = new Form2();
obj.Show();
}
}
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();
}