If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Insert into from Label1 to Label10..

Hi PilSung,

"PilSung" <youthcom@hanmir.com> wrote in message news:3d5b67e9@10.1.10.29...
> Error occured..!!
> 'text' is not a member of system.web.UI.control.
> and there are mixed with textbox,button,dropdownlist etc...

You might want to start posting ASP.NET questions into the web.asp.plus
newsgroup. Or, when posting in this newsgroup, you should specify that your
question is about WebForms vs WinForms.

Dim l As new Label()
Dim c As Control
Dim t As Type
For Each c In Me.FindControl("Form1").Controls
t = c.GetType()
If t.Namespace = "System.Web.UI.WebControls" Then
If t.Name = "Label" Then
l = CType(c, Label)
l.Text = Mid(l.ID.ToString, 6) & "0"
c = l
End If
End If
Next

Re: Insert into from Label1 to Label10..

Hi,
<snip>
> you can rearrange your labels so it index is from 0 to 10 and not mixed
with
> other object.

I don't think this is the best idea, since it relies on auto-generated code
from the IDE (which seems stable but isn't guaranteed to be). Your safest
bet is to but the controls you're interested in into an array and then
iterate over the array.

Re: Insert into from Label1 to Label10..

> For i = labelArray.GetLowerBound(0) To labelArray.Length - 1

Ian: Question: Why did you use GetLowerBound for the starting value of your
For...Next loop, but not GetUpperBound for the ending value? Alternatively,
since .Length - 1 assumes that the lower bound is 0, why not simply
hard-code 0 as the starting value?

I ask because I recently wrestled with the question of which method to use
when iterating over an array; I never considered using both. ;-)
---
Phil Weber

Re: Insert into from Label1 to Label10..

In addition to all the other answers you've received, here are some more
solutions. You can see that there are several ways to handle the problem,
depending on how fast you need it to be, and how much code you're willing to
add to the form, and how much of that you're willing to write manually, as
opposed to moving controls around in the designer. The example that Edwin
gave works fine, but it means you must position the controls with code. The
example that Ian gave shows you how to create an array of selected controls,
and Constance showed a specific solution to your problem.

It's easy to get a reference to any control by name at runtime. For example,
here's a function that returns a control given the control's name. This acts
like the FindControl method you'll find in a Web Form.

Private Function FindControl(ByVal aName As String) As Control
Dim c As Control
Dim i As Integer
For i = 0 To Me.Controls.Count - 1
c = Me.Controls.Item(i)
If String.Compare(c.Name, aName, True) = 0 Then
Return c
End If
Next
Return Nothing
End Function

You can use that to write code such as this:
Dim c As Control
Dim tx As TextBox
Dim i As Integer
For i = 1 To 5
c = Me.FindControl("TextBox" & CStr(i))
If Not c Is Nothing AndAlso TypeOf c Is TextBox Then
tx = DirectCast(c, TextBox)
tx.Text = CStr(i * 10)
End If
Next

Note that the FindControl function returns Nothing if it can't find a
control with the given name, and that the return value is Control, not
TextBox, so you need to cast the returned Control to the correct type. In
your particular example, that's not strictly necessary, because the
System.Windows.Forms.Control class has a Text property, so you could set
that property regardless of the type of Control returned.

A more complex (once) way is to create a ControlDictionary class that
inherits from DictionaryBase and add all the form's controls to it on
startup.
DISCLAIMER: This class is not suitable for production work. Add your own
error-trapping and index checks. Also, it doesn't implement all the methods
you'd normally want to implement for an inherited Dictionary.

Finding a control using this class is considerably faster than finding a
control with the FindControl method shown above, but you're unlikely to
notic much difference unless you perform huge numbers of lookups in a loop,
or unless you have a large number of controls on the form. The big advantage
is that it's generic, and you need only two lines of code in any form to
take advantage of it.

Imports System.Windows.Forms.Control
Imports System.Collections.Specialized
Public Class ControlDictionary
Inherits DictionaryBase
Public Sub New(ByVal aForm As Form)
Dim c As Control
For Each c In aForm.Controls
Me.Add(c)
Next
End Sub
Public Sub Add(ByVal c As Control)
Me.Dictionary.Add(c.Name, c)
End Sub
Public Sub Remove(ByVal c As Control)
Me.Dictionary.Remove(c.Name)
End Sub
Public ReadOnly Property Keys() As String()
Get
Dim sKeys(Me.Dictionary.Count - 1) As String
Me.Dictionary.Keys.CopyTo(sKeys, 0)
Return sKeys
End Get
End Property
Public ReadOnly Property Controls() As Control()
Get
Dim c As Control
Dim i As Integer
Dim s As String
Dim myKeys() As String
myKeys = Me.Keys
Dim ctrls(myKeys.Length - 1) As Control
For i = 0 To myKeys.Length - 1
ctrls(i) = Me.Item(myKeys(i))
Next
Return ctrls
End Get
End Property
Default Public ReadOnly Property Item(ByVal aKey As String) As Control
Get
Return DirectCast(Me.Dictionary.Item(aKey), Control)
End Get
End Property
End Class

This class requires you to pass a Form to the constructor. Now, you can
simply define a class member as a ControlDictionary...

....and then you can obtain any control on the form by name without the
overhead of the loop shown in the FindControl example earlier in this note.

Dim c As Control
Dim tx As TextBox
Dim i As Integer
For i = 1 To 10
c = ctrls.Item("TextBox" & CStr(i))
If Not c Is Nothing AndAlso TypeOf c Is TextBox Then
tx = DirectCast(c, TextBox)
tx.Text = CStr(i * 10)
End If
Next

Again, note that you have to cast the Control returned by the Item property
to the correct type.

Re: Insert into from Label1 to Label10..

Hi, Phil,
> Ian: Question: Why did you use GetLowerBound for the starting value of
your
> For...Next loop, but not GetUpperBound for the ending value?
Alternatively,
> since .Length - 1 assumes that the lower bound is 0, why not simply
> hard-code 0 as the starting value?

I plead lack of sleep! I honestly can't describe the though process that
went into that, um, decision. I almost never use GetLowerBound in my own
code since all my arrays are 0-based.

I suppose the only good thing I can say about that line of code is that it
stands as an excellent example of how NOT to iterate across arrays!