Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Code Question
Message
De
31/08/2015 15:12:13
 
 
À
31/08/2015 14:38:44
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Versions des environnements
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01623961
Message ID:
01624033
Vues:
49
>>>>>>>I think there's a simple answer to this but I can't think of it or find it.
>>>>>>>I want to set the character casing of all textboxes on a form to upper.
>>>>>>>I get this far and can't access the charactercasing property of the control.
>>>>>>>Any help appreciated.
>>>>>>>
>>>>>>>
>>>>>>>      foreach (Control c in parentForm.Controls)
>>>>>>>            {
>>>>>>>                if (c is TextBox)
>>>>>>>                {
>>>>>>>
>>>>>>>                }
>>>>>>>            }
>>>>>>>
>>>>>>
>>>>>>Did you try instead (not tested)
>>>>>>
>>>>>>var c as TextBox
>>>>>>
>>>>>>if (c!=null)
>>>>>> {
>>>>>>
>>>>>>  }
>>>>>>
>>>>>>See
>>>>>>
>>>>>>https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx?f=255&MSPPError=-2147217396
>>>>>
>>>>>
>>>>>Thanks, Naomi
>>>>>
>>>>>I woke up from my trance and this did it
>>>>>
>>>>>
>>>>>
>>>>> foreach (Control c in parentForm.Controls)
>>>>>            {
>>>>>                if (c is TextBox)
>>>>>                {
>>>>>                    TextBox tb = c as TextBox;
>>>>>                    tb.CharacterCasing = CharacterCasing.Upper;
>>>>>                }
>>>>>            }
>>>>>
A Linq way:
foreach (Control c in parentForm.Controls.Where(x=>x is TextBox))
>>>>            {
>>>>                ((TextBox)c).CharacterCasing = CharacterCasing.Upper;
>>>>            }
>>>
>>>Thank you, Viv.
>>>I couldn't think of this syntax.
>>>
>>>((TextBox)c).CharacterCasing = CharacterCasing.Upper;
>>>
>>>The one I used worked but I prefer this one and I substituted it.
>>
>>A better LINQ way:
>>
foreach (TextBox c in parentForm.Controls.OfType< TextBox>())
>>            {
>>                c.CharacterCasing = CharacterCasing.Upper;
>>            }
>>Good. I'd forgotten about OfType(). Don't know if it's any faster because, internally, I think it does a 'is X' anyway ?

Performance is probably similar, but it cleans up the code by getting rid of the lambda and cast.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform