Dim as Textbox Problem

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4823 Views - Last Post: 09 May 2011 - 08:00 AM Rate Topic: -----

#1 Kilokahn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 25-April 11

Dim as Textbox Problem

Posted 06 May 2011 - 07:24 PM

Quick question, I want to Dim a Texbox based on the input I get. However I get an error. Is there an easy way to solve this:

	Sub PlayerKeyIn(PlayerNum As Integer, ButtonType As String)
		Dim PlayerName As TextBox = "Player" + PlayerNum.ToString + "Name" 'String is not a Textbox error, of course...
		MsgBox(PlayerName.text)


Thanks in advance!

I should mention that I already have text boxes named "Player1Name", etc etc, so if I throw a 1 in there, it should work.

Is This A Good Question/Topic? 0
  • +

Replies To: Dim as Textbox Problem

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1031
  • View blog
  • Posts: 4,014
  • Joined: 02-July 08

Re: Dim as Textbox Problem

Posted 06 May 2011 - 07:37 PM

Me.Controls("Player" & PlayerNum.ToString & "Name") is how you locate controls based on there name.

This post has been edited by hawkvalley1: 06 May 2011 - 08:49 PM

Was This Post Helpful? 1
  • +
  • -

#3 souptoy  Icon User is offline

  • D.I.C Head


Reputation: 54
  • View blog
  • Posts: 244
  • Joined: 17-January 08

Re: Dim as Textbox Problem

Posted 06 May 2011 - 08:13 PM

Dim txtBox as textbox = new textbox

Txtbox.ID = blah
Txtbox.text = "something"

This what you're looking for?

This post has been edited by souptoy: 06 May 2011 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 Kilokahn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 25-April 11

Re: Dim as Textbox Problem

Posted 07 May 2011 - 09:47 AM

View Posthawkvalley1, on 06 May 2011 - 07:37 PM, said:

Me.Controls("Player" & PlayerNum.ToString & "Name") is how you locate controls based on there name.


I tried your method and it doesn't seem like it wants to work. Let me spell it out a bit more, I have 4 text boxes:

player1name
player2name
player3name
player4name

I want to see when I call this function PlayerKeyIn(1, "a"), I can attribute which player name it came from so I can show their name on my form. I've also did some Googling and I can't seem to find a clear-cut way to reference a control and get its information. Ultimately the idea is to copy that control and do things to it and then copy it back in the end.

I appreciate the assistance, it's really frustrating when it's on the tip of the tongue. :)
Was This Post Helpful? 0
  • +
  • -

#5 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 102
  • View blog
  • Posts: 584
  • Joined: 01-October 08

Re: Dim as Textbox Problem

Posted 07 May 2011 - 10:23 AM

assumes text boxes named:

playerXname where X is an whole number.


            Dim aTextbox As TextBox = DirectCast(Me.Controls("player" & x.ToString & "name"), TextBox)
            Debug.WriteLine(aTextbox.Name)


Was This Post Helpful? 1
  • +
  • -

#6 Kilokahn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 25-April 11

Re: Dim as Textbox Problem

Posted 07 May 2011 - 10:27 AM

View Postdbasnett, on 07 May 2011 - 10:23 AM, said:

assumes text boxes named:

playerXname where X is an whole number.


            Dim aTextbox As TextBox = DirectCast(Me.Controls("player" & x.ToString & "name"), TextBox)
            Debug.WriteLine(aTextbox.Name)



No workie, but I think we're getting close...

	Sub PlayerKeyIn(PlayerNum As Integer, ButtonType As String)
		Dim PlayerName As TextBox = DirectCast(Me.Controls("player" & PlayerNum.ToString & "name"), TextBox)
		MsgBox(PlayerNum & " - " & ButtonType & " - " & PlayerName.Text)


The error I get now is on the MsgBox, the error is: Object reference not set to an instance of an object.
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1031
  • View blog
  • Posts: 4,014
  • Joined: 02-July 08

Re: Dim as Textbox Problem

Posted 07 May 2011 - 10:35 AM

For this to work the control must belong to the form (Me.Controls) otherwise if they are contained in another container then <containername>.Controls(...).
Was This Post Helpful? 1
  • +
  • -

#8 Kilokahn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 25-April 11

Re: Dim as Textbox Problem

Posted 07 May 2011 - 11:42 AM

View Posthawkvalley1, on 07 May 2011 - 10:35 AM, said:

For this to work the control must belong to the form (Me.Controls) otherwise if they are contained in another container then <containername>.Controls(...).


But in my last code I did do that, maybe I am confused:

	Sub PlayerKeyIn(PlayerNum As Integer, ButtonType As String)
		Dim PlayerName As TextBox = DirectCast([b]Me.Controls[/b]("player" & PlayerNum.ToString & "name"), TextBox)
		MsgBox(PlayerNum & " - " & ButtonType & " - " & PlayerName.Text)


Thanks!
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1031
  • View blog
  • Posts: 4,014
  • Joined: 02-July 08

Re: Dim as Textbox Problem

Posted 07 May 2011 - 11:58 AM

I know you did, I was just making sure. As long as you have a control by that name this method should work. You also have to be in the framework 2.0+.
Was This Post Helpful? 0
  • +
  • -

#10 Kilokahn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 25-April 11

Re: Dim as Textbox Problem

Posted 07 May 2011 - 12:15 PM

View Posthawkvalley1, on 07 May 2011 - 11:58 AM, said:

I know you did, I was just making sure. As long as you have a control by that name this method should work. You also have to be in the framework 2.0+.


The odd thing is I am using Framework 4.0 and this is still giving an error. I thought it was variable poisoning, so I even changed it to:

	Sub PlayerKeyIn(PlayerNum As Integer, ButtonType As String)
		Dim playerzNamez As TextBox = New TextBox
		playerzNamez = DirectCast(Me.Controls("player" & PlayerNum.ToString & "name"), TextBox)
		MsgBox(PlayerNum & " - " & ButtonType & " - " & playerzNamez.Text)


The error I get is: System.NullReferenceException: Object reference not set to an instance of an object.

Is there anything I need to add to the class or any notation I am missing?

Thanks!

P.S. I even tried to ditch everything else and straight do this directly:

MsgBox("player" & PlayerNum.ToString & "name" & " - " & DirectCast(Me.Controls("player" & PlayerNum.ToString & "name"), TextBox).Text)


Still the same error: System.NullReferenceException: Object reference not set to an instance of an object.
Was This Post Helpful? 0
  • +
  • -

#11 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 102
  • View blog
  • Posts: 584
  • Joined: 01-October 08

Re: Dim as Textbox Problem

Posted 07 May 2011 - 12:23 PM

    Dim prng As New Random

    Private Sub Button1_Click(sender As System.Object, _
                              e As System.EventArgs) Handles Button1.Click
        'when the button is pressed
        'display the text of one of the TextBoxes
        'chosen randomly
        PlayerKeyIn(prng.Next(1, 5), "ButtonType string")

    End Sub

    Private Sub PlayerKeyIn(PlayerNum As Integer, ButtonType As String)

        Dim playerzNamez As TextBox = DirectCast(Me.Controls("player" & PlayerNum.ToString & "name"), TextBox)
        MessageBox.Show(PlayerNum.ToString & " - " & ButtonType & " - " & playerzNamez.Text, "It does work!")

    End Sub


Was This Post Helpful? 1
  • +
  • -

#12 Kilokahn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 25-April 11

Re: Dim as Textbox Problem

Posted 07 May 2011 - 12:30 PM

View Postdbasnett, on 07 May 2011 - 12:23 PM, said:

    Dim prng As New Random

    Private Sub Button1_Click(sender As System.Object, _
                              e As System.EventArgs) Handles Button1.Click
        'when the button is pressed
        'display the text of one of the TextBoxes
        'chosen randomly
        PlayerKeyIn(prng.Next(1, 5), "ButtonType string")

    End Sub

    Private Sub PlayerKeyIn(PlayerNum As Integer, ButtonType As String)

        Dim playerzNamez As TextBox = DirectCast(Me.Controls("player" & PlayerNum.ToString & "name"), TextBox)
        MessageBox.Show(PlayerNum.ToString & " - " & ButtonType & " - " & playerzNamez.Text, "It does work!")

    End Sub



Still errors: System.NullReferenceException: Object reference not set to an instance of an object.

Sure there is no other references that I need like "System.Collections.CollectionBase"?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#13 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1031
  • View blog
  • Posts: 4,014
  • Joined: 02-July 08

Re: Dim as Textbox Problem

Posted 07 May 2011 - 01:08 PM

No other reference is needed. Double check your textbox names. This a case sensitive search.

This post has been edited by hawkvalley1: 07 May 2011 - 01:10 PM

Was This Post Helpful? 1
  • +
  • -

#14 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 102
  • View blog
  • Posts: 584
  • Joined: 01-October 08

Re: Dim as Textbox Problem

Posted 07 May 2011 - 02:49 PM

View PostKilokahn, on 07 May 2011 - 02:30 PM, said:

Still errors: System.NullReferenceException: Object reference not set to an instance of an object.

Sure there is no other references that I need like "System.Collections.CollectionBase"?

Thanks!


Which means it is some fact you have not told us. Run this and paste the results here:


        Dim s As String = ""

        For Each c As Control In Me.Controls
            s &= c.Name & Environment.NewLine
        Next
        Clipboard.SetData(DataFormats.Text, s) 'copy to clipboard


Was This Post Helpful? 1
  • +
  • -

#15 Kilokahn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 25-April 11

Re: Dim as Textbox Problem

Posted 07 May 2011 - 06:33 PM

View Postdbasnett, on 07 May 2011 - 02:49 PM, said:

View PostKilokahn, on 07 May 2011 - 02:30 PM, said:

Still errors: System.NullReferenceException: Object reference not set to an instance of an object.

Sure there is no other references that I need like "System.Collections.CollectionBase"?

Thanks!


Which means it is some fact you have not told us. Run this and paste the results here:


        Dim s As String = ""

        For Each c As Control In Me.Controls
            s &= c.Name & Environment.NewLine
        Next
        Clipboard.SetData(DataFormats.Text, s) 'copy to clipboard



Did that, it comes out as this:

viewInGameBox
playlistBox
manualBox
timer1static
timer1sec
timer1mil
lockBox
questionString
label13
questionURL
label12
pointBox
timerBox
playerViewURL
viewBox
questionBox
playersBox
menuStrip1
buzzedPlayer

I don't see "player1name", is it because it's inside of a box? Yes, I see playersBox. So I should be looking at me.playersBox.Controls?

Appreciate it!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2