Autor Beitrag
C#
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: Do 01.12.11 21:26 
Hey Forum,

habe endlich mal wieder Zeit gefunden zu proggen. Und schon habe ich wieder eine Frage:
Ich habe ein Panel. Darum befinden sich wiederum andere Panels, aber auch andere Objekte. Wie kann ich nun (z.B.) in einer foreach Schleife herausfinden, ob das Objekt ein Panel ist. Meine Ansätze:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
try
            {
                foreach (Panel elements in MainPanel.Controls)
                    elements.Location = new Point(elements.Location.X - (e.NewValue - e.OldValue), elements.Location.Y);
            }
            catch (InvalidCastException) { } //Das ist die Exception die auslöst, wenn das Objekt nicht konvertierbar ist
            catch (Exception ex) { MessageBox.Show(ex.Message, "ERROR"); return; }

Das hier geht doch bestimmt auch ohne try-Block.
Ich habs auch schon mit element.GetType() == Panel probiert aber das schluckt der Compiler nicht.

Hat jemand eine Idee?

EDIT:
Habs grad geschafft xD. Mit foreach Control element in MainPanel.Controls.Cast<Control>().OfType<Panel>()

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 01.12.11 21:57 
Den Cast von Control nach Control ist überflüssig. Nur OfType<Panel>() reicht.
C# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: Do 01.12.11 22:01 
Hoppla stimmt ja xD. Ich benutze bei Linq meistens die Where()-Methode und da brauche ich in vielen Fällen das Cast<>() davor.

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler