C#_窗体传参

前言:

  软件版本:Visual Studio 2019

一、设置父窗体

1、添加父窗体

打开窗体1

2、添加父窗体代码


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);
        }
    }
    

二、设置子窗体1

1、添加子窗体1

在这里插入图片描述

2、设置控件“Modifiers”值为:Public

在这里插入图片描述

3、添加子窗体1代码


    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");
        }
    }
    

4、演示视频

  点击查看:演示视频。

三、设置子窗体2

1、添加子窗体2

在这里插入图片描述

2、设置控件“Modifiers”值为:Public

在这里插入图片描述

3、添加子窗体2代码


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;
        }
    }
    

4、添加父窗体代码


            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();
            

5、演示视频

  点击查看:演示视频。

四、成果

  Demo成品:点击下载。
  Demo源码:点击下载。

五、后记

  如果不是必须使用窗体,可以考虑使用用户控件。

            UserControl1 f4 = new UserControl1();
            f4.Dock = DockStyle.Fill;
            panel1.Controls.Add(f4);
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C# WinForm中,可以通过以下几种方式实现窗体之间的参数传递: 1. 构造函数传参:在打开新窗体时,在构造函数中传入参数,并在新窗体中接收参数。示例代码如下: ``` // 在父窗体中打开子窗体 ChildForm childForm = new ChildForm(param1, param2); childForm.Show(); // 在子窗体中接收参数 public ChildForm(string param1, int param2) { InitializeComponent(); // 对参数进行操作 } ``` 2. 属性传参:在打开新窗体时,在新窗体中定义公共属性,通过属性传递参数。示例代码如下: ``` // 在父窗体中打开子窗体 ChildForm childForm = new ChildForm(); childForm.Param1 = param1; childForm.Param2 = param2; childForm.Show(); // 在子窗体中定义公共属性 public string Param1 { get; set; } public int Param2 { get; set; } // 在子窗体中使用属性 private void ChildForm_Load(object sender, EventArgs e) { // 对属性进行操作 } ``` 3. 静态变量传参:在打开新窗体时,在新窗体中定义静态变量,通过静态变量传递参数。示例代码如下: ``` // 在父窗体中打开子窗体 ChildForm.Param1 = param1; ChildForm.Param2 = param2; ChildForm childForm = new ChildForm(); childForm.Show(); // 在子窗体中定义静态变量 public static string Param1 { get; set; } public static int Param2 { get; set; } // 在子窗体中使用静态变量 private void ChildForm_Load(object sender, EventArgs e) { // 对静态变量进行操作 } ``` 以上三种方式都可以实现窗体之间的参数传递,具体选择哪种方式取决于应用场景和个人习惯。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

momo_al

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值