Random rand = new Random(); int zufallszahl = rand.Next(1, 11) //berechnet eine Zufallszahl zwischen 1 und 10.
try { //Hier steht ein Code, wo es zu Fehlern kommen kann } catch(Exception ex) { MessageBox.Show("Fehler bei der Eingabe. Überprüfen Sie Ihre Eingabe.\n" + ex.Message,"Fehler"); }
Man möchte z.B. dass in einer Textbox nur Zahlen eingegeben werden können…
Man verwendet das Ereignis OnKeyPress
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e) { Console.WriteLine((int)e.KeyChar); if(char.IsDigit(e.KeyChar)||(int)e.KeyChar==8||e.KeyChar=='-'||e.KeyChar==',') { //MessageBox.Show("Ist Ziffer"); if(e.KeyChar=='-' && tb_op1.TextLength!=0) //Verhindert ein Minus, das nicht an 1. Stelle ist. { e.Handled = true; } if(e.KeyChar==',' &&tb_op1.Text.Contains(',')) //Verhindert zwei Kommas. { e.Handled = true; } } else { //MessageBox.Show("Keine Ziffer"); e.Handled = true; // } }
private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.ShowDialog(); //Öffnet Fenster exklusiv }
private void button1_Click(object sender, EventArgs e) { ActiveForm.Close(); }
public partial class Form1 : Form { public int zahl = 5; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //this.Hide(); Form2 form2 = new Form2(this); form2.ShowDialog(); //this.Show(); } }
public partial class Form2 : Form { Form1 form1; int z; public Form2() { InitializeComponent(); } public Form2(Form1 incomingform) { InitializeComponent(); form1 = incomingform; z = form1.zahl; } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Die int-Variable aus Form 1 hat als Wert: " + z.ToString()); } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Das Label wurde im Form 1 auf public gesetzt!"); //Form1.Designer.cs //public System.Windows.Forms.Label label1; //private auf public geändert label1.Text=form1.label1.Text; } private void button3_Click(object sender, EventArgs e) { ActiveForm.Close(); } }
Console.WriteLine("Spalte " + e.ColumnIndex);
chart1.Titles.Add(textBox1.Text.ToString()); chart1.Titles.Clear();
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Area;
chart1.ChartAreas[0].AxisX.Title="X-Achse"; chart1.ChartAreas[0].AxisY.Title = "Y-Achse";
//Balkenumrandung chart1.Series[0].BorderColor = Color.Aquamarine; //Balkenfarbe chart1.Series[0].Color= Color.DarkOliveGreen;
Ein Dictionary verlangt einen Key und einen Value, z.B. der Key ein string und der Value ein integer. Man kann z.B. eingeben:
| Dictionary | dict | <string> | <int> |
| rot | 10 | ||
| schwarz | 15 | ||
| grün | 3 |
Es ist so ähnlich wie ein Assoziatives Array in PHP.
Dictionary<string,int> dict = new Dictionary<string,int> (); //Wenn der Value "rot" schon existiert, dann wird der Wert um 1 erhöht, ansonst neu erstell. if(dict.ContainsKey("rot")) { dict.["rot"]++; } else { dict.Add("rot",1); } MessageBox.Show(dict["rot"].ToString());
Dictionary<string,int> dred = new Dictionary<int,int> (); Dictionary<string,int> dblack = new Dictionary<int,int> (); Dictionary<string,int> dgreen = new Dictionary<int,int> (); //Beispiel für Gruen if (columns[1].Length > 0) //Es ist ein grüner Eintrag in der Zeile { if(rrot !=0) //ob vorher rot vorgekommen ist --> Run in Dictionary eintragen { if(dred.ContainsKey(rrot)) //Gibts schon den Eintrag für den rrot-Run im Dictionary? { dred[rrot]++; //Anzahl rrot-Runs für Rot runs++; //Anzahl Gesamt Runs } else //Wenn nicht, dann wird der Eintrag für rrot-Run erstellt { dred.Add(rrot,1); runs++; } rrot = 0; //der rrot-Run wird auf 0 gesetzt, da ja grün gekommen ist. if(rschwarz !=0) //war vorher schwarz? { if(dred.ContainsKey(rschwarz)) //gibt es den rschwarz-Run schon im Dictionary? wenn ja, erhöhen, { dblack[rschwarz]++; //Anzahl rschwarz-Runs für Schwarz runs++; //Anzahl Gesamt Runs } else //wenn nein, dann erstelle ihn mit 1, da dieser Run das erste Mal vorgekommen ist. { dblack.Add(rschwarz,1); runs++; } rschwarz= 0; }