简体   繁体   中英

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”). This Box should have 2 characteristics:

  1. It should be modeless, no input is needed, it is merely an information box
  2. It should disappear automatically (based on a timer).

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.

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?

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:

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.

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM