0

好吧,我知道这个问题已被问过几次,但没有一个解决方案对我有用。我只是想将一个表单中的值传递给另一种表单的文本框。
在第一个表单上,我有一个数据网格,当双击它时,它会从 datagrid 列中获取一个值。

public partial class AvailableRooms : Form
{   
   private void DCRoom(object sender, DataGridViewCellMouseEventArgs e) 
  {
   var roomnum = dgRooms.Rows[e.RowIndex].Cells["iRoomNum"].Value.ToString();
   RoomBooking rb = new RoomBooking();//The second form
   rb.roomnumber = roomnum;           
   rb.Show();        
 }
}

在第二个表单上,我设置了文本框的属性

public partial class RoomBooking : Form
{

   public RoomBooking()
    {
      StartPosition = FormStartPosition.CenterScreen;
      InitializeComponent();
  }

  public string roomnumber
   {
      get { return txtRoomNum.Text; }
      set {txtRoomNum.Text = value;}
    }

 }

在此先感谢您的帮助?

4

2 回答 2

0

您必须找到控件来编辑它,因为它不属于该类。

private void DCRoom(object sender, DataGridViewCellMouseEventArgs e)
{
    //new value
    var roomnum = dgRooms.Rows[e.RowIndex].Cells["iRoomNum"].Value.ToString();
    //The second form
    RoomBooking rb = new RoomBooking(this);
    //The textbox
    TextBox roomnumber = (TextBox)rb.Controls.Find("roomnumber", true)[0];
    //set the value of the textbox
    roomnumber.Text = roomnum;
    //show second form
    rb.Show();
}
于 2013-07-17T07:28:24.107 回答
0

我会这样定义RoomBooking类:

public partial class RoomBooking : Form
{
    public RoomBooking() // WinForms Designer requires a public parameterless constructor
    {
        InitializeComponent();
        StartPosition = FormStartPosition.CenterScreen;
    }

    public RoomBooking(string roomNumber) : this() // Constructor chaining
    {
        RoomNumber = roomNumber;
        txtRoomNum.Text = RoomNumber;
    }

    public string RoomNumber { get; set; }
}

然后:

public partial class AvailableRooms : Form
{      
    private void DCRoom(object sender, DataGridViewCellMouseEventArgs e)
    {
        var roomNumber = dgRooms.Rows[e.RowIndex].Cells["iRoomNum"].Value.ToString();
        var roomBooking = new RoomBooking(roomNumber);
        roomBooking.Show();
    }
}

希望这可以帮助。

于 2013-07-17T08:08:53.010 回答