this is my first question on stackoverflow, therefore errors in asking-style aren't on purpose.
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.
The context is the following:
The code starts by initializing the UI which contains some Windows.Forms- Elements. The ToolStripButtomItem
that is of interest for me, has it's Enabled-attribute
set to false as default-value. 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. 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.
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.
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 ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.