שתף באמצעות


How to Assign a string to a textbox?

Question

Sunday, October 28, 2012 3:40 PM

Hello

I am trying to assign a string value to a textbox in Visual Basic.

In the design window, I have a textbox called MyName.

In code, I want to try to assign the value of a string to MyName:

Private Sub SomeRoutine()

     MyName.Text = "Mike Lee"

End Sub

When I run I get a message saying that I cannot convert a string to text.  How do I fix this?  In previous versions of VB, you could use a simple assignment statement.

All replies (4)

Sunday, October 28, 2012 3:48 PM ✅Answered

Put your cursor on MyName, then hit F12 (Go to definition).  It should show you in the editor the declaration statement of the variable MyName.  It sounds like MyName is not the textbox you think it is, as your code is exactly how you should do this.  That is, unless you are doing an ASP.Net app...

--
Mike


Sunday, October 28, 2012 3:47 PM

Mike,

We're missing something - that should work fine:

        Dim MyName As New TextBox        MyName.Text = "Mike Lee"

Is there more to your code that we're not seeing?

Please call me Frank :)


Sunday, October 28, 2012 6:06 PM

That is, unless you are doing an ASP.Net app...

--
Mike

@Mike, 

If it is an ASP.Net textbox then it should be accepted too. 

Moreover, a text property is by conventional always a property of the type string.

Success
Cor


Sunday, October 28, 2012 6:40 PM

You are correct Cor regarding an ASP.Net Textbox.  I was thinking of HtmlInputText which I prefer, rather than the TextBox control.  With HtmlInputText you use the Value property.

--
Mike