/// <summary> /// 获取当前标签的文本框 /// </summary> /// <returns></returns> public static TextBox GetSelectPageTextBox() { Control con = null; con = ControlCacheFactory.getSingletonCache(DefaultNameEnum.TAB_CONTENT); if (con is TabControl) { TabControl redrawTab = (TabControl)con; // 获取标签容器中当前标签的文本框 con = ControlsUtils.GetControlByName(redrawTab.SelectedTab.Controls, EnumUtils.GetDescription(DefaultNameEnum.TEXTBOX_NAME_DEF), true); } // 转化为文本框 TextBox textBox = con != null && con is TextBox?(TextBox)con : null; return(textBox); }
/// <summary> /// 监听文件变化并弹出提示框提示重新加载或另存为 /// </summary> /// <param name="filepath"></param> /// <param name="t"></param> public static void monitorFileTextShowMess(string filepath, TextBox t) { Dictionary <Type, object> data = new Dictionary <Type, object>(); data.Add(typeof(TextBox), t); // 监听文件变化 try { FileSystemWatcher wat = null; string[] pathArr = GetPathArr(filepath); Encoding encoding = Encoding.Default; if (!"txt".Equals(pathArr[2].ToLower())) { encoding = GetType(filepath); } // 判断文本框的Tag中是否纯在一个监听,存在就销毁他 if (TextBoxUtils.GetTextTagToMap(t).ContainsKey(TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR)) { object obj = TextBoxUtils.GetTextTagToMap(t)[TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR]; if (obj.GetType().Equals(typeof(FileSystemWatcher))) { wat = (FileSystemWatcher)TextBoxUtils.GetTextTagToMap(t)[TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR]; wat.EnableRaisingEvents = false; wat.Dispose(); } } // 获取一个新的文件监听 wat = fileMonitor(pathArr[0], pathArr[1] + "." + pathArr[2], NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.Size , null, null , delegate(object sender, FileSystemEventArgs e){ FileSystemWatcher watcher = (FileSystemWatcher)sender; // 闪烁窗体 Form f = t.FindForm(); if (f.InvokeRequired) { f.Invoke(new EventHandler(delegate { System.Media.SystemSounds.Asterisk.Play(); WindowsApiUtils.FlashWindow(f.Handle, 400, 3); })); } // 弹出对话框 ControlsUtils.ShowAskMessBox("文件内容已经更改,是否要重新加载文件", "提示" , delegate { if (t.InvokeRequired) { t.Invoke(new EventHandler(delegate { // 获取内容 string text = FileRead.Read(filepath, encoding); t.Text = text; })); } }, null); watcher.EnableRaisingEvents = false; } , delegate { // 弹出对话框 MessageBox.Show("文件在磁盘上已经被删除或重命名!"); }); // 加入到文本框的tag数据中 TextBoxUtils.TextBoxAddTag(t, TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR, wat); } catch { MessageBox.Show("监听文件状态时发生异常"); } }