设置其他表单的按钮可见性 [英] set a button visibility from another form

查看:99
本文介绍了设置其他表单的按钮可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置其他形式的按钮可见性?(在桌面应用程序中)

how can i set a button visibility from another form?(in desktop application)

推荐答案

有很多方法可以做到这一点,但是适当的机制确实取决于形式之间的关系.如果我们假设要在其上设置按钮可见性的表单是打开您的表单的表单,并且您已模态地打开了该表单,那么您的子表单上应该有一个事件,父母要订阅.调用事件时,父窗体将处理该事件,并更改按钮的可见性.

您应该从描述中获得的关键是,您永远不要直接尝试影响单独的UI区域.换句话说,只有需要更改按钮可见性的表单中应该包含告诉它这样做的逻辑,并且它应该基于外部刺激(例如事件)来触发此操作.


在子窗体中,创建一个事件:
There are numerous ways to do this, but the appropriate mechanism really depends on what the relationship is betwen the forms. If we assume that the form you want to set the button visibility on is the one that opened your form, and that you have opened it modally, then you should have an event on your child form that the parent subscribes to. When the event is invoked, the parent form will handle the event, and change the visibility of the button.

The key thing you should get from the description is that you should never directly try to affect separate UI areas. In other words, only the form that needs the button visibility changed should have the logic in it that tells it to do so, and it should trigger this based on external stimulus, such as an event.


In the child form, create an event:
public event EventHandler DataNotPresent;

向您的子窗体添加一些代码以实际触发该事件:

Add some code to your child form to actually trigger the event:

private void OnDataNotPresent()
{
  EventHandler handler = DataNotPresent;
  if (handler != null)
  {
    handler(this, EventArgs.Empty);
  }
}

在父表单中,以显示子表单:

In the parent form, to Show the child form:

childForm = new MyChildForm(); // Assumes you've created a member level variable.
childForm.DataNotPresent += DataNotPresentHandler;
childForm.Show();

向其添加处理程序:

private void DataNotPresentHandler(object sender, EventArgs e)
{
  // Hide your button here
}

别忘了在子窗体关闭时取消引用您的事件.

Don''t forget to dereference your events when the child form closes.


我认为Dave的 ^ ]是有意义的.

I think Dave''s explanation[^] about parent-child relationship makes sense.

Dave Kreskowiak写道:
Dave Kreskowiak wrote:

嗯,可以,但这不是直截了当的,这是一个可怕的想法.表单上的控件永远不能被其他表单代码所触及.

Well, you could, but it''s not straight forward and is a horrible idea. Controls on a form should never be touched by some other forms code.





Dave Kreskowiak写道:
Dave Kreskowiak wrote:

父级应始终可以选择是否选择数据或订阅事件.孩子们永远不要将数据推送到父母的喉咙.

The parent should always have the option of picking up data or not or subscribing to an event or not. The children should never shove data down the throat of a parent.


这篇关于设置其他表单的按钮可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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