Autor Beitrag
27sharp
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Fr 28.06.13 08:31 
Hallo Leute ,

ich war grade dabei ein Programm zu schreiben dabei bin ich auf ein Problem gestoßen:

Ich möchte aus einer listbox mit 114 Werten eine switch abfrage machen bei der betätigung der einzelnen Werte , doch gibt es keine Vereinfachung dafür ?

der Code :

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
int list = listBox1.SelectedIndex;

            switch (list)
            {

                case 0 :

                    1

                break;

                case 1:

                     2

                break;
       ....


So das geht jetzt so weiter bis zu der Zahl 114.

Vielen Dank im voraus.
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: Fr 28.06.13 08:43 
Passiert in jedem case Zweig was prinzipiel Unterschiedliches? Und was?
Die Anzahl Einträge ist so fix das man die 114 hart im Code festlegen könnte?
baumina
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 305
Erhaltene Danke: 61

Win 7
Delphi 10.2 Tokyo Enterprise
BeitragVerfasst: Fr 28.06.13 08:46 
Die Frage ist, ob denn bei den 114 verschiedenen Auswahlmöglichkeiten jedesmal etwas total unterschiedliches passieren soll, oder etwas Vergleichbares. Wenn es sich z.B. um Zahlenwerte handelt, mit denen du rechnen willst, kannst auf das Switch verzichten und direkt mit dem Wert rechnen.
27sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Fr 28.06.13 08:49 
Genau gesagt handelt es sich um Links die immer um eine Zahl erhöht werden:

bsp. : www.test.de/001 , www.test.de/002 .. und so weiter.

Also bei jedem auswälen einzelnen Werte in der Listbox sollen die Seiten geöffnet werden.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4805
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 28.06.13 09:02 
Hallo 27sharp,

meinst du einfach
ausblenden C#-Quelltext
1:
2:
int index = listBox1.SelectedIndex + 1;
string url = String.Format("www.test.de/{0}", index.ToString("N3"));

? So kannst du auf das ganze switch-case Konstrukt verzichten.
baumina
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 305
Erhaltene Danke: 61

Win 7
Delphi 10.2 Tokyo Enterprise
BeitragVerfasst: Fr 28.06.13 09:02 
Ich kann kein .NET, aber es müsste ungefähr so gehen:
ausblenden Quelltext
1:
Str = 'www.test.de/' + Listbox1.Text					


Moderiert von user profile iconTh69: Code-Tags hinzugefügt
27sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Fr 28.06.13 09:14 
Also irgendwie funktioniert das nicht.
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: Fr 28.06.13 09:19 
Und wie funktioniert es irgendwie nicht? Ohne Details ist dir kaum zu helfen.
27sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Fr 28.06.13 09:21 
Also das ist der Alte Code :

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {



            int index = listBox1.SelectedIndex;

            switch (index)
            {
                case 0 :
                    axWindowsMediaPlayer1.URL = "www.test.de/001";
                    break;
                case 1 :
                    axWindowsMediaPlayer1.URL = "www.test.de/002";
                    break;
                    //..
                    //..
                    //..
                case 114 :
                    axWindowsMediaPlayer1.URL = "www.test.de/114";
                    break;
            }


Ich habe es gemacht wie Sie es mir gesagt haben doch dann wird die Seite garnicht aufgerufen.
baumina
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 305
Erhaltene Danke: 61

Win 7
Delphi 10.2 Tokyo Enterprise
BeitragVerfasst: Fr 28.06.13 09:29 
Du zeigst uns jetzt aber deinen neuen Code schon noch?
27sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Fr 28.06.13 09:40 
Sry , der neue Code :

ausblenden C#-Quelltext
1:
2:
3:
4:
int index = listBox1.SelectedIndex + 1;
string url = String.Format("http://test.de/{0}", index.ToString("N3"));

axWindowsMediaPlayer1.URL = url;


Moderiert von user profile iconTh69: Beitragsformatierung überarbeitet.
27sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Fr 28.06.13 09:42 
Ok , Problem gelöst:

Es Sollte so sein :
ausblenden C#-Quelltext
1:
2:
3:
4:
int index = listBox1.SelectedIndex + 1;
string url = String.Format("http://test.de/{0}", index.ToString("D3"));

axWindowsMediaPlayer1.URL = url;


Moderiert von user profile iconTh69: Beitragsformatierung überarbeitet.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4805
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 28.06.13 10:47 
Sorry, stimmt: "D3" statt "N3" (da bei N die Zahl die Anzahl der Nachkommastellen angibt).

Um ganz sicher zu gehen, sollte man die CultureInfo auf Invariant setzen:
ausblenden C#-Quelltext
1:
2:
int index = listBox1.SelectedIndex + 1;
string url = String.Format("http://test.de/{0}", index.ToString("D3", CultureInfo.InvariantCulture));

(Nicht damit einem eine Ländereinstellung dazwischenfunkt, wobei "D" laut Der Dezimalformatbezeichner "D" aber die Ländereinstellung ignorieren sollte.)

P.S. Und bitte demnächst in Beiträgen nicht mehr plenken.