use variable as textbox name?

  • Thread starter Thread starter Co
  • Start date Start date
C

Co

Hi All,

I use following code to update a textbox when a date has been chosen
from my MonthCalendar.
Since 6 textboxes use the same MonthCalendar I need to tell him which
textbox was used.
Therefore I have the variable: sWhichDateBox.
How can I use that string to become the name of a textbox:
SelectedTextBox.Text =
m_picker.SelectionRange.Start.Date.ToShortDateString()


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 SelectedTextBox As TextBox = Nothing
SelectedTextBox.Name = sWhichDateBox
'Display the Start and End property values of
'the SelectionRange object in the text boxes.
SelectedTextBox.Text =
m_picker.SelectionRange.Start.Date.ToShortDateString()
SelectedTextBox.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()
SelectedTextBox.Parent.Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
End If
End Sub

Regards
Marco
The Netherlands
 
Co said:
Hi All,

I use following code to update a textbox when a date has been chosen
from my MonthCalendar.
Since 6 textboxes use the same MonthCalendar I need to tell him which
textbox was used.
Therefore I have the variable: sWhichDateBox.

Is there a reason why you don't declare "WhichDateBox As Textbox"?


Armin
 
Co said:
Hi All,

I use following code to update a textbox when a date has been chosen
from my MonthCalendar.
Since 6 textboxes use the same MonthCalendar I need to tell him which
textbox was used.
Therefore I have the variable: sWhichDateBox.

Is there a reason why you don't declare "WhichDateBox As Textbox"?


Armin
 
Try using the user-defined Tag property for each text box.

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
 
Try using the user-defined Tag property for each text box.

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
 
Co said:
Hi All,

I use following code to update a textbox when a date has been chosen
from my MonthCalendar.
Since 6 textboxes use the same MonthCalendar I need to tell him which
textbox was used.
Therefore I have the variable: sWhichDateBox.
How can I use that string to become the name of a textbox:
SelectedTextBox.Text =
m_picker.SelectionRange.Start.Date.ToShortDateString()


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 SelectedTextBox As TextBox = Nothing
SelectedTextBox.Name = sWhichDateBox
'Display the Start and End property values of
'the SelectionRange object in the text boxes.
SelectedTextBox.Text =
m_picker.SelectionRange.Start.Date.ToShortDateString()
SelectedTextBox.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()
SelectedTextBox.Parent.Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
End If
End Sub

Regards
Marco
The Netherlands


It sounds to me like you have a datetime object that is being set or
displayed by the calander control as well as the six text boxes. If this is
the case, why aren't all six text boxes just updated with the new datetime
from the calander? Either you are making this too complicated or, more
likely, I'm over simplifying your issue.
 
Co said:
Hi All,

I use following code to update a textbox when a date has been chosen
from my MonthCalendar.
Since 6 textboxes use the same MonthCalendar I need to tell him which
textbox was used.
Therefore I have the variable: sWhichDateBox.
How can I use that string to become the name of a textbox:
SelectedTextBox.Text =
m_picker.SelectionRange.Start.Date.ToShortDateString()


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 SelectedTextBox As TextBox = Nothing
SelectedTextBox.Name = sWhichDateBox
'Display the Start and End property values of
'the SelectionRange object in the text boxes.
SelectedTextBox.Text =
m_picker.SelectionRange.Start.Date.ToShortDateString()
SelectedTextBox.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()
SelectedTextBox.Parent.Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
End If
End Sub

Regards
Marco
The Netherlands


It sounds to me like you have a datetime object that is being set or
displayed by the calander control as well as the six text boxes. If this is
the case, why aren't all six text boxes just updated with the new datetime
from the calander? Either you are making this too complicated or, more
likely, I'm over simplifying your issue.
 
It sounds to me like you have a datetime object that is being set or
displayed by the calander control as well as the six text boxes.  If this is
the case, why aren't all six text boxes just updated with the new datetime
from the calander?  Either you are making this too complicated or, more
likely, I'm over simplifying your issue.

It's a search form with six textboxes holding dates.
All can have different values.

Marco
 
It sounds to me like you have a datetime object that is being set or
displayed by the calander control as well as the six text boxes.  If this is
the case, why aren't all six text boxes just updated with the new datetime
from the calander?  Either you are making this too complicated or, more
likely, I'm over simplifying your issue.

It's a search form with six textboxes holding dates.
All can have different values.

Marco
 
What does not work? What did you try?  More information, please.

Armin

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

Private Sub CreateDTP()

Dim t As New TextBox
t.Name = sDateBox
't = DirectCast(m_Controls.Item(sDateBox), TextBox)
bCalOpened = True
m_picker = New MonthCalendar
m_picker.BringToFront()
m_picker.MaxSelectionCount = 1
m_picker.Size = t.Size
m_picker.BackColor = Color.Beige
t.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 New TextBox
'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.Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
t.Dispose()
End If
End Sub

Marco
 
What does not work? What did you try?  More information, please.

Armin

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

Private Sub CreateDTP()

Dim t As New TextBox
t.Name = sDateBox
't = DirectCast(m_Controls.Item(sDateBox), TextBox)
bCalOpened = True
m_picker = New MonthCalendar
m_picker.BringToFront()
m_picker.MaxSelectionCount = 1
m_picker.Size = t.Size
m_picker.BackColor = Color.Beige
t.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 New TextBox
'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.Controls.Remove(m_picker)
m_picker = Nothing
bCalOpened = False
t.Dispose()
End If
End Sub

Marco
 
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
 
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
 
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
 
Back
Top