public void OpenFile(string path) { if (!File.Exists(path)) { MainWindow.SetCurrStatus("文件不存在"); return; } var title = Path.GetFileName(path); var bundle = new TabBundle { IsNew = false, IsEdit = false, SrcPath = path, TabTitle = title, TabPage = CreateEditPage(title), }; bundle.TabPage.AllowDrop = true; bundle.TabPage.DragDrop += OnDragDrop; ((RichTextBox)bundle.TabPage.Controls.Find("RichTextBox", true).FirstOrDefault()).LoadFile(bundle.SrcPath, RichTextBoxStreamType.PlainText); TabControl.TabPages.Add(bundle.TabPage); var index = TabControl.TabPages.IndexOf(bundle.TabPage); TabControl.SelectedIndex = index; TabBundles.Add(bundle); }
/// <summary> /// 添加TabPage通过TabBundle /// 1. 打开文件 /// 2. 新建文件 /// </summary> /// <param name="bundle">绑定TabPage与FilePath</param> public void Add(TabBundle bundle) { // 创建TabPage var form = CreateForm(EditFormClassName); TabPage page = CreateTabPage(bundle.TabTitle, CreateForm(EditFormClassName)); page.Tag = new TabPageNodeBase { Title = bundle.TabTitle, Type = PageNodeType.Edit.ToString(), }; //page.AllowDrop = true; TabBundles.Add(new TabBundle { TabTitle = bundle.TabTitle, IsNew = !((!string.IsNullOrWhiteSpace(bundle.SrcPath)) && File.Exists(bundle.SrcPath)), TabPage = page }); if ((!string.IsNullOrWhiteSpace(bundle.SrcPath)) && File.Exists(bundle.SrcPath) /* && bundle.SrcFilePath.isPath()*/) { ((RichTextBox)page.Controls.Find("RichTextBox", true).FirstOrDefault()).LoadFile(bundle.SrcPath, RichTextBoxStreamType.PlainText); } bundle.TabPage = page; TabControl.TabPages.Add(page); var index = TabControl.TabPages.IndexOf(page); TabControl.SelectedIndex = index; }
/// <summary> /// 新建文件菜单--菜单>文件>新建 /// </summary> /// <param name="sender">新建文件菜单对象</param> /// <param name="e"></param> private void NewMenuItem_Click(object sender, EventArgs e) { var budle = new TabBundle { IsNew = true, TabTitle = "新增选项卡" + TabAdapter.NextNo.ToString().PadLeft(3, '0'), }; TabAdapter.Add(budle); SetCurrStatus($"新建标题:{budle.TabTitle}"); }