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

解决方案 »

  1.   

    不行,在getIP()中string url=f1.textBox1.Text;  不能取得textBox1的当前值,url为textBox1的初始值。为何???
    HELP ME!!!
      

  2.   

    而且如果指定url的值,在resolver1.Resolve(url);返回的数组中会有数据,但f1.listBox1.Items.Add(resolver1[i]));不能将resolver1中的数据添加到listBox1中,为什么???
      

  3.   

    欧很奇怪,textbox1声明为private,不知道你怎么访问到的.而且我认为按照你的代码,url的值不会改变的,因为我觉得你的代码是没有办法对textbox1的值进行改动的
    欧改了一下,应该可以了
    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])); }
    }
    }
      

  4.   

    你不一定非要把实例化TestApp的实例化代码和调用代码放到Form1_Load里面,Formload的时候取到的当然是默认值。
    把TestApp ta=new TestApp();
         ta.getIP();
    放到你的事件相应代码断里面。
      

  5.   

    大哥,我没有把调用代码放到Form1_Load里面。
    我是这样调用的:
    private void button2_Click(object sender, System.EventArgs e)
    {
    TestApp IP=new TestApp();
    listBox1.Items.Clear();
    IP.getIP();
    }to: lyc10()兄,你的方法还是不行。
      

  6.   

    方法一:
    把私有控件改开公开控
    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]);                   }
    }
    }
      

  7.   

    private System.Windows.Forms.ListBox listBox1;
    改成public System.Windows.Forms.ListBox listBox1;
      

  8.   

    getIP()访问的textbox1和listbox1都是私有的,不可能访问的到。
    通常采用donger2000(东东) 的第二种方法。
      

  9.   

    没看清你的用法!你这样做,挺别扭的,还不如把TestApp做成方法方案一:改用方法,代码要简洁很多:
    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]);                   }
    }
    }
      

  10.   

    The form f1 which you are operating against in the TestApp::getIP() is not the form you are operating from. Actually you are working with a whole new instance of the instance of Form1 class, so you cannot get values from the existing Form1 instance, which is your main form (created by Application in Main()).The proper way to implement your desired functionality is to pass a URL parameter to the getIP() method, along with a delegation (callback) for adding item back to Form1's list box. Say:// data to be sent back to event handler
    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. :)
      

  11.   

    东东,见到你真好,谢谢你的解答,但我还是不明白,为什么在getIP()中实例化Form1不能得到想要的答案。 能否解释一下:
    private Form1 f1;
    public TestApp(Form1 form)
    {
    f1=form;
    }
    的作用。
      

  12.   

    你用new实例化出来的Form1是新建出来的,和你已经存在的Form1实例是不一样的。
    private Form1 f1;
    public TestApp(Form1 form)
    {
    f1=form;
    }
    这里的作用是把你已经存在的Form1实例作为参数传给 TestApp类,这样TestApp中使用的f1和你已经存在的Form1实例就一样了,避免上面所说的不一致问题。