Tag: 事件处理

我可以访问内部类中的外部类对象

我有三个这样的课程。 class A { public class innerB { //Do something } public class innerC { //trying to access objB here directly or indirectly over here. //I dont have to create an object of innerB, but to access the object created by A //ie innerB objInnerB = objB; //not like this innerB objInnerB= new innerB(); } […]

如何使用委托和事件处理程序进行用户控制

我创建了一个包含按钮的用户控件。 我在我的winform上使用这个控件,它将在从数据库中获取数据后在运行时加载。 现在我需要从该按钮的Click事件中的数据表中删除一行。 问题是我如何在表单中捕获该事件。 目前它进入用户控件的btn点击事件定义。

如何在WPF上下文菜单项单击事件处理程序中引用右键单击的对象?

在WPF应用程序中,有一个包含许多对象的Grid (它们来自自定义控件)。 我想使用上下文菜单对每个操作执行一些操作: 但是在事件处理程序中,我无法知道哪些对象被右键单击: private void EditStatusCm_Click(object sender, RoutedEventArgs e) { MyCustControl SCurrent = new MyCustControl(); MenuItem menu = sender as MenuItem; SCurrent = menu.DataContext as MyCustControl; // here I get a run-time error SCurrent.Status = MyCustControl.Status.Sixth; } 在该注释行上调试器说: 对象引用未设置为对象的实例。 请帮忙,我的代码有什么问题? 编辑(补充): 我尝试使用Command方法做同样的事情: 我使用RoutedUICommand Requery声明了一个RoutedUICommand Requery类,然后使用了Window.CommandBindings MenuItem的XAML现在看起来像: 事件处理程序如下所示: private void RequeryCommand_Executed(object sender, ExecutedRoutedEventArgs e) { […]

来自其他类的事件被触发时通知

我有 class A { B b; //call this Method when b.Button_click or b.someMethod is launched private void MyMethod() { } ?? } Class B { //here ie a button is pressed and in Class A //i want to call also MyMethod() in Class A after the button is pressed private void Button_Click(object o, EventArgs s) […]

如何在控件外部单击时关闭Silverlight中的弹出窗口?

在我的Silverlight UI中,我有一个按钮,当单击时会弹出一个带有一些过滤参数的控件。 当你在它外面点击时,我希望这个控件隐藏起来。 换句话说,它应该以类似于combobox的方式运行,但它不是combobox(您不选择其中的项目)。 这是我试图捕获控件外部的点击以解除它的方式: public partial class MyPanel : UserControl { public MyPanel() { InitializeComponent(); } private void FilterButton_Click(object sender, RoutedEventArgs e) { // Toggle the open state of the filter popup FilterPopup.IsOpen = !FilterPopup.IsOpen; } private void UserControl_Loaded(object sender, RoutedEventArgs e) { // Capture all clicks and close the popup App.Current.RootVisual.MouseLeftButtonDown += delegate […]

通过EventHandler传回返回值

我试图写入API,我需要在从表中获取数据时调用eventhandler。 像这样的东西: public override bool Run(Company.API api) { SomeInfo _someInfo = new SomeInfo(); if (_someInfo.Results == 1) return true; else return false; using (MyTable table = new MyTable(api)) { table.WhenData += new EventHandler<DataEventArgs>(table_WhenData); table.WhenDead += new EventHandler(table_WhenDead); table.Start(); } public void table_WhenData(object sender, DataEventArgs e) { return true; } 我遇到的问题是我不知道如何将返回值从table_WhenData传递回Run方法。 我尝试了很多方法(比如尝试将_someInfo传递给方法),但我似乎无法正确使用语法。 任何建议都非常感谢。

跨多个控件共享事件处理程序

在用C#编写的Windows窗体应用程序中,我有一堆按钮。 当用户的鼠标hover在按钮上时,我希望按钮的边框发生变化。 目前我有以下多个实例(每个按钮的副本): private void btnStopServer_MouseEnter(object sender, EventArgs e) { oldColor = btnStopServer.FlatAppearance.BorderColor; btnStopServer.FlatAppearance.BorderColor = mouseOverColor; } private void btnStopServer_MouseLeave(object sender, EventArgs e) { btnStopServer.FlatAppearance.BorderColor = oldColor; } 由于我有很多按钮,更改按钮边框颜色的代码占用了大量空间。 有没有更简单的方法可以做到这一点?

UIElement.AddHandler()vs .Event + = Definition

1.问题的一部分:这两个事件注册有什么区别? _popUp.AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopUp_PreviewMouseLeftButtonDown)); _popUp.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(_popUp_PreviewMouseLeftButtonDown); 2.问题的一部分:或最终与…相关 popUp.Opened += PopUp_Opened;

为什么不使用自定义类而不是inheritanceEventArgs类

我想知道为什么我应该使用从EventArgs类inheritance的类而不是使用在传递事件数据时为我做同样工作的自定义类?

如何获得右键单击鼠标事件? 将EventArgs更改为MouseEventArgs会导致Form1Designer中出错?

我有一种方法可以通过双击表单来检测visual studio所做的左键单击事件。 private void pictureBox1_Click(object sender, EventArgs e) { MessageBox.Show(“Left click”); } 我希望通过右键单击同一对象来进行右键单击事件。 我在网上看到你可以使用这个开关: private void pictureBox1_Click(object sender, EventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Right){MessageBox.Show(“Right click”);} if (e.Button == System.Windows.Forms.MouseButtons.Left){MessageBox.Show(“Left click”);} } 麻烦的是,当我这样做时,e.Button有一条红线和错误: “‘System.EventArgs’ does not contain a definition for ‘Button’… ” 所以我通过将“EventArgs e”改为“MouseEventArgs e”来解决这个问题。 但是Form1Designer中有一个新错误,其中事件行是: this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click); 错误说: “No overload for ‘pictureBox1_Click’ […]