后端开发
C#父窗体传值给子窗体方法
发布时间:2024-04-01 22:40:10 浏览量:60
Form1类中代码:
using System;
using System.Windows.Forms;
namespace WindowsForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string nodename = treeView1.SelectedNode.Text.ToString(); //获取选中树节点的名称
int nodeid = Convert.ToInt16(treeView1.SelectedNode.Tag); //获取选中树节点的分类ID
Form2 frm2 = new Form2(nodename,nodeid);
frm2.Show();
}
}
}
Form2类中代码:
using System;
using System.Windows.Forms;
namespace WindowsForms跨窗体传值大全
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string str)
{
InitializeComponent();
string nodename = name; //存储树节点的名称
int nodeid = Convert.ToInt16(id); //存储节点的分类ID
textBox1.Text = nodename;
}
}
}