我有一个usercontrol,在这个usercontrol中我创建了一个委托调用winform方法? [英] I have a usercontrol and in this usercontrol I create one delegate to call winform method?

查看:94
本文介绍了我有一个usercontrol,在这个usercontrol中我创建了一个委托调用winform方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我只有一个表单,其余部分都是用户控件。如果我点击菜单,它会加载相应的用户控件。当我保存新的记录消息时,我会以主表单显示给客户端每件事情都很好但是它的没有向主要表格AlertPanel标签显示消息。

In my application i have only one Form rest of all are user control.if i click menu it's load respective user control.when i save new record message will me shown to the client in the main form every thing goes fine but its did not show message to the main form AlertPanel lable.

public delegate void MessageAlert(string Message,AlertNotification enumNotification);
    public partial class ManageTenant : UserControl
    {
       
        public MessageAlert CallBack { get; set; }

        private async void ManageTenant_Load(object sender, EventArgs e)
        {
          CallBack("Load Successfully",AlertNotification.Success);
        }

}

这是我的表格代码:

public partial class Main : Form
    {

        Timer timer;
        public Main()
        {
            InitializeComponent();


        }

        private void Main_Load(object sender, EventArgs e)
        {

            this.WindowState = FormWindowState.Maximized;
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;

            ManageTenant manageTenant = new ManageTenant();
            manageTenant.CallBack = new MessageAlert(DisplayAlert);
            
        }

        public void DisplayAlert(string message, AlertNotification enumAlertNotification)
        {
            timer = new Timer();

            if (AlertNotification.Success == enumAlertNotification)
            {
                this.AlertPanel.BackColor = Color.YellowGreen;
                this.lblMessage.Text = message;

                #region hideAlertMessage
                timer.Interval = 2000;
                timer.Tick += (s, e) =>
                {
                    this.AlertPanel.Visible = false;
                    timer.Stop();
                };
                timer.Start();
                #endregion
            }
            
        }
}





我尝试过:



我正在创建一个委托我的用户控件访问winform方法,但没有显示任何内容。这是我的代码:



What I have tried:

I am creating a delegate in my user control to access winform method but is show nothing. Here is my code:

public delegate void MessageAlert(string Message,AlertNotification enumNotification);
    public partial class ManageTenant : UserControl
    {      
        public MessageAlert CallBack { get; set; }

        private async void ManageTenant_Load(object sender, EventArgs e)
        {
          CallBack("Load Successfully",AlertNotification.Success);
        }
}

以下是表格代码:

public partial class Main : Form
    {

        Timer timer;
        public Main()
        {
            InitializeComponent();


        }

        private void Main_Load(object sender, EventArgs e)
        {

            this.WindowState = FormWindowState.Maximized;
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;

            ManageTenant manageTenant = new ManageTenant();
            manageTenant.CallBack = new MessageAlert(DisplayAlert);
            
        }
public void DisplayAlert(string message, AlertNotification enumAlertNotification)
        {
            timer = new Timer();

            if (AlertNotification.Success == enumAlertNotification)
            {

                this.AlertPanel.BackColor = Color.YellowGreen;
                this.lblMessage.Text = message;

                #region hideAlertMessage
                timer.Interval = 2000;
                timer.Tick += (s, e) =>
                {
                    this.AlertPanel.Visible = false;
                    timer.Stop();
                };
                timer.Start();
                #endregion
            }
            
        }

推荐答案

一个更简单的解决方案是在你的UserControl中创建一个Event并从你的Form中订阅它 - 这也适合所有其他Controls使用的模型。

这样做很微不足道,但我很懒 - 所以我写了一个片段让它变得更简单:一个简单的代码片段来添加一个事件 [ ^ ]

现在我所做的就是输入evh并点击TAB两次它完成了。



你的回调方法不会检查是否有分配的回调 - 它应该 - 但除此之外,随便一眼就可以了。它唯一没有做的是让AlertPanel在任何一点都可见,尽管它确实隐藏了六次。我建议使用调试器来检查究竟发生了什么 - 我怀疑它看起来不起作用,因为你根本看不到面板。
A simpler solution would be to create an Event in your UserControl and subscribe to it from your Form - and that fits the model that all other Controls use as well.
That's pretty trivial to do, but I'm lazy - so I wrote a snippet to make it even simpler: A Simple Code Snippet to Add an Event[^]
Now all I do is type "evh" and hit TAB twice and it's done.

Your callback method doesn't check if there is a callback assigned - which it should - but other than that it looks ok to a casual glance. The only thing it doesn't do is make the AlertPanel visible at any point although it does hide it six times. I'd suggest using the debugger to check what exactly is happening - I suspect it appears not to work because you never see the panel at all.


这篇关于我有一个usercontrol,在这个usercontrol中我创建了一个委托调用winform方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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