Home   Cover Cover Cover Cover
 

Windows.Forms

Achtung, zu allen Beispielen muss beim übersetzen das Assembly System.Windows.Forms.dll angegeben werden:
also: csc /r:System.Windows.Forms.dll ExampleX.cs

Beispiel "Datei-Öffnen-Dialog"

In diesem Beispiel wird ein vordefiniertes Dialogfester angezeigt, mit dem man eine Datei auf einem Datenträger öffnen kann.

../../../samples/4/WindowsForms/DialogExample.cs
using System;
using System.Windows.Forms;
using System.IO;

namespace Kapitel4.Windows.Forms {

  public class DialogExample {
    public static void Main(string[] argv) {
      OpenFileDialog openDialog = new OpenFileDialog();
      openDialog.InitialDirectory = "c:\\";
      openDialog.Filter = "Alle Dateien|*.*";  // "Filtername | Filtermuster"
      if (openDialog.ShowDialog() == DialogResult.OK){
        Stream file = openDialog.OpenFile();
        if (file != null) {
          // die ausgew�hlte Datei wurde f�r die Bearbeitung ge�ffnet
          file.Close ();
        }
      }
    }
  }
}




Beispiel "HelloWorldForm"

In diesem Beispiel wird von der Klasse Windows.Form abgeleitet und ein Label mit der Aufschrift "HelloWorld" ausgegeben.

../../../samples/4/WindowsForms/HelloWorldFormExample.cs
using System;
using System.Windows.Forms;
using System.Drawing;

namespace Kapitel4.Windows.Forms {

  class HelloWorldForm : Form {
    Label lab;
    
    HelloWorldForm() {
      this.Text = "Hello World Fenster";        // Fenstertitel
      this.Size = new Size (200,100);        // Fenstergr��e
      lab = new Label();         // Label realisiert ein statisches Textfeld
      lab.Text = "Hello World";
      lab.Location = new Point(20, 20);        // Position des Textfelds im Fenster
      this.Controls.Add(lab);        // das Textfeld wird in das Fenster eingef�gt
    }

    public static void Main() {
      Application.Run(new HelloWorldForm()); // Fenster wird als Windows-App. gestartet
    }
  }
}




Beispiel "MouseEvent Color Panel"

In diesem Beispiel wird ein Form mit einem roten Panel erzeugt. Das Panel wechselt die Farbe auf grün wenn der Mauszeiger im Bereich des Panels ist.

../../../samples/4/WindowsForms/MouseEventColorExample.cs
using System;
using System.Windows.Forms;
using System.Drawing;

namespace Kapitel4.Windows.Forms {

  class MouseEventTest : Form {
    Panel p;

    MouseEventTest() {
      p = new Panel();
      p.BackColor = Color.Red;
      p.Size = new Size(100,100);
      p.Location = new Point(100,100); 
      p.MouseEnter+= new EventHandler(OnMouseEnter);
      p.MouseLeave+= new EventHandler(OnMouseLeave);
      this.Controls.Add(p);
    }
    
    public void OnMouseEnter (object sender, EventArgs args) {
      p.BackColor = Color.Green;
    }

    public void OnMouseLeave (object sender, EventArgs args) {
      p.BackColor = Color.Red;
    }

    public static void Main() {
      Application.Run(new MouseEventTest());
    }
  }
}




Beispiel "MouseEvent Position"

In diesem Beispiel wird ein Form mit einem Label erzeugt. Das Label zeigt die Position des Mauszeigers als Koordinaten an, wenn der MouseButton gedrückt wurde.

../../../samples/4/WindowsForms/MouseEventExample.cs
using System;
using System.Windows.Forms;
using System.Drawing;

namespace Kapitel4.Windows.Forms {

  class MouseEventExample : Form {
    Label lab;
  
    MouseEventExample (){
      this.Size = new Size(300,100);
      lab = new Label();
      lab.BackColor = Color.White;
      lab.Size = new Size(300,100);
      lab.Font = new Font(lab.Font.FontFamily,20.0f);
      lab.MouseDown+=new MouseEventHandler(OnMouseDown);
      this.Controls.Add (lab);
    }

    public void OnMouseDown(object sender, MouseEventArgs args) {
      lab.Text = "Button:" + args.Button + "\nx:" + args.X + " y:" + args.Y;
    }

    public static void Main() {
      Application.Run(new MouseEventExample());
    }
  }
}




Beispiel "BarChartControl Custom Control"

In diesem Beispiel wird ein Custom Control implementiert, das eine Menge von Werten als Balkendiagramm anzeigen kann.

../../../samples/4/WindowsForms/BarChartControlExample.cs
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

namespace Kapitel4.Windows.Forms {

  class BarChartControl : UserControl {
    ArrayList val;  // die Werte des Balkendiagramms
    ArrayList backup;      // Sicherungskopie der Werte
    bool sorted = false;   // ist die Darstellung sortiert oder unsortiert?
    Label label;

    public BarChartControl() {
      val = new ArrayList();
      this.BackColor = Color.Coral;
      label = new Label();
      label.Text = "sortiert";
      label.Location = new Point(1, this.Height-15);
      label.Click += new EventHandler(OnSortLabelClick);
      this.Controls.Add(label);
    }

    public int[] Values {
      get { return (int[]) val.ToArray(typeof(int)); }
      set { val = new ArrayList(value); }
    }

    protected void OnSortLabelClick(object sender, EventArgs args) {
      if (sorted) {
        val = new ArrayList(backup);
        label.Text = "sortiert";
      } else {
        backup = new ArrayList(val);
        val.Sort();
        label.Text = "unsortiert";
      }
      sorted = !sorted;
      this.Refresh();
    }

    protected override void OnPaint(PaintEventArgs e) {
      Graphics g = e.Graphics;
      Rectangle r = this.DisplayRectangle;
      int b = r.Width / (val.Count * 2+1); // Breite und Abstand der Balken
      int x = b;
      foreach (int i in val) {
        g.FillRectangle(new SolidBrush(this.ForeColor), x, r.Height - i - 20, b, i);
        x += b * 2;
      }
    }

    public static void Main() {
      Form f = new Form();
      BarChartControl chart = new BarChartControl();
      chart.Values = new int[] {30, 20, 50, 10, 15, 40, 100, 70, 80, 100};
      f.Controls.Add(chart);
      Application.Run(f);
    }
  }
}