如何在另一个控件中检查一个控件 [英] How to Check a Control in another Control

查看:106
本文介绍了如何在另一个控件中检查一个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个小问题.让我与您分享.

假设我们有一堆按钮,复选框,菜单项等.[我的意思是我们有不同类型的控件.]当我按下按钮时,会发生某些事情(例如缩放图片).

然后我点击另一个按钮;在第二个按钮控件中,我要检查是否按下了第一个按钮.

我的意思的示例伪代码:

Hi Everyone,

I have a little problem. Let me share with you.

Let''s say we have a bunch of buttons, checkBoxes, menuitems etc. [What I mean here is we have different kinds of controls.] When I presses a button something happens[like zooming a picture].

And then I''m clicking another button; and in 2nd buttons control, I want to check that if the 1st button is pressed.

Sample pseudocode for what I mean:

private void button1_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.InitialDirectory = @"C:\";
    saveFileDialog1.DefaultExt = ".bmp";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        ImOrtamCizdir.genelBmpNesnesi.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
    }
}

private void button2_Click(object sender, EventArgs e)
{

    // What I ask is here.
    // if button1 is pressed?!
    if(button1.IsPressed())
    {
        // do something...
    }


     KontrolleriAc();
     bmp = new Bitmap(p_box_map.Width, p_box_map.Height);
     ImOrtamCizdir ortamcizdir = new ImOrtamCizdir(p_box_map, bmp, ZoomKontrolCarpan);

     bmp.RotateFlip(RotateFlipType.Rotate180FlipX);

     lbxKucultmeOranlari.Items.Add("scale_factor_x :" + ortamcizdir.ScaleFactorY);
     lbxKucultmeOranlari.Items.Add("scale_factor_y :" + ortamcizdir.ScaleFactorX);
     lbxKucultmeOranlari.Items.Add(ortamcizdir.ScaleFactor);
}





有可能吗?

我们可以检查另一个控件中控件的情况吗?

对不起,我的英语不好...
我最好的问候...





is it possible?

Can we check a control''s situation in another control?

Sorry for my bad English...
My Best Regards...

推荐答案

不要这样做.

您的控件应反映业务对象的状态(无论是通过正式数据绑定,还是在简单情况下,通过在事件处理程序中设置属性).在一个非常简单的应用程序(一种表单)中,业务对象"可以是表单的本地字段.然后,在其他地方(例如您正在谈论的按钮的事件处理程序),您可以查找这些相同的对象.

为实现这一理想,对您提供的代码的最简单修改是:

Don''t do it.

Your controls should reflect the state of business objects (whether through formal data binding or, for a simple case, through setting properties in event handlers). In a really simple application (one form) the ''business objects'' can be local fields of the form. Then, in other places (such as the event handler for the button you are talking about), you can look up into those same objects.

The simplest modification of your provided code towards this ideal is:

private bool isSaved = false;

private void button1_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.InitialDirectory = @"C:\";
    saveFileDialog1.DefaultExt = ".bmp";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;
 
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        ImOrtamCizdir.genelBmpNesnesi.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
    }

    isSaved = true;
}
 
private void button2_Click(object sender, EventArgs e)
{
 
    // What I ask is here.
    // if button1 is pressed?!
    if(isSaved)
    {
        // do something...
    }
 
     KontrolleriAc();
     bmp = new Bitmap(p_box_map.Width, p_box_map.Height);
     ImOrtamCizdir ortamcizdir = new ImOrtamCizdir(p_box_map, bmp, ZoomKontrolCarpan);
 
     bmp.RotateFlip(RotateFlipType.Rotate180FlipX);
 
     lbxKucultmeOranlari.Items.Add("scale_factor_x :" + ortamcizdir.ScaleFactorY);
     lbxKucultmeOranlari.Items.Add("scale_factor_y :" + ortamcizdir.ScaleFactorX);
     lbxKucultmeOranlari.Items.Add(ortamcizdir.ScaleFactor);
}



在真实的应用程序中,您需要存储文件名,将其绑定到表单的标题栏上,依此类推–并且您可能希望该信息位于主表单之外的其他位置.有时,我创建一个"App","DataModel"或"GlobalState"类,以使其真正显露什么是状态信息.有时我将拥有一个"Project"类或类似的类,用于管理与单个文件有关的数据和逻辑,并且可以存储状态信息.

通常,对于任何类型的非UI逻辑都直接依赖于UI控件是一个一个非常糟糕的主意.您应该能够重新设计UI,或者将核心代码重用为没有UI的后端库,并且像这样创建依赖项会阻止您这样做,并导致意大利面条式代码"问题.



In a real application you''d want to store the file name, bind that to the titlebar of the form and so on – and you''d probably want that information to be somewhere other than in your main form. Sometimes I create an ''App'', ''DataModel'' or ''GlobalState'' class to make it really explicit what is state information; other times I will have a ''Project'' class or similar which manages the data and logic relating to a single file, and that can store the state information.

In general, having direct dependencies on UI controls for any kind of non-UI logic is a really bad idea. You should be able to redesign the UI, or reuse the core code as a back end library with no UI, and creating dependencies like this stops you from doing that and results in ''spaghetti code'' problems.


鲍勃的回答非常好.我只想补充一点:在处理其他按钮的单击时,您不能单击被按下的按钮.目前只能按下一个按钮.

如果不是,则有3状态按钮,例如WPF System.Windows.Controls.Primitives.ToggleButton,因此它具有属性IsPressed.请参阅 http://msdn.microsoft.com/en-us/library /system.windows.controls.primitives.togglebutton.aspx [ ^ ].

我认为您是在谈论常规按钮.对于他们来说,您的询问毫无意义.

—SA
The answer by Bob is very good. I only want to add: you can not click a button pressed while a click of other button is being handled. Only one button can be pressed at the moment.

There are 3-state buttons when this is not so, for example WPF System.Windows.Controls.Primitives.ToggleButton, so it has a property IsPressed. See http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.aspx[^].

I think you''re talking about regular buttons. For them, your inquiry makes no sense.

—SA


如果容器同时具有两个控件,则可以看到它们的状态.

If the container has both controls then it can see the state of them.

// not real code
class MyForm {
  Button button1;
  Button button2;

  // more stuff

  void button2_click(ButtonClickEvent ev) {
    if (button1.someProperty) {
      // do this
    } else {
      // do that    }
  }
}


这篇关于如何在另一个控件中检查一个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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