Knowledge Base Nr: 00084 firstCsharp.txt - http://www.swe-kaiser.de

Downloads:

.NET:mein erstes C# programm - ich bin begeistert :-)

  
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyFirstCSharp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.PrintDialog printDialog1;
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckedListBox checkedListBox1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.Button button5;
private WindowsControlLibrary1.MyUserControl myUserControl1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.printDialog1 = new System.Windows.Forms.PrintDialog();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.button4 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.button5 = new System.Windows.Forms.Button();
this.myUserControl1 = new WindowsControlLibrary1.MyUserControl();
this.SuspendLayout();
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.label1.BackColor = System.Drawing.SystemColors.HotTrack;
this.label1.ForeColor = System.Drawing.SystemColors.Window;
this.label1.Location = new System.Drawing.Point(168, 23);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(95, 45);
this.label1.TabIndex = 0;
this.label1.Text = "das ist mein text";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(175, 96);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(69, 22);
this.button1.TabIndex = 1;
this.button1.Text = "check box";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(72, 96);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(95, 23);
this.checkBox1.TabIndex = 2;
this.checkBox1.Text = "checkBox1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(72, 40);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "rot";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.button3.Location = new System.Drawing.Point(288, 40);
this.button3.Name = "button3";
this.button3.TabIndex = 4;
this.button3.Text = "blau";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// printDialog1
//
this.printDialog1.Document = this.printDocument1;
//
// printDocument1
//
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//
// button4
//
this.button4.Location = new System.Drawing.Point(176, 128);
this.button4.Name = "button4";
this.button4.TabIndex = 5;
this.button4.Text = "drucken";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// textBox1
//
this.textBox1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
this.textBox1.Location = new System.Drawing.Point(184, 184);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(152, 20);
this.textBox1.TabIndex = 7;
this.textBox1.Text = "fokuswechsel anzeigen";
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
//
// checkedListBox1
//
this.checkedListBox1.Anchor = System.Windows.Forms.AnchorStyles.None;
this.checkedListBox1.Items.AddRange(new object[] {
"das",
"ist der test",
"der",
"checkedkistbox",
"-",
"mal sehn ",
"wies",
"schaut",
":-)"});
this.checkedListBox1.Location = new System.Drawing.Point(24, 184);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(136, 124);
this.checkedListBox1.TabIndex = 8;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem3,
this.menuItem2});
this.menuItem1.Text = "file";
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5,
this.menuItem6});
this.menuItem4.Text = "farbe textbox";
//
// menuItem5
//
this.menuItem5.Index = 0;
this.menuItem5.Text = "blue";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.Text = "red";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "&save";
//
// menuItem2
//
this.menuItem2.Index = 2;
this.menuItem2.Text = "e&xit";
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem7,
this.menuItem8});
//
// menuItem7
//
this.menuItem7.Index = 0;
this.menuItem7.Text = "red";
this.menuItem7.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem8
//
this.menuItem8.Index = 1;
this.menuItem8.Text = "blue";
this.menuItem8.Click += new System.EventHandler(this.menuItem5_Click);
//
// button5
//
this.button5.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
this.button5.Location = new System.Drawing.Point(272, 216);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(120, 23);
this.button5.TabIndex = 9;
this.button5.Text = "abfrage usercontrol";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// myUserControl1
//
this.myUserControl1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
this.myUserControl1.Auswahl = WindowsControlLibrary1.MyUserControl.MyEnum.zwei;
this.myUserControl1.Location = new System.Drawing.Point(192, 216);
this.myUserControl1.Name = "myUserControl1";
this.myUserControl1.Size = new System.Drawing.Size(64, 88);
this.myUserControl1.TabIndex = 10;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(401, 318);
this.ContextMenu = this.contextMenu1;
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.myUserControl1,
this.button5,
this.checkedListBox1,
this.textBox1,
this.button4,
this.button3,
this.button2,
this.checkBox1,
this.button1,
this.label1});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
if (!checkBox1.Checked)
label1.Text = "nicht gechecked!";
else
label1.Text = "die checkbox ist im moment gechecked!";
}

private void button2_Click(object sender, System.EventArgs e)
{
label1.BackColor = Color.Red;
}

private void button3_Click(object sender, System.EventArgs e)
{
Color blau = Color.Blue;

label1.BackColor = blau;
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

}

private void button4_Click(object sender, System.EventArgs e)
{
printDialog1.ShowDialog();
}

private void textBox1_Enter(object sender, System.EventArgs e)
{
textBox1.Text = e.ToString();
textBox1.BackColor = Color.Red;
}

private void textBox1_Leave(object sender, System.EventArgs e)
{
textBox1.Text = e.ToString();
textBox1.BackColor = System.Drawing.SystemColors.ControlLightLight;
}

private void menuItem6_Click(object sender, System.EventArgs e)
{
textBox1.BackColor = Color.Red;
}

private void menuItem5_Click(object sender, System.EventArgs e)
{
textBox1.BackColor = Color.Blue;
}

private void button5_Click(object sender, System.EventArgs e)
{
textBox1.Text = myUserControl1.Auswahl.ToString();
}

private void label1_Click(object sender, System.EventArgs e)
{

}
}
}