| |
VB.NET - Ein- und UmsteigerRe: Immer Wiederkehrenden Code kürzen? | | | Autor: Manfred X | Datum: 29.10.23 20:43 |
| Hallo!
Deine Idee ging in die richtige Richtung.
Packe die Pictureboxen in ein Array und nutze den Wert im Label
als Index für den Zugriff auf die entsprechende Picturebox.
Beispiel:
Auf Formularebene deklarieren:
Dim myPictureboxen(45) As PictureBox
For ind As Integer = 0 To myPictureboxen.Length - 1
myPictureboxen(ind) = New PictureBox With
{.Parent = Me, .Left = 10, .Top = ind * 20, .Width = 15, .Height = 15,
.BorderStyle = BorderStyle.Fixed3D}
Next ind
Dim lbl As New Label, lbl2 As New Label
lbl.Text = "12" : lbl2.Text = "Feld"
If lbl2.text = "Feld" Then myPictureboxen(CInt(lbl.Text)).BackColor = _
Color.Yellow
Beitrag wurde zuletzt am 29.10.23 um 20:46:52 editiert. | |
Immer Wiederkehrenden Code kürzen? | | | Autor: blueberlin | Datum: 29.10.23 19:31 |
| Hallo,
Ich habe ein sehr Simplen Quellcode für ein Spiel was ich gerade Bastel.
Der Code ist immer der selbe, leider komme ich auf keine Lösung wie ich ihn kürzen kann.
Meine Idee mit
if Label2.Text="Feld" then Hauptfenster.PictureBox(Label3.Text).Visible = False wäre sicherlich schön gewesen, klappt aber leider nicht.
Habt ihr ein Tipp für mich wie ich da am besten rangehe?
If Label2.Text = "Feld" And Label3.Text = "1" Then _
Hauptfenster.PictureBox1.Visible = False
If Label2.Text = "Feld" And Label3.Text = "2" Then _
Hauptfenster.PictureBox2.Visible = False
If Label2.Text = "Feld" And Label3.Text = "3" Then _
Hauptfenster.PictureBox3.Visible = False
If Label2.Text = "Feld" And Label3.Text = "4" Then _
Hauptfenster.PictureBox4.Visible = False
If Label2.Text = "Feld" And Label3.Text = "5" Then _
Hauptfenster.PictureBox5.Visible = False
If Label2.Text = "Feld" And Label3.Text = "6" Then _
Hauptfenster.PictureBox6.Visible = False
If Label2.Text = "Feld" And Label3.Text = "7" Then _
Hauptfenster.PictureBox7.Visible = False
If Label2.Text = "Feld" And Label3.Text = "8" Then _
Hauptfenster.PictureBox8.Visible = False
If Label2.Text = "Feld" And Label3.Text = "9" Then _
Hauptfenster.PictureBox9.Visible = False
If Label2.Text = "Feld" And Label3.Text = "10" Then _
Hauptfenster.PictureBox10.Visible = False
If Label2.Text = "Feld" And Label3.Text = "11" Then _
Hauptfenster.PictureBox11.Visible = False
If Label2.Text = "Feld" And Label3.Text = "12" Then _
Hauptfenster.PictureBox12.Visible = False
If Label2.Text = "Feld" And Label3.Text = "13" Then _
Hauptfenster.PictureBox13.Visible = False
If Label2.Text = "Feld" And Label3.Text = "14" Then _
Hauptfenster.PictureBox14.Visible = False
If Label2.Text = "Feld" And Label3.Text = "15" Then _
Hauptfenster.PictureBox15.Visible = False
If Label2.Text = "Feld" And Label3.Text = "16" Then _
Hauptfenster.PictureBox16.Visible = False
If Label2.Text = "Feld" And Label3.Text = "17" Then _
Hauptfenster.PictureBox17.Visible = False
If Label2.Text = "Feld" And Label3.Text = "18" Then _
Hauptfenster.PictureBox18.Visible = False
If Label2.Text = "Feld" And Label3.Text = "19" Then _
Hauptfenster.PictureBox19.Visible = False
If Label2.Text = "Feld" And Label3.Text = "20" Then _
Hauptfenster.PictureBox20.Visible = False
If Label2.Text = "Feld" And Label3.Text = "21" Then _
Hauptfenster.PictureBox21.Visible = False
If Label2.Text = "Feld" And Label3.Text = "22" Then _
Hauptfenster.PictureBox22.Visible = False
If Label2.Text = "Feld" And Label3.Text = "23" Then _
Hauptfenster.PictureBox23.Visible = False
If Label2.Text = "Feld" And Label3.Text = "24" Then _
Hauptfenster.PictureBox24.Visible = False
If Label2.Text = "Feld" And Label3.Text = "25" Then _
Hauptfenster.PictureBox25.Visible = False
If Label2.Text = "Feld" And Label3.Text = "26" Then _
Hauptfenster.PictureBox26.Visible = False
If Label2.Text = "Feld" And Label3.Text = "27" Then _
Hauptfenster.PictureBox27.Visible = False
If Label2.Text = "Feld" And Label3.Text = "28" Then _
Hauptfenster.PictureBox28.Visible = False
If Label2.Text = "Feld" And Label3.Text = "29" Then _
Hauptfenster.PictureBox29.Visible = False
If Label2.Text = "Feld" And Label3.Text = "30" Then _
Hauptfenster.PictureBox30.Visible = False
If Label2.Text = "Feld" And Label3.Text = "31" Then _
Hauptfenster.PictureBox31.Visible = False
If Label2.Text = "Feld" And Label3.Text = "32" Then _
Hauptfenster.PictureBox32.Visible = False
If Label2.Text = "Feld" And Label3.Text = "33" Then _
Hauptfenster.PictureBox33.Visible = False
If Label2.Text = "Feld" And Label3.Text = "34" Then _
Hauptfenster.PictureBox34.Visible = False
If Label2.Text = "Feld" And Label3.Text = "35" Then _
Hauptfenster.PictureBox35.Visible = False
If Label2.Text = "Feld" And Label3.Text = "36" Then _
Hauptfenster.PictureBox36.Visible = False
If Label2.Text = "Feld" And Label3.Text = "37" Then _
Hauptfenster.PictureBox37.Visible = False
If Label2.Text = "Feld" And Label3.Text = "38" Then _
Hauptfenster.PictureBox38.Visible = False
If Label2.Text = "Feld" And Label3.Text = "39" Then _
Hauptfenster.PictureBox39.Visible = False
If Label2.Text = "Feld" And Label3.Text = "40" Then _
Hauptfenster.PictureBox40.Visible = False
If Label2.Text = "Feld" And Label3.Text = "41" Then _
Hauptfenster.PictureBox41.Visible = False
If Label2.Text = "Feld" And Label3.Text = "42" Then _
Hauptfenster.PictureBox42.Visible = False
If Label2.Text = "Feld" And Label3.Text = "43" Then _
Hauptfenster.PictureBox43.Visible = False
If Label2.Text = "Feld" And Label3.Text = "44" Then _
Hauptfenster.PictureBox44.Visible = False
If Label2.Text = "Feld" And Label3.Text = "45" Then _
Hauptfenster.PictureBox45.Visible = False | |
Re: Immer Wiederkehrenden Code kürzen? | | | Autor: blueberlin | Datum: 29.10.23 21:01 |
| Ich danke für diesen super schnellen Tipp, werde ich morgen direkt ausprobieren.
Sehr nett von dir | |
Re: Immer Wiederkehrenden Code kürzen? | | | Autor: Kuno60 | Datum: 29.10.23 21:13 |
| Falls du die Steuerelemente lieber per Designer erstellst, kannst du es auch so machen:
If Label2.Text = "Feld" Then
Dim t As String = Label3.Text
Dim pics As Control() = Hauptfenster.Controls.Find("PictureBox" & t, True)
If pics.Length = 1 Then pics(0).Visible = False
End If | |
Re: Immer Wiederkehrenden Code kürzen? | | | Autor: Manfred X | Datum: 29.10.23 21:32 |
| Hallo!
Damit das funktioniert, muss die Name-Eigenschaft der
Pictureboxen geeignet belegt worden sein, die von der
Find-Methode als Key verwendet wird.
Für den Zugriff auf die spezifischen Eigenschaften der Picturebox
ist im Code ggf. ein Directcast oder TryCast erforderlich. | |
| Sie sind nicht angemeldet! Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.
Einloggen | Neu registrieren |
|
|
sevISDN 1.0
Überwachung aller eingehender Anrufe!
Die DLL erkennt alle über die CAPI-Schnittstelle eingehenden Anrufe und teilt Ihnen sogar mit, aus welchem Ortsbereich der Anruf stammt. Weitere Highlights: Online-Rufident, Erkennung der Anrufbehandlung u.v.m. Weitere InfosTipp des Monats Neu! sevEingabe 3.0
Einfach stark!
Ein einziges Eingabe-Control für alle benötigten Eingabetypen und -formate, inkl. Kalender-, Taschenrechner und Floskelfunktion, mehrspaltige ComboBox mit DB-Anbindung, ImageComboBox u.v.m. Weitere Infos
|
|
|
Copyright ©2000-2024 vb@rchiv Dieter Otter Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.
Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel
|
|