如何一个接一个地加载多个表单? [英] How to load multiple forms one after another ?

查看:62
本文介绍了如何一个接一个地加载多个表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表单,如form1,form2,form3

每个表单将每10秒逐个加载。



请帮忙我出去了。



我尝试了什么:



Program.cs

.........................



静态class program

{

public static RadForm1 RadForm1 = null;

public static RadForm2 RadForm2 = null;

public static RadForm3 RadForm3 = null;

public static int Flag = 0;

///< summary>

///

[STAThread]

static void Main()

{



Application.EnableVisualStyles( );

Application.SetCompatibleTextRenderingDefault(false);



线程splashThread =新线程(新的ThreadStart(

)代表

{

RadForm1 = new RadForm1();

RadForm1.Size = new System.Drawing.Size(1365,739);

Application.Run (RadForm1);

}

));



splashThread.SetApartmentState(ApartmentState.STA);

splashThread.Start();





RadForm2 =新RadForm2();

RadForm2.Size = Screen.PrimaryScreen.WorkingArea.Size;

RadForm2.Load + = new EventHandler(RadForm1_Load);

Application.Run(RadForm2);



RadForm3 =新的RadForm3();

RadForm3.Size = Screen.PrimaryScreen.WorkingArea.Size;

RadForm3 .Load + = new EventHandler(RadForm2_Load);

Application.Run(RadForm3);



}

静态无效RadForm1_Load(对象发送者,EventArgs e)

{

if( RadForm1 == null)

{

return;

}

RadForm1.Invoke(新动作(RadForm1。关闭));

RadForm1.Dispose();

RadForm1 = null;

}

static void RadForm2_Load (对象发送者,EventArgs e)

{

if(RadForm2 == null)

{

return;

}

RadForm2.Invoke(新动作(RadForm2.Close));

RadForm2.Dispose();

RadForm2 = null;

}

}



和第二种形式...... ..........



public RadForm2()

{

Initia lizeComponent();

Thread.Sleep(5000);

this.StartPosition = FormStartPosition.CenterScreen;

}

解决方案

好吧,假设你在winforms开发方面有相当的技巧,这是一个简单的例子,我在大约10分钟内掀起。第一个for显示10秒,然后关闭,此时第二个表单自动打开。



步骤1 - 创建一个新的winforms应用程序(它已经有Form1)。我在表单中添加了一个ok和cancel按钮。但你没必要。



第2步 - 添加新表格(Form2)。



步骤3 - 在program.cs中,将主函数更改为:



 [STAThread] 
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Form1 f1 = new Form1();
Application.Run(f1);
if (f1.DialogResult == DialogResult.OK)
{
Form2 f2 = new Form2();
Application.Run(f2);
f2.Activate();

}
}





第4步 - 这是你的Form1代码



  public   partial  < span class =code-keyword> class  Form1:Form 
{
int dismiss = 10000 ;
int elapsed = 0 ;
定时器计时器;
public Form1()
{
InitializeComponent();
timer = new Timer(){Interval = 50 ,Enabled = true };
timer.Tick + = timer_Tick;
}

void timer_Tick( object sender,EventArgs e)
{
逝去+ = timer.Interval;
if (已过去> = dismiss)
{
button1 .PerformClick();
}
}

私有 void button2_Click( object sender,EventArgs e)
{
DialogResult = DialogResult.Cancel;
this .Close();
}

private void button1_Click( object sender,EventArgs e)
{
DialogResult = DialogResult.OK;
this .Close();
}

私有 void Form1_FormClosing( object sender,FormClosingEventArgs e)
{
timer.Enabled = false ;
timer.Dispose();

}
}





计时器可能过于精细,但计时器事件最低Windows中的优先级事件,并不保证在繁忙的系统上处理它们。我建议使用另一种技术,但是出于示例的目的,它是合适的。


你应该能够将这段代码移动到你想要的地方:



多个后续主要表格在C#Apps 中[ ^ ]

I have 3 forms like form1,form2,form3
each form will load one by one every 10 seconds .

Please help me out.

What I have tried:

Program.cs
.........................

static class Program
{
public static RadForm1 RadForm1 = null;
public static RadForm2 RadForm2 = null;
public static RadForm3 RadForm3 = null;
public static int Flag = 0;
/// <summary>
///
[STAThread]
static void Main()
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Thread splashThread = new Thread(new ThreadStart(
delegate
{
RadForm1 = new RadForm1();
RadForm1.Size = new System.Drawing.Size(1365, 739);
Application.Run(RadForm1);
}
));

splashThread.SetApartmentState(ApartmentState.STA);
splashThread.Start();


RadForm2 = new RadForm2();
RadForm2.Size = Screen.PrimaryScreen.WorkingArea.Size;
RadForm2.Load += new EventHandler(RadForm1_Load);
Application.Run(RadForm2);

RadForm3 = new RadForm3();
RadForm3.Size = Screen.PrimaryScreen.WorkingArea.Size;
RadForm3.Load += new EventHandler(RadForm2_Load);
Application.Run(RadForm3);

}
static void RadForm1_Load(object sender, EventArgs e)
{
if (RadForm1 == null)
{
return;
}
RadForm1.Invoke(new Action(RadForm1.Close));
RadForm1.Dispose();
RadForm1 = null;
}
static void RadForm2_Load(object sender, EventArgs e)
{
if (RadForm2 == null)
{
return;
}
RadForm2.Invoke(new Action(RadForm2.Close));
RadForm2.Dispose();
RadForm2 = null;
}
}

and in second form................

public RadForm2()
{
InitializeComponent();
Thread.Sleep(5000);
this.StartPosition = FormStartPosition.CenterScreen;
}

解决方案

Well, assuming you're reasonably skilled at winforms development, here's a simple example that I whipped up in about 10 minutes. The first for is displayed for 10 seconds, and then closes, upon which time the 2nd form opens automagically.

Step 1 - Create a new winforms app (it will already have "Form1"). I added an ok and cancel buttons to the form. but you don't have to.

Step 2 - Add a new form ("Form2").

Step 3 - In program.cs, change the main function to look like this:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Form1 f1 = new Form1();
    Application.Run(f1);
    if (f1.DialogResult== DialogResult.OK)
    {
        Form2 f2 = new Form2();
        Application.Run(f2);
        f2.Activate();
                
    }
}



Step 4 - This is your Form1 code

public partial class Form1:Form
{
    int dismiss = 10000;
    int elapsed = 0;
    Timer timer;
    public Form1()
    {
        InitializeComponent();
        timer = new Timer(){ Interval = 50, Enabled=true };
        timer.Tick += timer_Tick;
    }

    void timer_Tick(object sender,EventArgs e)
    {
        elapsed += timer.Interval;
        if (elapsed >= dismiss)
        {
            button1.PerformClick();
        }
    }

    private void button2_Click(object sender,EventArgs e)
    {
        DialogResult = DialogResult.Cancel;
        this.Close();
    }

    private void button1_Click(object sender,EventArgs e)
    {
        DialogResult = DialogResult.OK;
        this.Close();
    }

    private void Form1_FormClosing(object sender,FormClosingEventArgs e)
    {
        timer.Enabled = false;
        timer.Dispose();

    }
}



The timer might be too granular, but timer events are the lowest priority events in windows, and they are not guaranteed to be handled on a busy system. I would recommend using another technique for this, but for the purposes of example, it's suitable.


You should be able to moify this code to do what you want:

Multiple Subsequent "Main" Forms in C# Apps[^]


这篇关于如何一个接一个地加载多个表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆