Windows Develop Bookmark and Share   
 index > Windows Forms General > Cycle trough diffrent forms
 

Cycle trough diffrent forms

At my job they asked me if it would be able to build a program with 4 sepperate forms, that would switch to forground every 30 seconds.
So that first form1 is on top, 30 seconds later form 2.

I am not a prof-programmer and I do is as a hobby so thats why this question.

I managed to do this but then I had to include thread.sleep but then the form hangs. When I use a Timer to call the forms, after they all have been opened, it comes up with cross-threading.

I dont have any code at this moment to present you guys so sorry for that. but what is the best way to do this?

I start the application and press a start button.
It opens screen1.cs with some information/pictures ect on it.
After 30 seconds it opens screen2.cs with also info
30 secs later... screen 3.cs
and again 30 secs later, screen 4.

____
| Now I need it to set screen1.cs back to topmost, 30 secs later screen2 ect. ect.
| This will make sure that first screen1 and then ... is topmost so they switch positions
|-->this will be the loop

I dont know if this is the best way, or that there is a much easier way to do this. I hope you guys know what I exactly mean. I do not know much about threads, im still working on that so sorry for that.

Thanks in advance,
Lucas Mateijsen
Demonforce  Saturday, September 12, 2009 5:34 PM
Here is a crude example. In this case I'm just using a single generic UserControl, you would create special purpose ones instead.

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public Form1()
    {
        timer1 = new Timer();
        timer1.Interval = 2000; // 2 seconds because I'm not waiting around for 30
        timer1.Tick += timer1_Tick;

        Button button = new Button { Name = "button1", Text = "Click To Start", Location = new Point(10, 10), AutoSize = true };
        button.Click += button1_Click;
        Controls.Add(button);

        for (int n = 1; n <= NumberOfPanels; n++)
            Controls.Add(CreateGenericUserControl(n));
    }

    private Control CreateGenericUserControl(int number)
    {
        Control control;
        control = new UserControl { Name = "userControl" + number.ToString(), Dock = DockStyle.Fill, Visible = false };
        control.Controls.Add(new Label { Text = control.Name, Location = new Point(10, 10) });
        return control;
    }

    const int NumberOfPanels = 4;
    private int currentPanel = 1;
    private Timer timer1;

    private void button1_Click(object sender, EventArgs e)
    {
        Controls["button1"].Visible = false;
        Controls["userControl" + currentPanel.ToString()].Visible = true;
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Controls["userControl" + currentPanel.ToString()].Visible = false;
        currentPanel++;
        if (currentPanel > NumberOfPanels)
            currentPanel = 1;
        Controls["userControl" + currentPanel.ToString()].Visible = true;

    }
}

  • Marked As Answer byDemonforce Sunday, September 13, 2009 11:38 AM
  •  
Tergiver  Saturday, September 12, 2009 6:12 PM
If your timer handler is throwing a cross-thread exception, you are using the wrong timer. Use System.Windows.Forms.Timer rather than System.Timers.Timer, or System.Threading.Timer.

I wouldn't do this with forms, it makes no sense. Instead create four user controls, add them all to one form and hide/show them as appropriate.
Tergiver  Saturday, September 12, 2009 5:44 PM
Tergiver offers some good advice. Check out this thread for a different approach. A TabControl with invisible Tabs, using borderless forms.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Saturday, September 12, 2009 5:59 PM
Here is a crude example. In this case I'm just using a single generic UserControl, you would create special purpose ones instead.

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public Form1()
    {
        timer1 = new Timer();
        timer1.Interval = 2000; // 2 seconds because I'm not waiting around for 30
        timer1.Tick += timer1_Tick;

        Button button = new Button { Name = "button1", Text = "Click To Start", Location = new Point(10, 10), AutoSize = true };
        button.Click += button1_Click;
        Controls.Add(button);

        for (int n = 1; n <= NumberOfPanels; n++)
            Controls.Add(CreateGenericUserControl(n));
    }

    private Control CreateGenericUserControl(int number)
    {
        Control control;
        control = new UserControl { Name = "userControl" + number.ToString(), Dock = DockStyle.Fill, Visible = false };
        control.Controls.Add(new Label { Text = control.Name, Location = new Point(10, 10) });
        return control;
    }

    const int NumberOfPanels = 4;
    private int currentPanel = 1;
    private Timer timer1;

    private void button1_Click(object sender, EventArgs e)
    {
        Controls["button1"].Visible = false;
        Controls["userControl" + currentPanel.ToString()].Visible = true;
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Controls["userControl" + currentPanel.ToString()].Visible = false;
        currentPanel++;
        if (currentPanel > NumberOfPanels)
            currentPanel = 1;
        Controls["userControl" + currentPanel.ToString()].Visible = true;

    }
}

  • Marked As Answer byDemonforce Sunday, September 13, 2009 11:38 AM
  •  
Tergiver  Saturday, September 12, 2009 6:12 PM

You can use google to search for other answers

Custom Search

More Threads

• Alternative for the datetimepicker?
• Image in TextBox
• Why would my Form a = new Form() be *CLONEING* itself? ie the new form != a
• How to run a function from a MDIi Child in a MDI Parent
• Safe Source ?
• Winforms - error logging
• Smtp client help
• Setting the cursor at application startup
• WebBrowser control and Java Applets
• Panel's border style