저는 form1 안에 컨트롤을 추가하고 싶습니다. 에 대한 이벤트 처리기는 watcher.Changed + = new FileSystemEventHandler (OnChanged); 정의, 그것은 가능한 Form1에 예를 들어 목록 상자 컨트롤을 추가하지만이 당신을 찾고 무엇WinForm에서 이벤트가 발생하면 컨트롤을 추가하는 방법은 무엇입니까?
/*event added*/
private void btn_start_Click(object sender, EventArgs e)
{
string[] args = {this.txtfolder.Text};
if (args.Length != 1)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Invalid Operation");
return;
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[0];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = this.txtfilter.Text;//"*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
// watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
//Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
// Form1 F ;
// ListBox lst = new ListBox();
//lst.Items.Add("File: " + e.FullPath + " " + e.ChangeType.ToString());
//f.lsttracker.Items.Add("File: " + e.FullPath + " " + e.ChangeType.ToString());
// F.controls.Add(lst);
예, 가능합니다. 그러나 이벤트가 발생할 때마다 추가 될 것입니다. 이는 원하는 내용이 아닐 수 있습니다. 코드가 보여주고있는 것을 말하기가 어렵습니다. 왜 그 모든 것들이 주석 처리 되었습니까? –
시도해 보셨습니까? (주 : 가능한 질문이 보통 downvoted 끝나는가). –
@selvannand이 샘플을 사용해보십시오. 나는 이것이 당신이하려는 일을한다고 생각합니다 : dl.dropbox.com/u/18919663/vs%20samples/TestingSandbox.zip – Corylulu