简体   繁体   English

使用EventHandler更改ToolStripButtomItem属性

[英]Change ToolStripButtomItem Attributes Using EventHandler

this is my first question on stackoverflow, therefore errors in asking-style aren't on purpose. 这是我关于stackoverflow的第一个问题,因此提问式错误不是故意的。

I'm both, new to C# as to the concept of event-handling, so I would like to know, if there is any possibility to have the value of an ToolStripButtonItem-attribute changed by an EventHandler. 关于事件处理的概念,我都是C#的新手,所以我想知道,是否有可能通过EventHandler更改ToolStripButtonItem属性的值。

The context is the following: 上下文如下:

The code starts by initializing the UI which contains some Windows.Forms- Elements. 该代码从初始化包含一些Windows.Forms元素的UI开始。 The ToolStripButtomItem that is of interest for me, has it's Enabled-attribute set to false as default-value. 我感兴趣的ToolStripButtomItemEnabled-attribute默认值设置为false。 The functionality of this button is to switch into a comparision-view as soon as a certain reference file exists. 该按钮的功能是,一旦存在某个参考文件,便会切换到比较视图。 This allready can be the case when the programm-start, otherwise the reference file might be created during runtime. 在programm-start时已经存在这种情况,否则可能会在运行时创建参考文件。 Of course, you could perform 当然可以

Button.Enabled=System.IO.File.Exists(Reference-File) 

with the initilization and than do something like 与初始化,然后做类似的事情

CreateFile(ReferenceFile){
    ...
    Button.Enabled = true;
}

but this seems rather crude to me. 但这对我来说似乎很粗糙。

Instead I would like to something like: 相反,我想这样:

Button.Enabled = new System.EventHandler(this.EnableButton);

with

private void EnableButton(Object sender, EventArgs e){
  if(System.IO.FileExists(ReferenceFile)
  Button.Enabled = true; 
}  

What I intend is, to have the button get enabled as soon as the reference-file existst. 我的目的是在参考文件存在后立即启用按钮。 There are multiple ways to create the reference-file, and there are goint to be even more in the future. 创建引用文件的方法有多种,将来还会有更多。 To avoid setting the enable-value in each of those createReferenceFile() -Methods, the concept of EventHandling seems quite like the deal to me. 为了避免在每个createReferenceFile() Methods中设置启用值,EventHandling的概念对我来说似乎很重要。

The program I'm trying to run is quite comprehensive, so "polling" is no option at this place. 我要运行的程序非常全面,因此在此位置“轮询”是不可行的。

I suggest to use the FileSystemWatcher and set the enabled property every time it goes of. 我建议每次使用FileSystemWatcher并设置enabled属性。

    private void StartListening(string path)
    {
        var watch = new FileSystemWatcher();
        watch.Path = path;
        watch.Filter = "*.*";
        watch.Created += UpdateState;
        watch.Deleted += UpdateState;
    }

    void UpdateState(object sender, FileSystemEventArgs e)
    {
        MyButton.Enabled = File.Exists(@"C:\Folder\File.txt");
    }

PS: this is just some very basic example code, you will need to make sure that you have a reference to MyButton and have the correct path there as well ... PS:这只是一些非常基本的示例代码,您需要确保您有对MyButton的引用,并且在那里也有正确的路径...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM