如何向winform显示警报消息面板? [英] How to show alert message panel to the winform?

查看:113
本文介绍了如何向winform显示警报消息面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DisplayAlert方法存在于我的主窗体中,但如果我在用户控件上调用DisplayAlert方法,则显示我的用户控件中的所有消息警报,但是我在主窗体中调用相同的方法显示输出。



我尝试了什么:



公共分部主要:表格

{



public static void DisplayAlert(string message,AlertNotification enumAlertNotification)

{

var t = new Timer();

var main = new Main();

if(AlertNotification.Success == enumAlertNotification)

{



var myControl = new AlertControl();

myControl.lblMessage.Text = message;

myControl。 Controls.Add(myControl.lblMessage);

myControl.BackColor = Color.YellowGreen;

main.AlertPanel.Controls。添加(myControl);

myControl.Visible = true;

t.Interval = 2000;

t.Tick + =(s,e )=>

{

myControl.Visible = false;

t.Stop();

} ;

t.Start();

}

if(AlertNotification.Error == enumAlertNotification)

{

var myControl = new AlertControl();

myControl.lblMessage.Text = message;

myControl.Controls.Add(myControl.lblMessage) ;

myControl.BackColor = Color.Red;

main.AlertPanel.Controls.Add(myControl);

myControl.Visible = true;

t.Interval = 3000;

t.Tick + =(s,e)=>

{

myControl.Visible = false;

t.Stop();

};

t.Start();



}

if(AlertNotification.Information == enumAlertNotification)

{

var myControl = new AlertControl();

myControl.lblMessage.Text = message;

myControl.Controls.Add(myControl.lblMessage);

myControl.BackColor = Color.LightSkyBlue;

main.AlertPanel.Controls.Add(myControl);

myControl.Visible = true;

t .Interval = 3000;

t.Tick + =(s,e)=>

{

myControl.Visible = false;

t.Stop();

};

t.Start();

< br $>
}

}





}



//我在用户控制中调用这些代码行



Main.DisplayAlert(string.Format({0} {1 },TotalsCount,Records Found),AlertNotification.Information);



//下面是我的提醒用户控制代码



公共部分类AlertControl:UserControl

{

私有标签_lblMesage;



public label lblMessage

{

get

{

if(_lblMesage == null)

_lblMesage = new Label();

返回_lblMesage;

}



}

public AlertControl()

{

this.lblMessage.AutoSize = true;

this.lblMessage.BackColor = System。引过来g.Color.Transparent;

this.lblMessage.Font = new System.Drawing.Font(Century Gothic,9.75F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point ,((byte)(0)));

this.lblMessage.Location = new System.Drawing.Point(36,7);



this.lblMessage.Size = new System.Drawing.Size(188,17);

this.lblMessage.TabIndex = 0;



InitializeComponent();

}

}

my DisplayAlert method is present in my main form but i show all the message alerts in my user controls if i call DisplayAlert method on user control its show nothing but i call same method in my main form its show output.

What I have tried:

public partial class Main : Form
{

public static void DisplayAlert(string message, AlertNotification enumAlertNotification)
{
var t = new Timer();
var main = new Main();
if (AlertNotification.Success == enumAlertNotification)
{

var myControl = new AlertControl();
myControl.lblMessage.Text = message;
myControl.Controls.Add(myControl.lblMessage);
myControl.BackColor = Color.YellowGreen;
main.AlertPanel.Controls.Add(myControl);
myControl.Visible = true;
t.Interval = 2000;
t.Tick += (s, e) =>
{
myControl.Visible = false;
t.Stop();
};
t.Start();
}
if (AlertNotification.Error == enumAlertNotification)
{
var myControl = new AlertControl();
myControl.lblMessage.Text = message;
myControl.Controls.Add(myControl.lblMessage);
myControl.BackColor = Color.Red;
main.AlertPanel.Controls.Add(myControl);
myControl.Visible = true;
t.Interval = 3000;
t.Tick += (s, e) =>
{
myControl.Visible = false;
t.Stop();
};
t.Start();

}
if (AlertNotification.Information == enumAlertNotification)
{
var myControl = new AlertControl();
myControl.lblMessage.Text = message;
myControl.Controls.Add(myControl.lblMessage);
myControl.BackColor = Color.LightSkyBlue;
main.AlertPanel.Controls.Add(myControl);
myControl.Visible = true;
t.Interval = 3000;
t.Tick += (s, e) =>
{
myControl.Visible = false;
t.Stop();
};
t.Start();

}
}


}

// i am calling these line of code in user control

Main.DisplayAlert(string.Format("{0} {1}",TotalsCount,"Records Found"),AlertNotification.Information);

// below is my Alert User Control code

public partial class AlertControl : UserControl
{
private Label _lblMesage;

public Label lblMessage
{
get
{
if (_lblMesage == null)
_lblMesage = new Label();
return _lblMesage;
}

}
public AlertControl()
{
this.lblMessage.AutoSize = true;
this.lblMessage.BackColor = System.Drawing.Color.Transparent;
this.lblMessage.Font = new System.Drawing.Font("Century Gothic", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblMessage.Location = new System.Drawing.Point(36, 7);

this.lblMessage.Size = new System.Drawing.Size(188, 17);
this.lblMessage.TabIndex = 0;

InitializeComponent();
}
}

推荐答案

您可以更改父控制Control或UserControl,只需将其添加到您希望成为新父级的Control / Usercontrol的ControlCollection中。实际上,'添加操作从其当前的父ControlCollection中删除Control / UserControl,然后将其插入到新的ControlCollection中。



是否应该更改控件/用户的父级?在运行时控制是另一个问题,我的意见是你应该避免这样做,除非有一个非常令人信服的理由这样做。



我理解你的问题,你想在这里做什么并不是在你的Form控件的任意一个里面显示这个警告面板,但你确实希望在它的一个上面显示它,并且你的Formn's Controls。我将向您展示一些可以做到这一点的代码,但是......首先......对Control / UserControl继承的一些评论::



An 一个Control或UserControl的实例, sited-in / added-to Form或其他ContainerControl实例的ControlCollection可以访问已添加的Control / Form通过其'父母财产;它的'TopLevelControl属性将返回包含它的最外层(顶级)控件(ContainerControl)。



假设你有'Panel2 with'Button3, Panel2位于Panel1内,位于Form1内:
You can change the Parent Control of a Control, or UserControl, by simply adding it to the ControlCollection of the Control/Usercontrol you want to be the "new" Parent. In effect, the 'Add operation removes the Control/UserControl from its current Parent ControlCollection, then inserts it into the new ControlCollection.

Whether you should change the Parent of a Control/User?Control at run-time is another question, and, my opinion is that you should avoid doing that unless there is a very compelling reason to do so.

As I understand your question what you want to do here is not shows this alert panel inside an arbitrary one of your Form's Controls, but you do want to show it on top of, and adjacent to one of your Formn's Controls. I'll show you some code that can do that, but ... first ... a few remarks on Control/UserControl inheritance::

An instance of a Control, or UserControl, sited-in/added-to the ControlCollection of an instance of a Form, or other ContainerControl, has access to the Control/Form it has been added to via its 'Parent property; its 'TopLevelControl Propertry will return the outer-most (top-level) Control (ContainerControl) that contains it.

Assume you have 'Panel2 with 'Button3 inside it, and Panel2 is inside Panel1, which is inside Form1:
private void Form1_Load(object sender, EventArgs e)
{
    var parent = button3.Parent;   // => Panel2
    var toplvl = button3.TopLevelControl; // => Form1

    var inheritance = button3.GetControlInheritance().ToList(); // => see below
}

方法'GetControlInheritance是一种扩展方法:

The method 'GetControlInheritance is an Extension Method:

public static class ControlExtensions
{
    public static IEnumerable<Control> GetControlInheritance(this Control control)
    {
        while (control.Parent != null)
        {
            yield return control.Parent;
            control = control.Parent;
        }
    }
}

调用它的结果,在这个例子中是:< Panel2,Panel1,Form1>



现在,请考虑将Panel(或UserControl)用作警报。称之为'AlertPanel。假设您使用一个方法来处理某个事件,该方法获取对您想要显示旁边的AlertPanel的Control的引用,AlertPanel的左上角偏离Control的左上角10,10像素希望它显示结束。



1.您将Panel(或UserControl)放在Form上,将其'Visible属性设置为'false。



2.假设您的事件处理程序如下所示:

And the result of calling it, in this example is: <Panel2, Panel1, Form1>

Now, consider you have Panel (or UserControl) you want to use as an "alert." Call it 'AlertPanel. Assume that there's some event that you handle with a method that gets a reference to the Control you want to display the AlertPanel next-to, with the left-top of the AlertPanel offset 10,10 pixels from the left-top of the Control you want it displayed "over."

1. you put the Panel (or UserControl) on the Form, set its 'Visible property to 'false.

2. let's say your event handler looks like this:

private void ShowAlertPanel(Control toShowOver, int offx, int offy)
{
    Point loc = toShowOver.Location;
    loc.Offset(offx,offy);
    AlertPanel.Location = loc;
    
    AlertPanel.BringToFront();
    AlertPanel.Show();
}

3。但是,还有一个额外的要求:如果你想在一个嵌套在另一个Control中的Control上显示你的AlertPanel会怎样。由于嵌套的Control的位置是基于其Control Parent的坐标,因此您必须首先从嵌套Control的坐标映射到Screen坐标,然后映射到Form co-ordaintes。然而,这并不困难:

3. but, there's an additional requirement possible: what if you want to show your AlertPanel over a Control that's nested inside another Control. Since that nested Control's Location is in co-ordinates based on its Control Parent, you have to map from the the nested Control's co-ordinates first to Screen co-ordinates, and then to Form co-ordaintes. That's not difficult, however:

private void ShowAlertPanel2(Control toShowOver, int offx, int offy)
{
    if (toShowOver == this) throw new ArgumentException("Cannot use Form Parent");

    Point loc = toShowOver.Location;

    // handle showing over nested Control
    if (toShowOver.Parent != this)
    {
        // get screen location
        loc = toShowOver.Parent.PointToScreen(loc);

        // translate it to Form co-ordinates
        loc = this.PointToClient(loc);
    }

    loc.Offset(offx,offy);
    AlertPanel.Location = loc;

    AlertPanel.BringToFront();
    AlertPanel.Show();
}


Main.DisplayAlert不会做任何事情,因为控件/用户控件根本不知道它们所在的表格而且根本不应该在乎。如果你试图这样做,你永远将这两个表单/控件绑在一起。它们永远不能独立分离和使用。



您的DisplayAlert根本不应该在主表单上。如果您打算尝试在应用程序的任何地方使用它,那么应该是它自己的类,它使用静态方法ShowAlert创建Alert表单,设置其属性并显示表单。
"Main.DisplayAlert" won't do anything because controls/usercontrols shouldn't know anything at all about the form they are on and shouldn't care at all. If you try to do that you forever tie those two forms/controls together. They can never be separated and used independently.

Your DisplayAlert should NOT be on the Main form at all. If you're going to try and use it from anywhere in your application it should be it's own class with a static method, ShowAlert, that creates the Alert form, set its properties and shows the form.


这篇关于如何向winform显示警报消息面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆