0

我的名为 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 获取值并将其输入到另一个具有文本框的表单上。可以完成还是我做错了什么。请帮忙?

4

2 回答 2

7
Search.TextBox1 = value

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

这没有任何意义。

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

于 2013-08-22T17:27:21.980 回答
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",但没有成功。

于 2013-08-22T18:10:31.557 回答