Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Forms
Hello,
Please how do I move from one form to another? For example I click on the button and another form immediately show.
I know the only way and thats hiding one and showing another but it looks bad because it flickers (for very short time but still). Is there some way to do it without hiding?
Code what I use now to to that:

            this.Hide();
            Form2 secondtform = new Form2();  
            secondform.Show();
 
but this cause disappearing icon showing another etc.. So I hoped there is some way to make this look like you are moving across normal windows program.
 
EDIT: Just tried this code to get rid of flickering
public void EnableDoubleBuffering()
{
   this.SetStyle(ControlStyles.DoubleBuffer |
      ControlStyles.UserPaint |
      ControlStyles.AllPaintingInWmPaint,
      true);
   this.UpdateStyles();
}
Unfortenately it doesn't solved it..
Posted 15-Feb-12 13:38pm
Edited 16-Feb-12 6:07am
v4
Comments
Shahin Khorshidnia at 15-Feb-12 19:04pm
   
Please Tag your question.
What kind of application are you working on? Windows, Web, WPF ...?
LosEagle at 15-Feb-12 19:09pm
   
I tagged it as C# do I have to add another tag? I'm working on Windows Form application :)
SAKryukov at 15-Feb-12 20:03pm
   
Yes, yes, add "Forms". This is important to have experts looking at right questions, before they load this page.
--SA
mark merrens at 15-Feb-12 19:17pm
   
Do you really need to hide the parent form? Can't you just display the child as Modal?
LosEagle at 15-Feb-12 19:19pm
   
Don't know how to do it :D. Will it work if I use it for more than just two forms? Sorry for noob question just learning it for few weeks :)
SAKryukov at 15-Feb-12 20:04pm
   
"Improve question". Add a tag.
--SA
SAKryukov at 15-Feb-12 20:36pm
   
I basically answered, but please understand that you did not provide enough information to pin-point the reason of flicker.
 
So, if my advice cannot help your with flicker, this is not my fault; you would need to demonstrate such flicker on a code sample.
--SA
LosEagle at 16-Feb-12 6:45am
   
The reason of flickering is because I use this
this.Hide();
Form2 secondtform = new Form2();
secondform.Show();
So it hides first form and shows second.
I'am going to try your solution :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
Try this:
 
private Form2 form2;
public Form1()
{
    InitializeComponent();
 
    this.form2 = new Form2();
    //form2.Show();
    HideForm(form2);
}
 
private void ShowButton_Click(object sender, EventArgs e)
{
    ShowForm(form2);
    HideForm(this);
}
 
private int height;
private int width;
private System.Windows.Forms.FormBorderStyle borderStyle;
 
private void ShowForm(Form form)
{
    form.Visible = true;
    form.FormBorderStyle = borderStyle;
    form.Height = height;
    form.Width = width;
}
 
private void HideForm(Form form)
{
    height = form.Height;
    width = form.Width;
    borderStyle = form.FormBorderStyle;
 
    form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    form.Height = 0;
    form.Width = 0;
    form.Visible = false;
}
  Permalink  
v3
Comments
LosEagle at 16-Feb-12 6:54am
   
Thats similar to what I have right now but when I use hiding it looks bad when I'am "moving across program" because icon disapears and new shows etc... . But thanks for your help I appreciate that
Shahin Khorshidnia at 16-Feb-12 8:29am
   
You're welcome.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Normally, you don't need anything else but Form.Show, Form.Activate or Form.BringToFront please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^].
 
If this flickers, most likely you did something wrong. It's hard to say what, without having some code sample to manifest the problem. Just in case: look at my recent anti-clicker advice: Question about change form to full screen mode but my form flashing[^].
 
—SA
  Permalink  
Comments
LosEagle at 16-Feb-12 6:51am
   
Thanks I will give a look :)
My code:
this.Hide();
Form2 secondtform = new Form2();
secondform.Show();
but it doesn't look good because when moving from one form to second etc.. icon disapears new shows .. it just doesn't look normal
SAKryukov at 17-Feb-12 21:52pm
   
There is nothing particularly wrong with this particular piece of code. Maybe this is not a real flicker, just an unpleasant effect of form change.
 
Let's assume you patten is just showing one form at a time navigating from one to another. If so, this is just wrong UI design, technically correct, but transition effect just cannot be done smother (you can only put Hide at the and, makes the last statement instead of first).
 
If this is so, I could advice your a very different implementation pattern which would be nearly 100% smooth.
 
What you have as different forms should be refactored to become separate panels, with all the controls and layout exactly the same as in your present forms.
 
Now, have only one forms filled (Dock = DockStyle.Fill) with set of panels. Each panel except one should be hidden (Visisible = false), only on panel at a time is visible. The transition will be changing from one visible panel to another. You will be surprised to see how much better it will look, easier to program and support, too.
 
Good luck,
--SA
LosEagle at 19-Feb-12 19:53pm
   
That sounds finaly like solution for my problem :) please can you give me a piece of code where you use this? I've tried to make it but I just don't know how and where to do this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 16 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100