“String”类型的值无法转换为“System.Windows.Forms.Textbox”?

时间:2013-08-22 17:25:29

标签: vb.net

我的名为form2.vb的表单有此代码。

Private Sub ADDRESS_TICKETDataGridView_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles ADDRESS_TICKETDataGridView.CellDoubleClick
        Dim value As String = ADDRESS_TICKETDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
        If e.ColumnIndex = e.ColumnIndex Then
            Search.Show()
            Search.TextBox1 = value



        End If
    End Sub
End Class

但是错误告诉我'String'类型的值不能转换为'System.Windows.Forms.TextBox'。 我想解决这个问题基本上我想要的是从datagridview获取值并将其输入到另一个具有文本框的表单上。可能会这样做,还是我做错了什么。请帮帮忙?

2 个答案:

答案 0 :(得分:7)

Search.TextBox1 = value

您刚尝试分配TextBox1变量来保存字符串而不是文本框。

这没有任何意义。

相反,您希望通过设置其Text属性来设置文本框中显示的文本。

答案 1 :(得分:1)

仅仅是为了获取信息(并添加到我对Slacks答案的评论中),有一种方法可以使用运算符重载来解决此问题。 (代码在C#中,但我想它在VB.Net中很容易翻译)

只需创建一个继承自TextBox的类:

public class MyTextBox : TextBox
{
    public static implicit operator string(MyTextBox t)
    {
        return t.Text;
    }

    public static implicit operator MyTextBox(string s)
    {
        MyTextBox tb = new MyTextBox();
        tb.Text = s;
        return tb;
    }

    public static MyTextBox operator +(MyTextBox tb1, MyTextBox tb2)
    {
        tb1.Text += tb2.Text;
        return tb1;
    }
}

然后你就可以做到这样的事情:

MyTextBox tb = new MyTextBox();
tb.Text = "Hello ";
tb += "World";

您的文本框内容将为Hello World

我尝试使用tb = "test",但没有成功。