Problems with programmatically added control - can't get properties, can't even reference control!

  • Thread starter Thread starter Just_a_fan
  • Start date Start date
J

Just_a_fan

I am adding a bunch of controls with the code below.

Problem 1: When program flow passes to "UpperChanged" when I click it,
the control name is undefined. When I enter:

If udUpperLim1.Value > 1 Then

I get an error that udUpperLim1 is "Not Defined" so I cannot get the
value in the control.

Problem 2: When I use sender.tostring, it returns the class name of the
control, not its assigned name. I get
"System.Windows.Forms.NumericUpDown" and not udUpperLim1 and I don't
know who was changed and along with the preceding problem, even if I
did, I can't reference it!

The folks who decided to remove control arrays DID NOT DO US ANY
FAVORS!!!!!!!!!!

This is all straight out of a book and is not doing anything useful for
me.

Help, Please!

Mike Morrow
MSDN:MMSA00E62537

iTop = 84

Dim udUpperLim1 As New NumericUpDown
udUpperLim1.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim1)
AddHandler udUpperLim1.ValueChanged, AddressOf UpperChanged
udUpperLim1.Value = My.Settings.Alarm1

Dim udUpperLim3 As New NumericUpDown
udUpperLim3.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim3)
AddHandler udUpperLim3.ValueChanged, AddressOf UpperChanged
udUpperLim3.Value = My.Settings.Alarm3

Dim udUpperLim5 As New NumericUpDown
udUpperLim5.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim5)
AddHandler udUpperLim5.ValueChanged, AddressOf UpperChanged
udUpperLim5.Value = My.Settings.Alarm5
 
Problem 1: When program flow passes to "UpperChanged" when I click it,
the control name is undefined. When I enter:

If udUpperLim1.Value > 1 Then

I get an error that udUpperLim1 is "Not Defined" so I cannot get the
value in the control.

This is because in your code your not giving the control an ID. Your naming
the variable that is holding the control but not setting the ID property.

Dim udUpperLim1 As New NumericUpDown
'**************************
'Set the ID property of the control
udUpperLim1.ID = "udUpperLim1"
'**************************
udUpperLim1.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim1)
AddHandler udUpperLim1.ValueChanged, AddressOf UpperChanged
udUpperLim1.Value = My.Settings.Alarm1
Problem 2: When I use sender.tostring, it returns the class name of the
control, not its assigned name. I get
"System.Windows.Forms.NumericUpDown" and not udUpperLim1 and I don't
know who was changed and along with the preceding problem, even if I
did, I can't reference it!

This is because you are calling .ToString() which returns the objects type
in a string ... try using sender.ID

- Adam
 
SneeKeeFahk said:
This is because in your code your not giving the control an ID. Your
naming the variable that is holding the control but not setting the ID
property.

Dim udUpperLim1 As New NumericUpDown
'**************************
'Set the ID property of the control
udUpperLim1.ID = "udUpperLim1"
'**************************
udUpperLim1.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim1)
AddHandler udUpperLim1.ValueChanged, AddressOf UpperChanged
udUpperLim1.Value = My.Settings.Alarm1


This is because you are calling .ToString() which returns the objects type
in a string ... try using sender.ID

- Adam

If you are doing ASP use the ID property. If you are using winforms use the
Name property.

LS
 
Mike,

See my sample in your latter question. Be aware that as you drag a control
on a form the ObjectName of the control is in ASPNET or Windowsforms always
the ClassName extended with a number from one to n.

Cor
 
<snip>
The folks who decided to remove control arrays DID NOT DO US ANY
FAVORS!!!!!!!!!!
<snip>
Mike Morrow
MSDN:MMSA00E62537
<snip>

Hi Mike,

When I saw the above statement in your post I thought that just couldn't be.
Surely, I thought, arrays of controls are still possible. If not, I have a
lot of code which is not going to work. But then I remembered, from another
of your threads, that you are, like me, an old mainframer - so certainly a
guy of superior intelligence. And I thought therefore I'd better see if
"control arrays" are different from arrays of controls. So I did a search
(using a VB Express which I downloaded just a few days ago) and found the
following statement ...

The BaseControlArray class is the base class for all control arrays used in
applications upgraded from Visual Basic 6.0.

Having never used VB 6 and having never looked into BaseControlArrays I may
not fully understand it, but it looks like there might be some new and
improved version of "control arrays" - whatever they were. And if control
arrays are what I'd imagine them to be perhaps you could create an array of
controls.

ALSO, not sure what you are trying to do, but when I've needed to know
something about a control which caused an event handler to be invoked (when
one event handler is shared by many similar controls, I've used the Tag
property. It's defined as an object so you can jam as much information into
it as you need to.

Are you an ex-mainframer or a current mainframer who also does .Net?

SVC 3, Bob
 
I did DOS, DOS/VSE and a couple of variations of MVS during my time with
big iron. Mostly assembler, some operating system support with heavy
emphasis on assembler, machine language and REXX. I have known and work
with about a dozen languages but have gone to PC stuff exclusively now.

I have written a little to a lot in all of the following:

1620 Fortran II (a little)
1620 Assembler (a lot)
Fortran IV (moderate)
IBM Assembler (a lot)
IBM Assembler Macro (a lot)
Pascal (till I got tired of typing ";"s)
Atari "Action" (well, I was broke at the time)
C (a little)
CLIST (moderate)
REXX (a lot)
Visual Basic 3-6 (a lot)
Visual Basic.Net (becoming a lot, quite rapidly)

I must have missed a few. I miss assembler and REXX the most. REXX did
some stuff that nothing else did and made life easier.

I remember reading about Array of Controls but cannot find anything in
the index of my books. Going online.

Mike
 
OK. I had not run across this .ID stuff in the two books I have read so
far. That should help. Thanks.
 
You're really missing the point.

It is NOT the control on the form that is goes out of scope. It is your
LOCAL reference to the control that goes out of scope.

If you do not want your references to go out of scope then define them at a
higher level.

Public Class MyForm
Inherits Form

Private udUpperLim1 As NumericUpDown
Private udUpperLim3 As NumericUpDown
Private udUpperLim4 As NumericUpDown

Private Sub Me_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Load

iTop = 84

udUpperLim1 = New NumericUpDown
udUpperLim1.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim1)
AddHandler udUpperLim1.ValueChanged, AddressOf UpperChanged
udUpperLim1.Value = My.Settings.Alarm1

udUpperLim3 = New NumericUpDown
udUpperLim3.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim3)
AddHandler udUpperLim3.ValueChanged, AddressOf UpperChanged
udUpperLim3.Value = My.Settings.Alarm3

udUpperLim5 = New NumericUpDown
udUpperLim5.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim5)
AddHandler udUpperLim5.ValueChanged, AddressOf UpperChanged
udUpperLim5.Value = My.Settings.Alarm5

End Sub

Now, udUpperLim1, udUpperLim3 and udUpperLim5 can be referenced from
anywhere inside MyForm, and, in normal circumstances, will not go out of
scope until MyForm is 'disposed'.

If you look in the 'designer' code for a form you will see that this is
similar to the code generated by VS from the graphical designer.
 
I will do as you say. However, it looks like it is being defined twice.
When I get to a stopping point, I will certainly try this all out and be
ready to use it next time.

Thanks,
Mike
 
I remember reading about Array of Controls but cannot find anything in
the index of my books. Going online.

Control arrays as they existed in VB6 do not exist in VB.Net. That
is, there is not a index property for a control and the IDE will not
create the array automatically if you give a control the same name as
an existing one.

However, you can create an array of controls just like you would
create an array of integers or an array of strings:

Dim MyControls(12) As Control 'An array of 13 controls

If you wish to be more specific:

Dim MyTextBoxes(12) As TextBox 'An array of 13 text boxes

To assign objects to the arrays, you can use controls that already
exist on the form:

MyTextBoxes(0) = Form1.TextBox1
MyTextBoxes(1) = Form1.TextBox2
'etc.

Or you can create new controls:

MyTextBoxes(0) = New TextBox()
MyTextBoxes(1) = New TextBox()

Then you can wire up their events:

AddHandler MyTextBoxes(0).TextChanged, AddressOf Text_TextChanged
AddHandler MyTextBoxes(1).TextChanged, AddressOf Text_TextChanged

And then in the TextChanged event, you can work with the textbox:

Private Sub Text_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim tmpTextBox As TextBox = DirectCast(sender, TextBox) 'Get a
reference to the textbox that whose text changed

MsgBox(tmpTextBox.Text) 'display its text
End Sub

Chris
 
... snip ...
I remember reading about Array of Controls but cannot find anything in
the index of my books. Going online.

Mike

I didn't mean Array of Controls, I meant array of controls. I.E.

Dim whatever() as TextBox

just like Dim whatever() as String.

Bob
 
Bob,

What do you mean by a mainframe.

I assume you are talking about non event driven programming, Be aware that
it is mostly something inherited from the puchcard time.

Most moderen programming is done by handling events raised by humans (wm
messages). Although I am at the moment busy with a project that is batch
processing, the same as in the punchcard time, beside a start I don't need
any events raised by humans.

However, as you are interacting with humans, on whatever device then you
need events to fullfil the way an user expects a program is working in this
milenium.

An event is (as it has to be used) always bound to a method which handles
the event. It is in fact very simple but mostly very difficult described.
In C# the call they first make a delegate (simple a let say in mainframe
terminology a reference to a program address where the method starts), and
then invoke that address (the delegate).

The advance from VB is that is has more less semi hardware solutions.

Cor
 
Back
Top