-
Insert into from Label1 to Label10..
There are 10Labels from Label1 to Label10...
I want to insert like this
label1.text = 10 , label2.text = 20 , .... , label10.text = 100
I want to use a For ~ Next.
-
Re: Insert into from Label1 to Label10..
"PilSung" <youthcom@hanmir.com> wrote:
>There are 10Labels from Label1 to Label10...
>I want to insert like this
>
>label1.text = 10 , label2.text = 20 , .... , label10.text = 100
>
>I want to use a For ~ Next.
>
>
dim i as integer
for i = 0 to 10
me.controls(i).text = i
next
see region " Windows Form Designer generated code " in
'
'Form1 (Main Form)
'
Me.Controls.AddRange(New System.Windows.Forms.Control() { ..... }
for controls index.
-
Re: Insert into from Label1 to Label10..
Error occured..!!
'text' is not a member of system.web.UI.control.
and there are mixed with textbox,button,dropdownlist etc...
"Edwin" <edwin.arif@chek.com> wrote in message news:3d5b661f$1@10.1.10.29...
>
> "PilSung" <youthcom@hanmir.com> wrote:
> >There are 10Labels from Label1 to Label10...
> >I want to insert like this
> >
> >label1.text = 10 , label2.text = 20 , .... , label10.text = 100
> >
> >I want to use a For ~ Next.
> >
> >
>
> dim i as integer
> for i = 0 to 10
> me.controls(i).text = i
> next
>
> see region " Windows Form Designer generated code " in
>
> '
> 'Form1 (Main Form)
> '
> Me.Controls.AddRange(New System.Windows.Forms.Control() { ..... }
>
> for controls index.
>
-
Re: Insert into from Label1 to Label10..
in #Region " Windows Form Designer generated code "
Me.Controls.AddRange(New System.Windows.Forms.Control() { .... })
you can rearrange your labels so it index is from 0 to 10 and not mixed with
other object.
Example :
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.label0, Me.label1,
...., Me.label10, Me.textbox1, Me.statusBar1, etc, etc})
this will make your label0 to be controls(0), label1 to be controls(1) and
so on. So you can access your label0 with Me.controls(0) because they are
the same object.
"PilSung" <youthcom@hanmir.com> wrote:
>Error occured..!!
>'text' is not a member of system.web.UI.control.
>
>and there are mixed with textbox,button,dropdownlist etc...
>
>
>
>"Edwin" <edwin.arif@chek.com> wrote in message news:3d5b661f$1@10.1.10.29...
>>
>> "PilSung" <youthcom@hanmir.com> wrote:
>> >There are 10Labels from Label1 to Label10...
>> >I want to insert like this
>> >
>> >label1.text = 10 , label2.text = 20 , .... , label10.text = 100
>> >
>> >I want to use a For ~ Next.
>> >
>> >
>>
>> dim i as integer
>> for i = 0 to 10
>> me.controls(i).text = i
>> next
>>
>> see region " Windows Form Designer generated code " in
>>
>> '
>> 'Form1 (Main Form)
>> '
>> Me.Controls.AddRange(New System.Windows.Forms.Control() { ..... }
>>
>> for controls index.
>>
>
>
-
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.
That said, in answer to your original question:
"PilSung" <youthcom@hanmir.com> wrote:
>There are 10Labels from Label1 to Label10...
>I want to insert like this
>label1.text = 10 , label2.text = 20 , .... , label10.text = 100
Here's one way to handle it:
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
--
Constance Petersen, DevX newsgroup section leader
New look; new content: http://www.smartisans.com/
Hot off the press!
"Programming the Web with Visual Basic .NET"
http://amazon.com/exec/obidos/ASIN/1...tancepeterseA/
--
Please reply in the newsgroup so everyone can benefit
-
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.
Public Sub SetTheLabels()
Dim labelArray() As TextBox = _
{Label1, Label2, Label3, Label4, Label5, _
Label6, Label7, Label8, Label9, Label10}
Dim i As Integer
For i = labelArray.GetLowerBound(0) To labelArray.Length - 1
labelArray(i).Text = ( (i + 1) * 10 ).ToString
Next
End Sub
Hope this helps,
Ian.
-
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...
Private ctrls As ControlDictionary
....create a new ControlDictionary when the form loads...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ctrls = New ControlDictionary(Me)
...
End Sub
....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!
Ian.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|