实现一个类似WshShell.popup的function

marcus2006 2010-02-02 04:49:56
Envionment:C#, .Net3.5,Win7
要求: 1. 一个主窗口弹出一个子窗口,子窗口负责询问用户是否要做什么事'是/否'
2. 如果在规定时间内,如10秒,用户无反应,就自动返回是或否
请问:
这个要如何实现?
...全文
163 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
marcus2006 2010-02-08
  • 打赏
  • 举报
回复
再提一下,
1. 中国人在回贴时很多都不详细,帮助人也要帮助清楚嘛,简单说2句就跑了
2.很喜欢直接贴源码,贴完也不用颜色表示一下,那些是重要的,那些是自动生成的,看的人七晕八素的.

进步,我们需要进步
marcus2006 2010-02-08
  • 打赏
  • 举报
回复
现在弄出来了,详细写下方法.虽然很多人不屑这个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;
}
}
}
绝代坏坏 2010-02-02
  • 打赏
  • 举报
回复
第一个问题 设置dialogresult 返回即可
第二个问题 添加timer控件 用tick事件控制即可。

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧