多次创建相同的表单

时间:2019-02-26 18:28:32

标签: c# winforms

我需要帮助,我目前正在编写通知,并且我想多次创建相同的表单,但是这始终是我的代码。 我拥有所学到的所有知识,但都没有得到应用,希望对我有所帮助。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UniqueOS_
{
    public partial class alert : Form
    {

    public alert(string _message, AlertType type)
    {
        InitializeComponent();

        Nachricht.Text = _message;

        switch (type)
        {
            case AlertType.success:
                this.BackColor = Color.SeaGreen;
                this.pictureBox1.Image = Properties.Resources.icons8_ok_70;
                break;
            case AlertType.info:
                this.BackColor = Color.Gray;
                this.pictureBox1.Image = Properties.Resources.icons8_info_70;
                break;
            case AlertType.warning:
                this.BackColor = Color.FromArgb(255, 128, 0);
                this.pictureBox1.Image = Properties.Resources.icons8_error_70__1_;
                break;
            case AlertType.error:
                this.BackColor = Color.Crimson;
                this.pictureBox1.Image = Properties.Resources.icons8_error_70__1_;
                break;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="message"></param>
    /// <param name="type"></param>
    public static void Show(string message, AlertType type)
    {
        new UniqueOS_.alert(message, type).Show();
    }


    private void alert_Load(object sender, EventArgs e)
    {         
         this.TopMost = true;
         this.Top = -1 * (this.Height);
         this.Left = Screen.PrimaryScreen.Bounds.Width - this.Width - 40;

         show.Start();
         button1.FlatAppearance.BorderColor = this.BackColor;                    
    }
    public enum AlertType
    {
        success, info, warning, error
    }

    private void button1_Click(object sender, EventArgs e)
    {
        close.Start();
    }
    int interval = 0;
    public void show_Tick(object sender, EventArgs e)
    {
        if (this.Top < 60)
        {
            this.Top += interval;
            interval += 2;
        }
        else
        {
            show.Stop();
            interval += 4;
            Verschwindertimer.Start();
        }
    }
    private void close_Tick(object sender, EventArgs e)
    {
        if (this.Opacity > 0)
        {
            this.Opacity -= 0.1;
        }
        else
        {
            this.Close();
        }

    }

    private void timeout_Tick(object sender, EventArgs e)
    {

    }

    private void Verschwindertimer_Tick(object sender, EventArgs e)
    {
        close.Start();
    }

    private void Nachricht_Click(object sender, EventArgs e)
    {

    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }
  }
}

1 个答案:

答案 0 :(得分:0)

尝试此操作,创建一个新的构造函数以更改left和top属性。这将在指定位置打开form2的新实例。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public enum AlertType
    {
        success,
        info,
        warning,
        error
    }

    public partial class alert : Form
    {
        public alert()
        {
            InitializeComponent();
        }

        public alert(string _message, AlertType type, int top, int left)
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.Manual;
            this.Left = left;
            this.Top = top;

            this.Text = _message;

            switch (type)
            {
                case AlertType.success:
                    this.BackColor = Color.SeaGreen;
                    break;
                case AlertType.info:
                    this.BackColor = Color.Gray;
                    break;
                case AlertType.warning:
                    this.BackColor = Color.FromArgb(255, 128, 0);
                    break;
                case AlertType.error:
                    this.BackColor = Color.Crimson;
                    break;
            }

            this.Show();
        }

        private void alert_Load(object sender, EventArgs e)
        {

        }
    }
}