Autor Beitrag
basti2005
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Mi 22.09.10 00:28 
Wie vergleiche ich einen Text aus einer Textbox ?

Und zwar so:

Wenn der Text in textbox1 = BlaBla ist dann ändere die
Farbe des Labels auf grün


Bei diesem Code

schreibt er immer Offline und wechselt zur Farbe Rot

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
                if ("textBox_host1" ==  "aa")
                    
            {             panel1.BackColor = System.Drawing.Color.Green;
                          label1.Text = "Online";
            }
                else
            {

                           panel1.BackColor = System.Drawing.Color.Red;
                              label1.Text = "Offline";
            }


Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19345
Erhaltene Danke: 1753

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 22.09.10 05:19 
Die beiden Strings sind ja auch unbestreitbar nicht gleich. ;-)

Was du vergleichen willst sind doch nicht die Zeichen "textBox_host1", sondern den Inhalt der Textbox. Den Inhalt eines Strings interpretiert C# aber nicht, es sei denn du sagst das C# (das ist hier aber nicht empfehlenswert). Deshalb musst du auf die Variable der Textbox zugreifen.

Da du auf den Text darin zugreifen willst, musst du das auch noch dazuschreiben:
ausblenden C#-Quelltext
1:
textBox_host1.Text					

Für diesen Beitrag haben gedankt: basti2005
basti2005 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Mi 22.09.10 16:54 
Wenn ich das ganze so mache also ohne Text in den "" dann schreibt er erst Online rein wenn ich zb ein Leerzeichen machen und dieses dann wieder lösche.Wie kann ich dieses problem lösen ?

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
             if (textBox_host1.Text ==  "")
                    
            {             panel1.BackColor = System.Drawing.Color.Green;
                          label1.Text = "Online";
            }
                else
            {

                           panel1.BackColor = System.Drawing.Color.Red;
                              label1.Text = "Offline";
            }


Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
bakachan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 503
Erhaltene Danke: 34

W7 (x64) Ultimate
C# / VB.NET (VS2010 Ultimate)
BeitragVerfasst: Mi 22.09.10 17:03 
Also was soll er machen?
Online in das Label schreiben wenn etwas in der Textbox steht? Dann solltest du deine bedingung negieren.
im moment steht da ja: wenn der Text leer ist -> online ansonsten -> offline

Für diesen Beitrag haben gedankt: basti2005
basti2005 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Mi 22.09.10 17:08 
Das weiß ich doch nur muss ich erst ein leerzeichen machen und dies wieder löschen bevor überhaupt "Online" da steht.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19345
Erhaltene Danke: 1753

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 22.09.10 17:13 
Wenn du das erst in dem Change Event der Textbox aufrufst, ist das klar. Das musst du schon vorher machen.

Für diesen Beitrag haben gedankt: basti2005
basti2005 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Mi 22.09.10 20:54 
Also muss ich den code auf die Form legen, oder ?
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: Mi 22.09.10 20:57 
Nein, nur vorweg von der Form aus einmal aufrufen unabhängig von dem genannten Event.

Für diesen Beitrag haben gedankt: basti2005
basti2005 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Mi 22.09.10 21:04 
Verwirrung ^^
schwandi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 21
Erhaltene Danke: 3



BeitragVerfasst: Do 23.09.10 00:15 
Rufe es nicht nur im Change Event auf sondern zb. auch im "Enter-Event" Dies wird aufgerufen sobald du in die TextBox klickst.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
private void textBox_host1_Enter(object sender, EventArgs e)
{
  if (textBox_host1.Text ==  "")
                    
            {             panel1.BackColor = System.Drawing.Color.Green;
                          label1.Text = "Online";
            }
                else
            {

                           panel1.BackColor = System.Drawing.Color.Red;
                              label1.Text = "Offline";
            }

}

Für diesen Beitrag haben gedankt: basti2005
basti2005 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Do 23.09.10 01:02 
Also irgendwie will das nicht so klappen !

Habe den Code von dir jetzt ganz unten vor den letzten beiden "}" eingebunden und bei einem klick in das Feld Passiert nichts.
schwandi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 21
Erhaltene Danke: 3



BeitragVerfasst: Do 23.09.10 01:16 
Füge den Code nicht einfach ein sondern, gehe in die Form1 ansicht, so das du das TExtfeld markieren kannst, dann suchst du dir rechts das Event "Enter" raus und machst doppelklick drauf.

Dann kommst du automatisch zum erstellten event, dort kopierst du dann deinen code rein!

Poste mal deinen code wenns so nicht geht

Für diesen Beitrag haben gedankt: basti2005
basti2005 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Do 23.09.10 01:29 
Klappt soweit Danke