use variable as textbox name?

C

Co

Marco,

You can set the name of a textbox to another name, however that does not
change the reference to that texbox object.

In other words, you don't make it dynamic like that.
When you want to use it a little bit more dynamic then look at that samples
I have given you some time ago but it did not fit you then (it was for
another reason)

Dim TextBoxes as Textbox() = {Textbox1,Textbo2 tot 6 }

Now you can use it dynamically by just using the TextBoxes with its number
from 0 to 5

Cor

I worked it out.

Dim sDateBox As TextBox

Private Sub tbVerlooptTot_DoubleClick(ByVal sender As Object, ByVal e
As System.EventArgs) Handles tbVerlooptTot.DoubleClick
If bCalOpened = False Then
sDateBox = tbVerlooptTot
CreateDTP()
End If

Private Sub CreateDTP()

bCalOpened = True
m_picker = New MonthCalendar
m_picker.BringToFront()
m_picker.MaxSelectionCount = 1
m_picker.Size = tbVerlooptTot.Size
m_picker.BackColor = Color.Beige
tbVerlooptTot.Parent = Me
Me.Controls.Add(m_picker)
m_picker.BringToFront()
m_picker.Select()

End Sub

Private Sub m_picker_DateChanged(ByVal sender As Object, _
ByVal e As DateRangeEventArgs) Handles m_picker.DateChanged

Dim hti As MonthCalendar.HitTestInfo = m_picker.HitTest
(m_picker.PointToClient(MousePosition))
Dim t As TextBox = DirectCast(sDateBox, TextBox)

'Display the Start and End property values of
'the SelectionRange object in the text boxes.
t.Text = m_picker.SelectionRange.Start.Date.ToShortDateString
()
t.Text = m_picker.SelectionRange.End.Date.ToShortDateString()

'if we clicked on a date then hide the Calendar
If hti.HitArea = MonthCalendar.HitArea.Date Then
m_picker.Hide()
Me.Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
End If
End Sub

Thanks for helping me in the right direction.

Marco
 
M

Mike

Maybe that isn't useful for you, but I thought I mention it. :)

I was referring to the Tag property that is a persistent user-defined
element.

Dim tb As TextBox
tb = New TextBox : tb.Tag = "Tot" : m_Controls.Add(tb.Tag, tb)
tb = New TextBox : tb.Tag = "Van" : m_Controls.Add(tb.Tag, tb)
tb = New TextBox : tb.Tag = "Gemaakt" : m_Controls.Add(tb.Tag, tb)

This is very useful for when the user-defined element is more complex,
like a key or reference or pointer to some data associations to link
to some other controls.

--
 
M

Mike

Maybe that isn't useful for you, but I thought I mention it. :)

I was referring to the Tag property that is a persistent user-defined
element.

Dim tb As TextBox
tb = New TextBox : tb.Tag = "Tot" : m_Controls.Add(tb.Tag, tb)
tb = New TextBox : tb.Tag = "Van" : m_Controls.Add(tb.Tag, tb)
tb = New TextBox : tb.Tag = "Gemaakt" : m_Controls.Add(tb.Tag, tb)

This is very useful for when the user-defined element is more complex,
like a key or reference or pointer to some data associations to link
to some other controls.

--
 
A

Armin Zingler

Co said:
I can only create a New textbox.
That will bring up a new textbox on the form instead of using the
existing one.

You did not really read my post, did you? I asked why you don't declare
"WhichDateBox As Textbox". Now you removed it completely.


Armin
 
A

Armin Zingler

Co said:
I can only create a New textbox.
That will bring up a new textbox on the form instead of using the
existing one.

You did not really read my post, did you? I asked why you don't declare
"WhichDateBox As Textbox". Now you removed it completely.


Armin
 
J

Jack Jackson

I've been trying this:

Dim textBoxNew As New TextBox
textBoxNew.Name = "tbVerlooptTot"
m_Controls.Add(textBoxNew.Name, textBoxNew)
Dim textBoxNew2 As New TextBox
textBoxNew2.Name = "tbVerlooptVan"
m_Controls.Add(textBoxNew2.Name, textBoxNew2)
Dim textBoxNew3 As New TextBox
textBoxNew3.Name = "tbGemaaktTot"
m_Controls.Add(textBoxNew3.Name, textBoxNew3)

Private Sub CreateDTP()

Dim t As TextBox = DirectCast(m_Controls.Item(iDateBox),
TextBox)
bCalOpened = True
m_picker = New MonthCalendar
m_picker.BringToFront()
m_picker.MaxSelectionCount = 1
m_picker.Size = t.Size
m_picker.BackColor = Color.Beige
Me.Parent.Controls(0).Controls.Clear()
Me.Parent.Controls(0).Controls.Add(m_picker)
m_picker.BringToFront()
m_picker.Select()

End Sub

Private Sub m_picker_DateChanged(ByVal sender As Object, _
ByVal e As DateRangeEventArgs) Handles m_picker.DateChanged

Dim hti As MonthCalendar.HitTestInfo = m_picker.HitTest
(m_picker.PointToClient(MousePosition))
Dim t As TextBox = DirectCast(m_Controls.Item(iDateBox),
TextBox)


'Display the Start and End property values of
'the SelectionRange object in the text boxes.
t.Text = m_picker.SelectionRange.Start.Date.ToShortDateString
()
t.Text = m_picker.SelectionRange.End.Date.ToShortDateString()

'if we clicked on a date then hide the Calendar
If hti.HitArea = MonthCalendar.HitArea.Date Then
m_picker.Hide()
Me.Parent.Controls(0).Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
End If
End Sub

But I keep getting an error saying I should use the New instance.
"Object reference not set to an instance of an object"

Marco

First, when you say you get an error, TELL US WHICH LINE GETS THE
ERROR. Most people will read this post and ignore it, because it is
too much work to look through your lengthy code to try to figure out
which line might get that error.

My guess is that the error occurs on the second of these lines:

Dim textBoxNew As New TextBox
textBoxNew.Name = "tbVerlooptTot"

"Dim textBoxNew As New TextBox" does not create at TextBox object. It
creates a variable named textBoxNew that contains Nothing. You need
to create a TextBox object.

You need to do this:
Dim textBoxNew As TextBox = New TextBox
textBoxNew.Name = "tbVerlooptTot"
 
J

Jack Jackson

I've been trying this:

Dim textBoxNew As New TextBox
textBoxNew.Name = "tbVerlooptTot"
m_Controls.Add(textBoxNew.Name, textBoxNew)
Dim textBoxNew2 As New TextBox
textBoxNew2.Name = "tbVerlooptVan"
m_Controls.Add(textBoxNew2.Name, textBoxNew2)
Dim textBoxNew3 As New TextBox
textBoxNew3.Name = "tbGemaaktTot"
m_Controls.Add(textBoxNew3.Name, textBoxNew3)

Private Sub CreateDTP()

Dim t As TextBox = DirectCast(m_Controls.Item(iDateBox),
TextBox)
bCalOpened = True
m_picker = New MonthCalendar
m_picker.BringToFront()
m_picker.MaxSelectionCount = 1
m_picker.Size = t.Size
m_picker.BackColor = Color.Beige
Me.Parent.Controls(0).Controls.Clear()
Me.Parent.Controls(0).Controls.Add(m_picker)
m_picker.BringToFront()
m_picker.Select()

End Sub

Private Sub m_picker_DateChanged(ByVal sender As Object, _
ByVal e As DateRangeEventArgs) Handles m_picker.DateChanged

Dim hti As MonthCalendar.HitTestInfo = m_picker.HitTest
(m_picker.PointToClient(MousePosition))
Dim t As TextBox = DirectCast(m_Controls.Item(iDateBox),
TextBox)


'Display the Start and End property values of
'the SelectionRange object in the text boxes.
t.Text = m_picker.SelectionRange.Start.Date.ToShortDateString
()
t.Text = m_picker.SelectionRange.End.Date.ToShortDateString()

'if we clicked on a date then hide the Calendar
If hti.HitArea = MonthCalendar.HitArea.Date Then
m_picker.Hide()
Me.Parent.Controls(0).Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
End If
End Sub

But I keep getting an error saying I should use the New instance.
"Object reference not set to an instance of an object"

Marco

First, when you say you get an error, TELL US WHICH LINE GETS THE
ERROR. Most people will read this post and ignore it, because it is
too much work to look through your lengthy code to try to figure out
which line might get that error.

My guess is that the error occurs on the second of these lines:

Dim textBoxNew As New TextBox
textBoxNew.Name = "tbVerlooptTot"

"Dim textBoxNew As New TextBox" does not create at TextBox object. It
creates a variable named textBoxNew that contains Nothing. You need
to create a TextBox object.

You need to do this:
Dim textBoxNew As TextBox = New TextBox
textBoxNew.Name = "tbVerlooptTot"
 
J

Jack Jackson

I worked it out.

Dim sDateBox As TextBox

Private Sub tbVerlooptTot_DoubleClick(ByVal sender As Object, ByVal e
As System.EventArgs) Handles tbVerlooptTot.DoubleClick
If bCalOpened = False Then
sDateBox = tbVerlooptTot
CreateDTP()
End If

Private Sub CreateDTP()

bCalOpened = True
m_picker = New MonthCalendar
m_picker.BringToFront()
m_picker.MaxSelectionCount = 1
m_picker.Size = tbVerlooptTot.Size
m_picker.BackColor = Color.Beige
tbVerlooptTot.Parent = Me
Me.Controls.Add(m_picker)
m_picker.BringToFront()
m_picker.Select()

End Sub

Private Sub m_picker_DateChanged(ByVal sender As Object, _
ByVal e As DateRangeEventArgs) Handles m_picker.DateChanged

Dim hti As MonthCalendar.HitTestInfo = m_picker.HitTest
(m_picker.PointToClient(MousePosition))
Dim t As TextBox = DirectCast(sDateBox, TextBox)

'Display the Start and End property values of
'the SelectionRange object in the text boxes.
t.Text = m_picker.SelectionRange.Start.Date.ToShortDateString
()
t.Text = m_picker.SelectionRange.End.Date.ToShortDateString()

'if we clicked on a date then hide the Calendar
If hti.HitArea = MonthCalendar.HitArea.Date Then
m_picker.Hide()
Me.Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
End If
End Sub

Thanks for helping me in the right direction.

Marco

It looks like you have six DoubleClick event routines. You should
have only one that handles the DoubleClick events from all of the
textboxes. It is not necessary to have bCalOpened, use sDateBox
instead. CreateDTP is hard coded to use one textbox. Why does
CreateDTP set the Size of the MonthCalendar to match the textbox Size?
I would think you would want to leave the size alone and set the
Location to something like New Point(sDateBox.Left, sDateBox.Bottom +
5).

Private sDateBox As TextBox = Nothing

Private Sub tbVerlooptTot_DoubleClick(ByVal sender As Object, ByVal e
As System.EventArgs) Handles tbVerlooptTot.DoubleClick, ...
If sDateBox Is Nothing Then
sDateBox = DirectCast(sender, TextBox)
m_picker = New MonthCalendar
m_picker.BringToFront()
m_picker.MaxSelectionCount = 1
m_picker.Size = sDateBox.Size
m_picker.BackColor = Color.Beige
Me.Controls.Add(m_picker)
m_picker.BringToFront()
m_picker.Select()
End If
End Sub
 
J

Jack Jackson

I worked it out.

Dim sDateBox As TextBox

Private Sub tbVerlooptTot_DoubleClick(ByVal sender As Object, ByVal e
As System.EventArgs) Handles tbVerlooptTot.DoubleClick
If bCalOpened = False Then
sDateBox = tbVerlooptTot
CreateDTP()
End If

Private Sub CreateDTP()

bCalOpened = True
m_picker = New MonthCalendar
m_picker.BringToFront()
m_picker.MaxSelectionCount = 1
m_picker.Size = tbVerlooptTot.Size
m_picker.BackColor = Color.Beige
tbVerlooptTot.Parent = Me
Me.Controls.Add(m_picker)
m_picker.BringToFront()
m_picker.Select()

End Sub

Private Sub m_picker_DateChanged(ByVal sender As Object, _
ByVal e As DateRangeEventArgs) Handles m_picker.DateChanged

Dim hti As MonthCalendar.HitTestInfo = m_picker.HitTest
(m_picker.PointToClient(MousePosition))
Dim t As TextBox = DirectCast(sDateBox, TextBox)

'Display the Start and End property values of
'the SelectionRange object in the text boxes.
t.Text = m_picker.SelectionRange.Start.Date.ToShortDateString
()
t.Text = m_picker.SelectionRange.End.Date.ToShortDateString()

'if we clicked on a date then hide the Calendar
If hti.HitArea = MonthCalendar.HitArea.Date Then
m_picker.Hide()
Me.Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
End If
End Sub

Thanks for helping me in the right direction.

Marco

It looks like you have six DoubleClick event routines. You should
have only one that handles the DoubleClick events from all of the
textboxes. It is not necessary to have bCalOpened, use sDateBox
instead. CreateDTP is hard coded to use one textbox. Why does
CreateDTP set the Size of the MonthCalendar to match the textbox Size?
I would think you would want to leave the size alone and set the
Location to something like New Point(sDateBox.Left, sDateBox.Bottom +
5).

Private sDateBox As TextBox = Nothing

Private Sub tbVerlooptTot_DoubleClick(ByVal sender As Object, ByVal e
As System.EventArgs) Handles tbVerlooptTot.DoubleClick, ...
If sDateBox Is Nothing Then
sDateBox = DirectCast(sender, TextBox)
m_picker = New MonthCalendar
m_picker.BringToFront()
m_picker.MaxSelectionCount = 1
m_picker.Size = sDateBox.Size
m_picker.BackColor = Color.Beige
Me.Controls.Add(m_picker)
m_picker.BringToFront()
m_picker.Select()
End If
End Sub
 
C

Co

You did not really read my post, did you? I asked why you don't declare
"WhichDateBox As Textbox". Now you removed it completely.

Armin

I didn't remove it just renamed it:
Dim sDateBox As TextBox

Marco
 
C

Co

You did not really read my post, did you? I asked why you don't declare
"WhichDateBox As Textbox". Now you removed it completely.

Armin

I didn't remove it just renamed it:
Dim sDateBox As TextBox

Marco
 
A

Armin Zingler

Co said:
I didn't remove it just renamed it:
Dim sDateBox As TextBox


Yes, but in your first post you were referring to 'sWhichDateBox'. It is not
a local variable. Therefore I had to assume it is declared at class level. I
don't see it in your latest code. In Sub CreateDTP, you use 'sDateBox' now.
I can not see where it is declared - probably a field in the class - and
which value you assign.

Anyway, if sDateBox is the Textbox in question, I don't know why you create
a new Textbox instead of just using sDateBox.


Armin
 
A

Armin Zingler

Co said:
I didn't remove it just renamed it:
Dim sDateBox As TextBox


Yes, but in your first post you were referring to 'sWhichDateBox'. It is not
a local variable. Therefore I had to assume it is declared at class level. I
don't see it in your latest code. In Sub CreateDTP, you use 'sDateBox' now.
I can not see where it is declared - probably a field in the class - and
which value you assign.

Anyway, if sDateBox is the Textbox in question, I don't know why you create
a new Textbox instead of just using sDateBox.


Armin
 
C

Co

Yes, but in your first post you were referring to 'sWhichDateBox'. It is not
a local variable. Therefore I had to assume it is declared at class level. I
don't see it in your latest code. In Sub CreateDTP, you use 'sDateBox' now.
I can not see where it is declared - probably a field in the class - and
which value you assign.

Anyway, if sDateBox is the Textbox in question, I don't know why you create
a new Textbox instead of just using sDateBox.

Armin

That's what I couldn't get done.
How would that look Armin?

Marco
 
C

Co

Yes, but in your first post you were referring to 'sWhichDateBox'. It is not
a local variable. Therefore I had to assume it is declared at class level. I
don't see it in your latest code. In Sub CreateDTP, you use 'sDateBox' now.
I can not see where it is declared - probably a field in the class - and
which value you assign.

Anyway, if sDateBox is the Textbox in question, I don't know why you create
a new Textbox instead of just using sDateBox.

Armin

That's what I couldn't get done.
How would that look Armin?

Marco
 
A

Armin Zingler

Co said:
That's what I couldn't get done.
How would that look Armin?

I don't know _what_ you couldn't get done. Could you please be a bit more
precise? :)

Ok, back the start:
What I understood is that you have several textboxes. For which reason ever,
you want to assign the value from a DateTimePicker to one of the Textboxes.
First you want to memorize a certain Textbox in a variable. Later, in the
DTP's DateChanged event, you want to assign the value to the Textbox that
you've memorized in the variable. Is this correct? You first used a String
to store the name of the Textbox in question. I suggested to use a variable
of type Textbox instead. This enables you to directly use the variable
instead of finding the Textbox by a String. Therefore I currently don't know
what's the problem. By saying "just using sDateBox" I meant, well, just use
it like any other variable, for example in

sDateBox.Text = m_picker.SelectionRange.Start.Date.ToShortDateString

I do not know when you intend to set variable 'sDateBox'. Maybe in the
Textbox' Enter or GotFocus event? I don't know. Is this the problem?



Armin
 
A

Armin Zingler

Co said:
That's what I couldn't get done.
How would that look Armin?

I don't know _what_ you couldn't get done. Could you please be a bit more
precise? :)

Ok, back the start:
What I understood is that you have several textboxes. For which reason ever,
you want to assign the value from a DateTimePicker to one of the Textboxes.
First you want to memorize a certain Textbox in a variable. Later, in the
DTP's DateChanged event, you want to assign the value to the Textbox that
you've memorized in the variable. Is this correct? You first used a String
to store the name of the Textbox in question. I suggested to use a variable
of type Textbox instead. This enables you to directly use the variable
instead of finding the Textbox by a String. Therefore I currently don't know
what's the problem. By saying "just using sDateBox" I meant, well, just use
it like any other variable, for example in

sDateBox.Text = m_picker.SelectionRange.Start.Date.ToShortDateString

I do not know when you intend to set variable 'sDateBox'. Maybe in the
Textbox' Enter or GotFocus event? I don't know. Is this the problem?



Armin
 
A

Andrew Morton

Jack said:
My guess is that the error occurs on the second of these lines:

Dim textBoxNew As New TextBox
textBoxNew.Name = "tbVerlooptTot"

"Dim textBoxNew As New TextBox" does not create at TextBox object.

Oh yes it does: "Dim textBoxNew As New TextBox" is short for "Dim textBoxNew
As TextBox = New TextBox".

Andrew
 
A

Andrew Morton

Jack said:
My guess is that the error occurs on the second of these lines:

Dim textBoxNew As New TextBox
textBoxNew.Name = "tbVerlooptTot"

"Dim textBoxNew As New TextBox" does not create at TextBox object.

Oh yes it does: "Dim textBoxNew As New TextBox" is short for "Dim textBoxNew
As TextBox = New TextBox".

Andrew
 
J

Jack Jackson

Oh yes it does: "Dim textBoxNew As New TextBox" is short for "Dim textBoxNew
As TextBox = New TextBox".

Andrew

My mistake. I meant to say "Dim textBoxNew As TextBox".
 
Top