public void SetHostList(HostList hostList) { this.hostList = hostList; ReloadList(); }
/// <summary> /// 直接编辑Host文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnEditSourceClick(object sender, EventArgs e) { if (!btnEditSource.Checked) { // 请求查看源代码 if (Edited) { Save(sender, e); } TextBox txtSource = new TextBox() { Text = File.ReadAllText(Host.SYSTEM_HOST_PATH, System.Text.Encoding.Default), Font = new System.Drawing.Font("Consolas", 9), Multiline = true, ScrollBars = ScrollBars.Vertical, Location = listViewHost.Location, Size = listViewHost.Size, Anchor = listViewHost.Anchor, Parent = listViewHost.Parent }; txtSource.TextChanged += OnSourceChanged; txtSource.KeyDown += OnKeyDown; btnEditSource.Tag = txtSource; listViewHost.Visible = false; btnEditSource.Checked = true; } else { // 退出源代码查看模式 TextBox txtSource = btnEditSource.Tag as TextBox; if (Edited) { var result = MessageBoxEx.Confirm("是否保存源代码的修改?"); if (result == DialogResult.Cancel) { return; } if (Edited && result == DialogResult.Yes) { Save(sender, e); } Edited = false; } hostList = new HostList(Host.SYSTEM_HOST_PATH); listViewHost.SetHostList(hostList); txtSource.Dispose(); listViewHost.Visible = true; btnEditSource.Checked = false; } btnAdd.Enabled = btnEdit.Enabled = btnDelete.Enabled = btnImport.Enabled = btnExport.Enabled = dropDownView.Enabled = !btnEditSource.Checked; }