vb.net “字符串”类型的值无法转换为“System.Windows.Forms.Textbox”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18387004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:06:10  来源:igfitidea点击:

Value of type 'String' cannot be converted to 'System.Windows.Forms.Textbox'?

vb.net

提问by NOE2270667

My form named form2.vb has this code.

我的名为 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

But on the error gives me that Value of type 'String' cannot be converted to 'System.Windows.Forms.TextBox'. I want to fix this issue essentially what I want is to get the value from a datagridview and input it on another form that has a textbox. Could it be done or am I doing something wrong. Please help?

但是在错误上,我无法将“String”类型的值转换为“System.Windows.Forms.TextBox”。我想从根本上解决这个问题,我想要的是从 datagridview 获取值并将其输入到另一个具有文本框的表单上。可以做到还是我做错了什么。请帮忙?

回答by SLaks

Search.TextBox1 = value

You just tried to assign the TextBox1variable to hold a string instead of a textbox.

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

That doesn't make any sense.

那没有任何意义。

Instead, you want to set the text being displayed in the textbox, by setting its Textproperty.

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

回答by Shimrod

Just for information (and to add to my comment on Slacks's answer), there is a way to approach this behaviour, using operators overloading. (Code is in C#, but I guess it's easily translatable in VB.Net)

仅供参考(并添加到我对 Slacks 答案的评论中),有一种方法可以使用运算符重载来处理这种行为。(代码在 C# 中,但我想它在 VB.Net 中很容易翻译)

Just create a class inheriting from TextBoxlike this:

只需创建一个继承自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;
    }
}

And then you'll be able to do things like this:

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

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

The content of your textbox will then be Hello World

然后你的文本框的内容将是 Hello World

I tried making it work with tb = "test", but haven't succeeded.

我尝试让它与 一起工作tb = "test",但没有成功。