您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在.NET Winform应用程序中,有多种方法可以进行数据传输。以下是一些常见的数据传输方式:
public class Form1 : Form
{
public string Data { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string newData = "Hello from Form1";
Form2 form2 = new Form2();
form2.Data = newData;
form2.Show();
}
}
public class Form2 : Form
{
public string Data { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string receivedData = Data;
MessageBox.Show(receivedData);
}
}
public class Form1 : Form
{
public event EventHandler<string> DataTransferred;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string newData = "Hello from Form1";
DataTransferred?.Invoke(this, newData);
}
}
public class Form2 : Form
{
public Form2()
{
InitializeComponent();
Form1 form1 = new Form1();
form1.DataTransferred += Form2_DataTransferred;
form1.Show();
}
private void Form2_DataTransferred(object sender, string data)
{
MessageBox.Show(data);
}
}
public class Form1 : Form
{
public DataTable Data { get; set; }
public Form1()
{
InitializeComponent();
Data = new DataTable();
Data.Columns.Add("Column1");
Data.Rows.Add("Hello from Form1");
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Data = Data;
form2.Show();
}
}
public class Form2 : Form
{
public DataTable Data { get; set; }
public Form2()
{
InitializeComponent();
dataGridView1.DataSource = Data;
}
}
// 在app.config中添加配置
<configuration>
<appSettings>
<add key="Data" value="Hello from Form1"/>
</appSettings>
</configuration>
// 在Form1中读取数据
public class Form1 : Form
{
public string Data { get; set; }
public Form1()
{
InitializeComponent();
var config = ConfigurationManager.AppSettings["Data"];
Data = config;
}
}
// 在Form2中写入数据
public class Form2 : Form
{
public void SaveData()
{
var config = ConfigurationManager.AppSettings;
config["Data"] = "Hello from Form2";
ConfigurationManager.RefreshSection("appSettings");
}
}
这些方法可以根据你的需求和应用场景进行选择。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。