NOE2270667
NOE2270667

Reputation: 55

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

My form named form2.vb has this code.

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?

Upvotes: 0

Views: 26901

Answers (2)

Shimrod
Shimrod

Reputation: 3205

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)

Just create a class inheriting from TextBox like this:

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

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

Upvotes: 1

SLaks
SLaks

Reputation: 888303

Search.TextBox1 = value

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

That doesn't make any sense.

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

Upvotes: 7

Related Questions