[英]Create modeless child form in C#
I am currently working on a desktop application (Main Form) which uploads data in order to perform some basic calculations. 我目前正在开发一个桌面应用程序(主窗体),该应用程序上载数据以便执行一些基本计算。
I would like to include a Box popping up once the user has uploaded his file (eg “Upload has been completed”). 我希望在用户上传文件后(例如“上传已完成”)弹出一个Box。 This Box should have 2 characteristics: 此Box应具有2个特征:
I have already found a solution for the second point, however I still have to create the form I would like to pop up. 我已经找到第二点的解决方案,但是我仍然必须创建要弹出的表单。 As I do not want it to have buttons, MessageBox is not a good fit. 因为我不希望它具有按钮,所以MessageBox不太适合。
I know that I can create a modeless form as follows: 我知道我可以创建如下的无模式表单:
Public class test
{
Form f = new Form();
f.show()
}
Starting from that, how can I insert a string text and a string caption much similar to the messageBox style? 从那开始,如何插入与messageBox样式非常相似的字符串文本和字符串标题?
thanks 谢谢
I think this can give you an idea how to start 我认为这可以让您了解如何开始
public class MyDialog: Form
{
public MyDialog(string prompt, int timeout)
{
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
rtb.Font = new Font("Times new Roman", 14f, FontStyle.Bold);
rtb.Text = prompt;
this.Controls.Add(rtb);
var _Timer = new System.Windows.Forms.Timer()
{
Enabled = true,
Interval = timeout
};
_Timer.Tick += (s, e) => this.Close();
this.Show();
}
}
All you need to do is creating this form like below 您需要做的就是创建如下所示的表单
var f = new MyDialog("It works", 5000);
First, you can create a form, something like the following, which would be your message box equivalent. 首先,您可以创建一个表单,如下所示,它相当于您的消息框。
Then you can change that new Form2
constructor like this: 然后,您可以像这样更改新的Form2
构造函数:
public partial class Form2 : Form
{
public Form2(string title, string message)
{
InitializeComponent();
this.Text = title;
label1.Text = message;
}
}
this.Text
is your title and then by using label1.Text
, set the label text. this.Text
是您的标题,然后使用label1.Text
设置标签文本。
Then, when you're launching the new form, do like so: 然后,在启动新表单时,请执行以下操作:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2("header","message");
f2.Show();
}
And this is what you'll get: 这就是您将得到的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.