public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
...
}private void Form1_Load(object sender, System.EventArgs e)
{
TestApp ta=new TestApp();
ta.getIP();
}
public class ResolveDNS
{
...
}
public class TestApp
{
public void getIP()
{
ResolveDNS resolver1=new ResolveDNS();
Form1 f1=new Form1();
string url=f1.textBox1.Text;
resolver1.Resolve(url);
int n=resolver1.IPLength;
for(int i=0;i<n;i++)
{
f1.listBox1.Items.Add(resolver1[i])); }
}
}
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
...
}private void Form1_Load(object sender, System.EventArgs e)
{
TestApp ta=new TestApp();
ta.getIP();
}
public class ResolveDNS
{
...
}
public class TestApp
{
public void getIP()
{
ResolveDNS resolver1=new ResolveDNS();
Form1 f1=new Form1();
string url=f1.textBox1.Text;
resolver1.Resolve(url);
int n=resolver1.IPLength;
for(int i=0;i<n;i++)
{
f1.listBox1.Items.Add(resolver1[i])); }
}
}
HELP ME!!!
欧改了一下,应该可以了
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
... public string test()
{
return this.textBox1.text;
}}public class TestApp
{
public void getIP()
{
ResolveDNS resolver1=new ResolveDNS();
Form1 f1=new Form1();
f1.showdialog();
string url=f1.test();
resolver1.Resolve(url);
int n=resolver1.IPLength;
for(int i=0;i<n;i++)
{
f1.listBox1.Items.Add(resolver1[i])); }
}
}
把TestApp ta=new TestApp();
ta.getIP();
放到你的事件相应代码断里面。
我是这样调用的:
private void button2_Click(object sender, System.EventArgs e)
{
TestApp IP=new TestApp();
listBox1.Items.Clear();
IP.getIP();
}to: lyc10()兄,你的方法还是不行。
把私有控件改开公开控
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public System.Windows.Forms.TextBox textBox1;
public System.Windows.Forms.ListBox listBox1;
...
}
public class ResolveDNS
{
...
}
public class TestApp
{
public void getIP()
{
ResolveDNS resolver1=new ResolveDNS();
Form1 f1=new Form1();
string url=f1.textBox1.Text;
resolver1.Resolve(url);
int n=resolver1.IPLength;
for(int i=0;i<n;i++)
{
f1.listBox1.Items.Add(resolver1[i])); }
}
}方法二:
加两个公开的方法
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
public string gettext()
{
if(this.textBox1.Text!="")
return this.textBox1.Text;
esle
return "";
} public void AddItems(object newitems)
{
this.listBox1.Items.Add(newitems);
} ...
}
public class ResolveDNS
{
...
}
public class TestApp
{
public void getIP()
{
ResolveDNS resolver1=new ResolveDNS();
Form1 f1=new Form1();
string url=f1.gettext();
resolver1.Resolve(url);
int n=resolver1.IPLength;
for(int i=0;i<n;i++)
{
f1.AddItems(resolver1[i]); }
}
}
改成public System.Windows.Forms.ListBox listBox1;
通常采用donger2000(东东) 的第二种方法。
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1; private void button2_Click(object sender, System.EventArgs e)
{
getIP();
} private void getIP()
{
this.listBox1.Items.Clear();
ResolveDNS resolver1=new ResolveDNS();
string url=this.textBox1.Text;
resolver1.Resolve(url);
int n=resolver1.IPLength;
for(int i=0;i<n;i++)
{
this.listBox1.Items.Add(resolver1[i]); }
} ...
}public class ResolveDNS
{
...
}
方案二:
如果非要用那个类,应该:public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
public string gettext()
{
if(this.textBox1.Text!="")
return this.textBox1.Text;
esle
return "";
} public void AddItems(object newitems)
{
this.listBox1.Items.Add(newitems);
} private void button2_Click(object sender, System.EventArgs e)
{
TestApp IP=new TestApp(this);
listBox1.Items.Clear();
IP.getIP();
} ...
}public class ResolveDNS
{
...
}
public class TestApp
{
private Form1 f1;
public TestApp(Form1 form)
{
f1=form;
}
public void getIP()
{
ResolveDNS resolver1=new ResolveDNS();
string url=f1.gettext();
resolver1.Resolve(url);
int n=resolver1.IPLength;
for(int i=0;i<n;i++)
{
f1.AddItems(resolver1[i]); }
}
}
public class SomeEventArgs: EventArgs
{
public SomeEventArgs(string IP)
{
this.IP = IP;
} public string IP;
}// delegation (callback) function definition
public delegate void NewItemEventHandler(object sender, SomeEventArgs e);class TestApp
{
public void GetIP(string URL, NewItemEventHandler onNewItem)
{
...
resolver1.Resolve(URL);
...
for(int i=0; i<...;i++)
{
if (onNewItem!=null)
onNewItem(new SomeEventArgs(resolver[i]));
}
}
}class Form1
{
...
// actual callback function
private void DoOnNewItem(object sender, SomeEventArgs e)
{
this.Listbox1.Items.Add(e.IP);
} private void Form1_Load()
{
TestApp ta = new TestApp();
ta.GetIP(textbox1.text, new NewItemEventHandler(DoOnNewItem));
...
}
}Carefully think about your program's structure before coding it. :)
private Form1 f1;
public TestApp(Form1 form)
{
f1=form;
}
的作用。
private Form1 f1;
public TestApp(Form1 form)
{
f1=form;
}
这里的作用是把你已经存在的Form1实例作为参数传给 TestApp类,这样TestApp中使用的f1和你已经存在的Form1实例就一样了,避免上面所说的不一致问题。