Envionment:C#, .Net3.5,Win7
要求: 1. 一个主窗口弹出一个子窗口,子窗口负责询问用户是否要做什么事'是/否'
      2. 如果在规定时间内,如10秒,用户无反应,就自动返回是或否
请问:
 这个要如何实现?

解决方案 »

  1.   

    第一个问题 设置dialogresult 返回即可
    第二个问题 添加timer控件 用tick事件控制即可。
      

  2.   

    现在弄出来了,详细写下方法.虽然很多人不屑这个case,但毕竟是基础.
    如 绝代坏坏)所说,只是他省略了具体步骤. 这可是个恶习啊,能详细点就详细点吧.
    1. 通过studio加入一个form文件(名字叫mess好了),设计好.比如我就是很简单:1个yes+1个no+1个Label+1个timer
    2. 在GUI上设置yes与no的dialog性质为DialogResultYes与DialogResultNo.
       this.acceptbutton=yes;
       this.cancelbutton=no;
    3. YesButtonClick事件中返回DialogResult.Yes,No中返回DialogResult.No.
    4. 创建一个function,当timer时间到时invoke它. 在mess的load事件中加入Timer.Tick +=yourfunctionname. ;
    5. 主form调用这个form,用showDialog. (MSDN说的,我尝试如果用show,那就立刻显示主form与mess form,达不到要求了)虽然解决了很多要求,但是通过看MSDN又有了很多新的疑问:1.窗体之间不能直接控制,如果要控制,有多少种方法,是哪些 2.timer有2种,但是都有问题 .而且还把异步调用与多线程都看了一遍(当然,还是很多不懂)
    Sourcode:
    主窗体的
    namespace tmp2
    {
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : System.Windows.Forms.Form
    {
    public MainForm()
    {
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();
    dosomething();
    // TODO: Add constructor code after the InitializeComponent() call. // TODO: Add Code to call testing functions - for example:
    // Automation.Start() }

    void Button1Click(object sender, EventArgs e)
    {

    }
    public void dosomething()
    {

    messwin newmess =new messwin();

    newmess.setlabel("good");
    newmess.ShowDialog();
    System.Windows.Forms.MessageBox.Show(newmess.DialogResult.ToString());


    }
    }
    }
    message form的
    namespace tmp2
    {
    /// <summary>
    /// Description of mess.
    /// </summary>
    public partial class messwin : Form
    {

    public messwin()
    {
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //
    // TODO: Add constructor code after the InitializeComponent() call.
    //
    }
    public void setlabel(string s)
    {
    this.label1.Text=s;
    }
    void MessLoad(object sender, EventArgs e)
    {
    this.AcceptButton=buttonyes;
    this.CancelButton=buttonno;
    this.timer1.Tick +=new EventHandler(this.returnvalue);
    timer1.Interval=6000;
    timer1.Enabled=true;
    timer1.Start();


    }
    void returnvalue(object sender,EventArgs e)
    {
    ButtonyesClick(null,null);
    timer1.Stop();
    }


    void ButtonnoClick(object sender, EventArgs e)
    {
    this.DialogResult= DialogResult.No;

    }

    void ButtonyesClick(object sender, EventArgs e)
    {
    this.DialogResult= DialogResult.Yes;
    }
    }
    }