软件版本:Visual Studio 2019
打开 | 窗体1 |
---|---|
public partial class Form1 : Form
{
Form2 f2 = null;
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
}
private void 打开窗体1ToolStripMenuItem_Click(object sender, EventArgs e)
{
f2 = new Form2(this);
f2.MdiParent = this;
f2.WindowState = FormWindowState.Maximized;
f2.Text = "子窗体1";
f2.Show();
}
private void 设置值ToolStripMenuItem_Click(object sender, EventArgs e)
{
f2.textBox1.AppendText(DateTime.Now.ToString() + " 来自:" + Text + "\r\n");
}
private void 读取值ToolStripMenuItem_Click(object sender, EventArgs e)
{
string str = string.Empty;
for(int i = 0; i < f2.textBox1.Lines.Count(); i++)
{
str += f2.textBox1.Lines[i]+"\n";
}
MessageBox.Show(str);
}
}
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form f1)
{
this.f1 = (Form1)f1;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.AppendText(DateTime.Now.ToString() + " 来自:" + Text + "\r\n");
}
}
点击查看:演示视频。
public partial class Form3 : Form
{
Form1 f1;
public Form3(Form f1)
{
this.f1 = (Form1)f1;
InitializeComponent();
}
private void groupBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Purple, 1, 7, ((GroupBox)sender).Width - 2, 7);
e.Graphics.DrawLine(Pens.Purple, 1, 7, 1, ((GroupBox)sender).Height - 2);
e.Graphics.DrawLine(Pens.Purple, 1, ((GroupBox)sender).Height - 2, ((GroupBox)sender).Width - 2, ((GroupBox)sender).Height - 2);
e.Graphics.DrawLine(Pens.Purple, ((GroupBox)sender).Width - 2, 7, ((GroupBox)sender).Width - 2, ((GroupBox)sender).Height - 2);
}
public void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
public void listBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
public void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
DataGridView dataGridView = (DataGridView)sender;
dataGridView.Columns.Clear();
dataGridView.Columns.Add("文件名", "文件名");
dataGridView.Columns.Add("扩展名", "扩展名");
dataGridView.Columns.Add("路径", "路径");
for (int i = 0; i < ((System.Array)e.Data.GetData(DataFormats.FileDrop)).Length; i++)
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(i).ToString();
int r = dataGridView.Rows.Count;
dataGridView.Rows.Add();
dataGridView.Rows[r].Cells[0].Value = Path.GetFileNameWithoutExtension(path);
dataGridView.Rows[r].Cells[1].Value = "*" + Path.GetExtension(path);
dataGridView.Rows[r].Cells[2].Value = path;
}
}
public void listBox1_DragEnter(object sender, DragEventArgs e)
{
ListBox listBox = (ListBox)sender;
listBox.Items.Clear();
for (int i = 0; i < ((System.Array)e.Data.GetData(DataFormats.FileDrop)).Length; i++)
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(i).ToString();
listBox.Items.Add(path);
listBox.SelectedIndex = listBox.Items.Count - 1;
}
}
public ListBox ListBox_Add(GroupBox groupBox, string tips)
{
ListBox l = new ListBox();
l.Name = tips;
l.Parent = groupBox;
l.Dock = DockStyle.Fill;
l.BackColor = Control.DefaultBackColor;
l.BorderStyle = BorderStyle.None;
l.ScrollAlwaysVisible = true;
l.AllowDrop = true;
l.DragDrop += listBox1_DragDrop;
l.DragEnter += listBox1_DragEnter;
toolTip1.SetToolTip(l, "将“" + tips + "”文件拖到这里");
l.Show();
return l;
}
public DataGridView DataGV_Add(GroupBox groupBox, string tips)
{
DataGridView d = new DataGridView();
d.Name = tips;
d.Parent = groupBox;
d.Dock = DockStyle.Fill;
d.BackgroundColor = Control.DefaultBackColor;
d.BorderStyle = BorderStyle.None;
d.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
d.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
d.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
d.ReadOnly = true;
d.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
d.RowHeadersVisible = false;
d.ShowCellToolTips = false;
d.AllowUserToAddRows = false;
d.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
d.AlternatingRowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
d.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(255, 192, 192, 255);
d.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
d.DefaultCellStyle.BackColor = Color.FromArgb(255, 192, 255, 255);
d.AllowDrop = true;
d.DragDrop += dataGridView1_DragDrop;
d.DragEnter += dataGridView1_DragEnter;
toolTip1.SetToolTip(d, "将“" + tips + "”文件拖到这里");
d.Show();
return d;
}
}
f3 = new Form3(this);
f3.MdiParent = this;
f3.WindowState = FormWindowState.Maximized;
f3.Text = "添加文件:";
d1 = f3.DataGV_Add(f3.groupBox1, "文件1");
l1 = f3.ListBox_Add(f3.groupBox2, "文件2");
f3.Show();
点击查看:演示视频。
Demo成品:点击下载。
Demo源码:点击下载。
如果不是必须使用窗体,可以考虑使用用户控件。
UserControl1 f4 = new UserControl1();
f4.Dock = DockStyle.Fill;
panel1.Controls.Add(f4);