else if (e.KeyChar == 68 || e.KeyChar == 100)//如果按下的是d,将跳转到form2            {
                Form2 fs = new Form2();
                fs.Show();
form2窗体代码    private void Form2_Load_1(object sender, EventArgs e)
        {
          
         MHouse h = ((Button)sender).Tag as MHouse;   //  运行完这句就报错  这句是获得button上的id 他代表一个户id
           textBox1.Text =h.Houseid.ToString();
           int id = Convert.ToInt32(textBox1.Text);未处理的“System.InvalidCastException”类型的异常出现在 VirtualHouse.exe 中。其他信息: 无法将类型为“VirtualHouse.Form2”的对象强制转换为类型“System.Windows.Forms.Button”。
谢谢各位大侠帮忙··谢谢 

解决方案 »

  1.   

     MHouse h = ((Button)sender).Tag as MHouse; 是什么意思呢?没看明白!呵呵
      

  2.   

    无法将类型为“VirtualHouse.Form2”的对象强制转换为类型“System.Windows.Forms.Button”。
    这不写清楚了么。把窗体转换button错误了。
      

  3.   

    MHouse h = ((Button)sender).Tag as MHouse;
    非常不理解。
      

  4.   

    MHouse h = ((Button)sender).Tag as MHouseMhouse 是房屋类·· 每栋楼的层有多少户   它的户室号是一个button  这里我也不知道什么意思不过h.Houseid.ToString(); 是代表这一户在数据库的户id    这代码不是我写的 我是在改别人的··· 希望各位大大帮帮忙···
      

  5.   

    你不会只是想获得 FORM1 按下的那个数字键吧?
      

  6.   

    MHouse h = ((Button)sender).Tag as MHouse; // 运行完这句就报错 这句是获得button上的id 他代表一个户id
    换成
    MHouse h = ((Form)sender).Tag as MHouse; // 运行完这句就报错 这句是获得button上的id 他代表一个户id
      

  7.   

    就是根据前一个窗体的id  查找出它的信息 显示在另外一个窗体  
    MHouse h = ((Button)sender).Tag as MHouse  这句代码是为了或得他的id  h.house_id 
      
      

  8.   

    想或得按下那个键他代表的id  然后在第二个窗体根据这个id 查询   
      

  9.   

    MHouse h = ((Button)sender).Tag as MHouse
    完全的看不懂,你解释清楚呀。
      

  10.   

    不好意思 被这错误搞混了··· 是大括号没弄好   改成你那样的话
    可以运行 但是到了下一句    会报错
    MHouse h = ((Form)sender).Tag as MHouse;
                 //  MHouse h = ((Button)sender).Tag as MHouse;
                   textBox1.Text =h.Houseid.ToString();               int id = Convert.ToInt32(textBox1.Text);
    未处理的“System.NullReferenceException”类型的异常出现在 VirtualHouse.exe 中。其他信息: 未将对象引用设置到对象的实例。  
      

  11.   

    MHouse h = ((Button)sender).Tag as MHouseMhouse 是房屋类·· 每栋楼的层有多少户 它的户室号是一个button 这里我也不知道什么意思不过h.Houseid.ToString(); 是代表这一户在数据库的户id 这代码不是我写的 我是在改别人的···
      

  12.   

    MHouse h = ((Button)sender).Tag as MHouse;
    sender 指向窗体对象 可以转换成一个业务对象?
    加个if(h!=null)看看 肯定为空
    if(h!=null)
    {
      textBox1.Text =h.Houseid.ToString(); 
      int id = Convert.ToInt32(textBox1.Text);
      //这两句肯定不执行
    }
      

  13.   

    恩,是的 ,运行完MHouse h = ((Button)sender).Tag as MHouse;
      这句就直接报错了  高手,请问下该怎么解决?
      

  14.   

    sender窗体,怎么能转换成Button呢
      

  15.   

    Form2 fs = new Form2("01");
    fs.Show();
    Form2.cs:
    string strNum="";
    public Form2(string str)
    {
       strNum=str;
    }
    然后Load里面根据strNum读取信息放到文本框中如果只有一个就不用读数据库直接赋值
    ....Text=strNum;
      

  16.   

    谢谢哈,不过我已经解决了· 先在那个窗体获得值  然后用  AppDomain.CurrentDomain.SetData("aa ", id);传的值,麻烦大家了哈··谢谢了  等下在试一试你的方法 谢谢  谢谢各位了