窗口形式C#中的按钮单击事件的委托和事件 [英] Delegate and event on button click event in window form C#

查看:547
本文介绍了窗口形式C#中的按钮单击事件的委托和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,







 

任何正文都可以在窗口形式C#中给出一个关于单击按钮事件的委托和事件的简单示例。就像我点击按钮事件,点击按钮上的配置将触发。





 使用系统; 
使用 System.Windows.Forms;

namespace WindowsFormsApplication21
{
public partial class Form1:Form
{
public delegate void BoilerLogHandler( string status);
public event BoilerLogHandler BoilerEventLog;
public Form1()
{
InitializeComponent();

}

private void button1_Click(< span class =code-keyword> object sender,EventArgs e)
{
MessageBox.Show( Dot Net Perls说你好。
怎么样?你的一天?);
}
}
}





谢谢



我尝试了什么:



如何在窗口形式c#

解决方案

你可能意思是这样的:

  private  委托  void  BoilerLogHandler( string 状态); 
BoilerLogHandler theDelegate = BoilerLogHandlerB;
private void myCheckBox_CheckedChanged( object sender,EventArgs e)
{
if (myCheckBox.Checked)
{
theDelegate = BoilerLogHandlerA;
}
else
{
theDelegate = BoilerLogHandlerB;
}
}

private void myButton_Click( object sender,EventArgs e)
{
theDelegate( 点击按钮即可获得Hello!);
}
私有 静态 void BoilerLogHandlerA( string status)
{
Console.WriteLine( A:{0},status);
}
私有 静态 void BoilerLogHandlerB( string status)
{
Console.WriteLine( B:{0},status);
}



当您更改复选框的状态时,它会更改委托。当你点击按钮时,委托用于调用当前设置的任何方法。

对于一个事件,它略有不同:

  ///   <  摘要 >  
/// < span class =code-comment>表示测试事件抛出的事件
/// < / summary >
public event EventHandler TestEvent;
/// < 摘要 >
/// 调用以向订阅者发出测试事件抛出
/// < / summary > ;
/// < param name =e > < / param >
protected virtual void OnTestEvent(EventArgs e)
{
EventHandler eh = TestEvent;
if (eh!= null
{
eh( ,e);
}
}
私有 void myCheckBox_CheckedChanged( object sender,EventArgs e)
{
if (myCheckBox.Checked)
{
TestEvent + = frmMain_TestEventA;
}
else
{
TestEvent + = frmMain_TestEventB;
}
}

void frmMain_TestEventA( object sender,EventArgs e)
{
Console.WriteLine( EventA) ;
}

void frmMain_TestEventB( object sender,EventArgs e)
{
Console.WriteLine( EventB);
}

private void myButton_Click( object sender,EventArgs e)
{
OnTestEvent( new EventArgs());
}



现在每次更改CheckBox时,都会在事件中添加另一个处理程序。

因为事件是在内部实现的通过委托,你也可以使用+ =语法与委托链接方法,但直接做的更为罕见。


如何使用委托 -



  public   delegate   void  PalashDelegate(); 

class TestExpDelegate
{
public static void MyFunc()
{
Console.WriteLine( 我被委托调用了......);
}

public static void Main()
{
// Instantiation
PalashDelegate PalashDelegate = new PalashDelegate(MyFunc);

// 调用
PalashDelegate();
}
}







如何使用活动 -





  class  TestExpEvent 
{
public event EventHandler chwEventFire_click;

TestExpEvent()
{
this .chwEventFire_click + = new EventHandler(Obj_chwEventFire_click);
<跨度类= 代码关键字>此 .chwEventFire_click + = <跨度类= 代码关键字>新事件处理程序(Obj_chwEventFire_click);
Console.Write( 构造函数);
}

private void Button_Click(对象发件人,RoutedEventArgs E)
{
<跨度类= 代码关键字 >如果(chwEventFire_click!= <跨度类= 代码 - 关键字> null

{
chwEventFire_click( this new EventArgs());
}
}

void Obj_chwEventFire_click( object sender,EventArgs e)
{
Console.Write( Event fire );
}

}









你也可以参考这个链接。



第17章代表和事件 [ ^ ]


对不起,没有代码给你,因为这个问题毫无意义。这并不意味着做你所要求的是可能的或不可能的;这只是因为您使用的是不存在或错误理解的概念。请看我对这个问题的评论。



你真的需要了解面向事件的编程是如何工作的。这是开始的事情:

控制倒置 - 维基百科,免费的百科全书 [< a href =https://en.wikipedia.org/wiki/Inversion_of_controltarget =_ blanktitle =New Window> ^ ],

事件编程 - 维基百科,免费的百科全书 [ ^ ],

降级(C#编程指南) [ ^ ],

代表教程(C#) [ ^ ],

活动教程(C#) [ ^ ],

< a href =https://msdn.microsoft.com/en-us/library/edzehd2t%28v=vs.110%29.aspx>处理和提升事件 [ ^ ]。



我想警告你:在你学习并非常清楚地理解上述所有内容之前,你甚至不想在此时进行UI开发。这将是烹饪食谱的编程,这将是非常糟糕的。你必须学习所有这些,并且最好的方法是首先基于非常简单的仅限控制台练习。



-SA

Hello,



Can any body give me a simple example of delegate and event on click button event in window form C#. like if i click on button event which is configure on click button will fire.


using System;
using System.Windows.Forms;

namespace WindowsFormsApplication21
{
    public partial class Form1 : Form
    {
        public delegate void BoilerLogHandler(string status);
        public event BoilerLogHandler BoilerEventLog;
	public Form1()
	{
	    InitializeComponent();

	}

	private void button1_Click(object sender, EventArgs e)
	{
	    MessageBox.Show("Dot Net Perls says hello.",
		"How is your day going?");
	}
    }
}



Thanks

What I have tried:

how to fire delegate and event on button click event in window form c#

解决方案

It possible you mean something like this:

private delegate void BoilerLogHandler(string status);
BoilerLogHandler theDelegate = BoilerLogHandlerB;
private void myCheckBox_CheckedChanged(object sender, EventArgs e)
    {
    if (myCheckBox.Checked)
        {
        theDelegate = BoilerLogHandlerA;
        }
    else
        {
        theDelegate = BoilerLogHandlerB;
        }
    }

private void myButton_Click(object sender, EventArgs e)
    {
    theDelegate("Hello from a button click!");
    }
private static void BoilerLogHandlerA(string status)
    {
    Console.WriteLine("A: {0}", status);
    }
private static void BoilerLogHandlerB(string status)
    {
    Console.WriteLine("B: {0}", status);
    }


When you change the status of the checkbox, it changes the delegate. And when you click the button, the delegate is used to call whichever method is currently set.
For an event it's slightly different:

/// <summary>
/// Event to indicate Test Event Thrown
/// </summary>
public event EventHandler TestEvent;
/// <summary>
/// Called to signal to subscribers that Test Event Thrown
/// </summary>
/// <param name="e"></param>
protected virtual void OnTestEvent(EventArgs e)
    {
    EventHandler eh = TestEvent;
    if (eh != null)
        {
        eh(this, e);
        }
    }
private void myCheckBox_CheckedChanged(object sender, EventArgs e)
    {
    if (myCheckBox.Checked)
        {
        TestEvent +=frmMain_TestEventA;
        }
    else
        {
        TestEvent += frmMain_TestEventB;
        }
    }

void frmMain_TestEventA(object sender, EventArgs e)
    {
    Console.WriteLine("EventA");
    }

void frmMain_TestEventB(object sender, EventArgs e)
    {
    Console.WriteLine("EventB");
    }

private void myButton_Click(object sender, EventArgs e)
    {
    OnTestEvent(new EventArgs());
    }


Now each time you change the CheckBox, another handler is added to the event.
Since an event is internally implemented via a delegate, you can also use the "+=" syntax with delegates to "chain" methods into it, but that's a little more rare to do directly.


How to Use Delegate -

public delegate void PalashDelegate();

        class TestExpDelegate
        {
            public static void MyFunc()
            {
                Console.WriteLine("I was called by delegate ...");
            }

            public static void Main()
            {
                // Instantiation
                PalashDelegate PalashDelegate = new PalashDelegate(MyFunc);

                // Invocation
                PalashDelegate();
            }
        }




How to use Event -


class TestExpEvent
{
   public event EventHandler chwEventFire_click;

   TestExpEvent()
   {                
       this.chwEventFire_click += new EventHandler(Obj_chwEventFire_click);
       this.chwEventFire_click += new EventHandler(Obj_chwEventFire_click);
       Console.Write("Constructor");
   }

   private void Button_Click(object sender, RoutedEventArgs e)
   {
       if (chwEventFire_click != null)
       {
           chwEventFire_click(this, new EventArgs());
       }
    }
       
    void Obj_chwEventFire_click(object sender, EventArgs e)
    {
       Console.Write("Event fire");
    }   
            
}





You can refer this link also.

Chapter 17. Delegates and Events[^]


Sorry, no code for you, because the question makes no sense. It does not mean that doing what you ask is possible or impossible; it's just because you are using non-existing or incorrectly understood concepts. Please see my comment to the question.

You really need to understand how event-oriented programming works. This is something to start with:
Inversion of control — Wikipedia, the free encyclopedia[^],
Event-driven programming — Wikipedia, the free encyclopedia[^],
Delegates (C# Programming Guide)[^],
Delegates Tutorial (C#)[^],
Events Tutorial (C#)[^],
Handling and Raising Events[^].

I want to warn you: don't even play with the idea of doing UI development at this moment, before you learn and very clearly understand all of the above. It would be programming by cookbook recipes, which would be very bad. You have to learn all that, and the best way would be doing it based on very simple console-only exercises at first.

—SA


这篇关于窗口形式C#中的按钮单击事件的委托和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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