Entwickler-Ecke

Basistechnologien - Ändern einer variable per button


DeStein - So 24.11.13 04:17
Titel: Ändern einer variable per button
Ich habe einen zähler der immer plus 1 macht und möchte dass man per button das änder und es plus 2 macht

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ErhöheUmEins();
            GebeAus();
        }
        private int Zähler = 0;
        
        
        public void ErhöheUmEins()
        {
            Zähler += 1;
        }
        public void GebeAus()
        {

            label1.Text = Zähler.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
             if (Zähler > 0)
    {
        ErhöheUmEins2();
    }
            GebeAus2();
        }
        private int Zähler2 = 1;

        public void ErhöheUmEins2()
        {
            Zähler2 += 4;
            Zähler -= 1;
        }
        public void GebeAus2()
        {

            label2.Text = Zähler2.ToString();
            label1.Text = Zähler.ToString();
        }

        

        public void button3_Click(object sender, EventArgs e)
        {
            if (label2.Text = 100)
            {

            }
        }
    
    
        }
    }


Christian S. - So 24.11.13 12:23

Hallo!

Du musst zusätzlich zu zähler ein zweites privates Feld anlegen, indem Du die Schrittweite speicherst. Den Inhalt des Feldes (also zum Beipsiel 1 oder 2) kannst Du dann mit einem Button ändern. Und zu zähler addierst Du dann nicht 1 sondern schrittweite.

Grüße
Christian


DeStein - So 24.11.13 12:57

Ich habe noch eine frage ganz unten bei Button 3 ist eine If Anwendung ich möchte wenn in Lable 2 100 steht das es funktioniert aber dazu muss ich string in int umwandeln wie geht das?


jaenicke - So 24.11.13 13:34

Dafür gibt es Parse bzw. TryParse:
http://msdn.microsoft.com/de-de/library/system.int32.tryparse.aspx

Allerdings ist diese Umwandlung gar nicht nötig, denn du selbst schreibst ja in den Text des Labels, du weißt also was drin steht.


Christian S. - So 24.11.13 13:35

Hallo!

Vorweg: Bitte benutze Satzzeichen. Zumindest ein Punkt am Satzende ist nicht zu viel verlangt und macht das Lesen direkt viel leichter. Kommas sind auch gerne gesehen ;-)


Den Rest hat jaenicke jetzt schon geschrieben, also kann ich mir das sparen :D

Grüße
Christian


DeStein - So 24.11.13 14:00

Wenn ich das so mache,achtet es gar nicht darauf und gibt auch den fehler aus das int nicht in string konvertiert werden kann.Es geht also auch wenn da 0 steht.

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ErhöheUmEins();
            GebeAus();
        }
        private int Zähler = 0;
        private int schrittweite = 1;

        public void schritte()
        {
            schrittweite *= 2;
        }
        
        
        public void ErhöheUmEins()
        {
            Zähler += schrittweite;
        }
        public void GebeAus()
        {

            label1.Text = Zähler.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
             if (Zähler > 0)
    {
        ErhöheUmEins2();
    }
            GebeAus2();
        }
        private int Zähler2 = 1;
        private int schrittweite2 = 4;
        private int schrittweite3 = 1;

        public void schritte2()
        {
            schrittweite2 *= 2;
            schrittweite3 *= 2;
        }


        public void ErhöheUmEins2()
        {
            Zähler2 += schrittweite2;
            Zähler -= schrittweite3;
        }
        public void GebeAus2()
        {

            label2.Text = Zähler2.ToString();
            label1.Text = Zähler.ToString();
        }

        

        public void button3_Click(object sender, EventArgs e)
        {
            if (label2.Text = 100)
            {
                schritte();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            schritte2();
        }
    
    
        }
    }


papa69 - So 24.11.13 14:51

ohne jetzt böse zu sein, aber mit

Zitat user profile iconDeStein

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
  

        public void button3_Click(object sender, EventArgs e)
        {
            if (label2.Text = 100)
            {
                schritte();
            }
        }

   
    }


weist du label2.text etwas zu, und vergleichst nicht !!! , ob es etwas "erreicht" hat...

Letztens hatte ich Dir schon mal 'nen Link geschickt! Lies Dir das mal richtig durch...dann verstehst du, was ich meine wegen ZUWEISEN und VERGLEICHEN

http://msdn.microsoft.com/de-de/library/5011f09h%28v=vs.90%29.aspx