c#windows應用程式表單間傳值

來源:互聯網
上載者:User

Form1 f1 =new Form1();
f1.parameter["XXX"]="XXX";(字串)
f1.show();

當在f1中使用這個值時這樣接值
string s = type(me.parameter["XXX"],"string")
VB.NET的文法
我寫的這幾句可能有錯誤
但大意就是這樣了
我後來看了一下這個DLL原來這個parameter是個hashtable

像他這種表單間的傳值的方法可以傳個種類型的值

我的問題就是:這個父表單應該怎麼寫,讓繼承他的表單都能實現我說的這種方法來傳值??

 

其實parameter只是Form1提供的一個屬性,你自己也可以實現。例如:
public class clsParameters
{
    ArrayList arrParameters;
    public clsParameters()
    {
        arrParameters = new ArrayList();
     }
    public object this[int Index]
    {
        get{
             if( i >=0 && i < arrParameters.Count )
                return arrParameters[i];
             else
                throw new ApplicationException( "Invalid index" );
            }
         set{
             if( i >=0 && i < arrParameters.Count )
                arrParameters[i] = value;
             else
                throw new ApplicationException( "Invalid index" );
            }
    }
}

在你的Form1中,加入如下即可:
public clsParameters parameters = new clsParameters();

就可以用this.parameters[i]來訪問了。

 


f1.parameter["XXX"]="XXX";

 

我自己想到了一種方法
FORM1向FORM2中傳值
在FORM2中聲明一個Hashtable
在FORM2的構造方法中執行個體化
在FORM1中寫入值

FORM2中
public Hashtable parameter = null;
public void form2()
{
    parameter = new hashtable();
}
form1 中

form2 f2 = new form2();
f2.parameter.add("XXX", "XXX");
f2.show();

在FORM2中接值這樣寫
string s = this.parameter["XXX"].toString();
messagebox.show(s);

第二種方法:
第1步:在解決方案上添加一個表單Form2;並添加textBox1、textBox2、Button1,將Button1的標題設為“確定”,DialogResult屬性設為“OK”;
第2步:在表單Form2的類代碼中添加兩個私人欄位: _username、_password,並添加兩個public屬性:UserName、Password;代碼如下:
public partial class Form2: Form
{
       private string _username;
       private string _password;
       public string UserName
       {
           get
           {
               return _username;
           }
           set
           {
               _username=value;
           }
       }
       public string Password
       {
           get
           {
               return _password;
           }
           set
           {
               _password=value;
           }
       }
       .....
}

第3步:表單間相互傳值示範
在Form1添加一個的Botton1、一個comboBox1,在Botton1_Click函數中如下代碼:
private void button1_Click(object sender,EventArgs e)
{
      Form2 myForm2 = new Form2();
      myForm2.UserName="Richard";//Form1向Form2傳值!!!
      myForm2.Password="pwd1234";
      DialogResult result= myForm2.ShowDialog();
      if(result==DialogResult.OK)
      {
           comboBox1.Items.Add(myForm2.UserName);////Form2向Form1傳值!!!
           comboBox2.Items.Add(myForm2.Password);
       }
}
  要充分示範上述功能,還需要對Form2的代碼作如下完善:
第1,在Form2_Load中添加如下代碼:
private Form2_Load(object sender,EventArgs e)
{
    textBox1.Text=_username;
    textBox2.Text=_password;
}
第2,在textBox1、textBox2的textChanged事件中添加如下代碼:
private void textBox1_TextChanged(object sender,EventArgs e)
{
    this.UserName=textBox1.Text;
}
private void textBox2_TextChanged(object sender,EventArgs e)
{
    this.Password=textBox2.Text;
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.