C#调用问题.

来源:百度知道 编辑:UC知道 时间:2024/06/14 01:51:18
下面这段代码是在窗体一的代码,我怎么可以在窗体二里调用它

public static void Sx(ListView lv,Label la)
{
OleDbDataReader Dr = Form1.MySql("select * from the");
lv.Items.Clear();
while (Dr.Read())
{
ListViewItem li = new ListViewItem();
li.Text = Dr[0].ToString();
li.SubItems.Add(Dr[1].ToString());
li.SubItems.Add(Dr[2].ToString());
li.SubItems.Add(Dr[3].ToString());
li.SubItems.Add(Dr[4].ToString());
li.SubItems.Add(Dr[5].ToString());
lv.Items.Add(li);
}
la.Text = "记录总数:" + lv.Items.Count.ToString();
}

我在窗体二这样写没有成功.
private void button2_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form1.Sx(f1.listView1,f1.labe

按照我的方法做不会有错的,有问题HI我
另外加强c#基础

private void button2_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form1.Sx(f1.listView1,f1.label2);
f1.Show();
}

在你要调用的文件开头
输入
using 要调用的名字;

窗体一里
public static void Sx(ListView lv,Label la)
改为
public void Sx(ListView lv,Label la)
去掉
static

建议看一些讲static的教程

假设窗体一里面有个函数
public static int Add(int x, int y)
{
return x + y;
}
那么在窗体二中,可以直接这样调用
private void button2_Click(object sender, EventArgs e)
{
int z = Form1.Add(1, 2);
}
不用 new 一个Form1 的 instance 出来

例如
public partial class ukadmins_Main : System.Web.UI.Page
{
public static void Sx(ListView lv,Label la)
{
OleDbDataReader Dr = Form1.MySql("select * from the");
lv.Items.Clear();
while (Dr