C#作业样本⑻-- GUI Ⅳ

0 views
Skip to first unread message

bluefin

unread,
Jun 6, 2008, 4:44:47 PM6/6/08
to C# Guide
(注:以下教程适合初学者,介绍C#。下载.NET Framework 2.0 版本到你的Windows
2000 SP4 或者Windows XP SP2。)

①打开notepad.exe

②输入以下内容:

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

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.OpenFileDialog openFileDialog2;
private System.ComponentModel.IContainer components;

public Form1()
{
this.components = new System.ComponentModel.Container();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.listBox2 = new System.Windows.Forms.ListBox();
this.mainMenu1 = new
System.Windows.Forms.MainMenu(this.components);
this.textBox1 = new System.Windows.Forms.TextBox();
this.openFileDialog2 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "Exit";
this.menuItem4.Click += new
System.EventHandler(this.menuItem4_Click);
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[]
{
this.menuItem2,
this.menuItem4});
this.menuItem1.Text = "File";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Open";
this.menuItem2.Click += new
System.EventHandler(this.menuItem2_Click);
//
// openFileDialog1
//
this.openFileDialog1.Title = "Please Enter Text File";
this.openFileDialog1.FileOk += new
System.ComponentModel.CancelEventHandler(
this.openFileDialog1_FileOk);
//
// listBox2
//
this.listBox2.ItemHeight = 18;
this.listBox2.Location = new System.Drawing.Point(12, 73);
this.listBox2.Name = "listBox2";
this.listBox2.ScrollAlwaysVisible = true;
this.listBox2.Size = new System.Drawing.Size(364, 202);
this.listBox2.TabIndex = 3;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[]
{
this.menuItem1});
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars =
System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(364, 45);
this.textBox1.TabIndex = 0;
this.textBox1.Visible = false;
this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged)
;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(7, 19);
this.ClientSize = new System.Drawing.Size(390, 278);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.textBox1);
this.ForeColor = System.Drawing.SystemColors.ControlText;
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "FileDialog";
this.TransparencyKey = System.Drawing.Color.Yellow;
this.ResumeLayout(false);
this.PerformLayout();
}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
Text = "open file clicked";
openFileDialog1.ShowDialog();
}

private void openFileDialog1_FileOk(object sender,
System.ComponentModel.CancelEventArgs
e)
{ // 这段很重要
textBox1.Text = openFileDialog1.FileName;
textBox1.Visible = true;

StreamReader strmInput = new
StreamReader(openFileDialog1.FileName );
string item;
while (( item = strmInput.ReadLine() ) != null )
listBox2.Items.Add( item );
strmInput.Close();

listBox2.Invalidate();
listBox2.SelectedIndex = -1;
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
Text = "Open";
openFileDialog1.ShowDialog();
}

private void menuItem4_Click(object sender, System.EventArgs e)
{
DialogResult dr =
MessageBox.Show( "Are you sure you want to quit, " + textBox1.Text
+
"?",
"Exit",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question );

if ( dr == DialogResult.OK )
Close();
}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{ ;; }

}
}


③储存名档为FileDialog.cs

④设计一个Image 名为App.ico,根据以下Property value:
Width/Height = 32/32 pixels
Resolution (Horz/Vert) = 96/96 dpi
Bith depth= 32
Frame Count= 1

⑤进入"C:\Windows\Microsoft.NET\Framework\v2.0.50727" 里头,执行csc.exe
如下:

csc.exe /t:winexe /win32icon:App.ico /nologo FileDialog.cs [ 回车键
]

这时它再也不会显示以下讯息了:

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

⑥输入dir FileDialog.*

这时它会显示以下讯息:

FileDialog.cs
FileDialog.exe

注:若你见不到FileDialog.exe, 你一定要先除错FileDialog.cs,一直得到为止。


⑥输入FileDialog.exe

路途多艰,唯勤是岸。加油、加油、再加油!
Reply all
Reply to author
Forward
0 new messages