传智播客C - Winform笔记-苏坤

更新时间:2024-01-12 05:14:02 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

WinForms:

一个窗体其实就是一个类

partial 关键字,部分类,允许我们把一个类放在多个文件中 Application.Run(运行应用程序要启动的窗体对象)

Button控件: 属性:

BackColor:背景颜色

BackgroundImage:设置背景图片

BacklgroundImageLayout:背景图片的显示样式 Cursor:鼠标进入的现实样式 Enabled:该对象是否可用 Font:字体样式

Location: 控件的位置 Size:大小 Text:文本

Visible:是否可见

按钮事件: Click

MouseEnter:鼠标进入触发一次 MouseLeave:鼠标离开触发一次

MouseMove:在鼠标在控件上移动时不断的触发

Label控件:用于向用户显示信息 属性:

Text:向用户展示的信息

窗体: 属性:

Icon:窗体左上角的图标

FormBorderStyle: 窗体的边线样式 MaximizeBox: 最大化按钮是否可用 MinimizeBox: 最小化按钮是否可用 Opacity: 透明度 0-1

ShowInTaskbar: 是否在任务栏显示 StartPosition:启动程序时显示的位置 Text:标题栏中的文本 TopMost: 保持在最前端

WindowState: 指示窗体处于最大化最小化还是正常化

事件:

Load(默认):窗体在显示之前最后一个被触发的事件,所以一般在这个事件中对窗体上的控件进行初始化赋值

Activated: 窗体获得焦点时触发 Deactivated:窗体失去焦点时触发 默认确认按钮:AcceptButton 默认取消按钮:CancelButton

注意:窗体的enter和leave事件被取消,请不要使用 FormClosing: 窗体关闭前触发的事件 方法: Close()

Show() 以非模态形式显示窗体,可以不停打开窗体,程序不会停到这里 ShowDialog() 以模态的形式显示窗体,只能打开一个窗体,程序会停到这里

System.Diagnostics.Process.Start(\

TextBox:文本框 属性:

MaxLength:用户能够输入的最大长度 MultiLine:是否可以多行 PasswordChar: 掩码

ReadOnly: 文本框内容只读

SelectionStart 如果是选中一段文本,则为起始位置,如果没有选中,则指示光标位置。 注意:第一个位置为0

事件:

Enter: 获得焦点时触发 Leave:失去焦点时触发

TextChanged:当文本框的内容修改时触发该事件 KeryPress: 方法:

Focus();获得焦点 Select() SelectAll()

PictureBox: 属性:

Image: 设置显示的图片 SizeMode:显示的样式

设置Tab键顺序: 视图-> tab键顺序

TabStop: 是否允许该控件获得焦点

1. 窗体运行时要屏幕居中 StartPosition

2. 不允许最大化

3. 不允许用户拖动窗体边框调整窗体大小 FormBolderStyle 4. 按Tab键,光标由上到下依次移动 5. 异常要捕捉 6. Return退出方法

7. 能够给用户指出是哪个文本框的数据有问题

8. 当出错时,出错的文本框自动获得焦点,并选中以方便用户修改 9. 利用TextChanged()事件,当用户修改加数时,清空计算结果 10. 无论光标在哪里,按回车都会计算

11. 只允许用户输入数字,其他字符输入不进去 12. 输入数字时,第一个字符不能是0

每个窗体都有一个默认确认按钮(按回车时,就相当于点击了这个按钮) 每个窗体都有一个默认取消按钮(按ESC时,就相当于点击了这个按钮)

在计算机中,存储字符其实就是存储他的ascii码值

object sender中存的就是触发控件的对象

校验数字

校验身份证:

校验18位身份证

System.Threading.Thread.Sleep(500); txtText.Update();

Stopwatch watch = newStopwatch(); watch.Start(); watch.Stop();

MessageBox.Show(string.Format(watch.ElapsedTicks.ToString()));

DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss”):时分秒自动加0 //y表示年,M月,d表示日,HH表示24小时 m分钟,是秒

应用程序的目录:Application.StartupPath+”\\\\mm\\\\”

This.pictuerBox1.Image.FromFile(“路径”);

方法:展开所有的节点 节点的Remove 删除

CheckBox 复选框: 属性: Checked Text

RadioButton: 单选框只能选择一个 Checked

处于同一分组下的所有RadioButton只能选中一个

ProgressBar: 进度条当进行大量计算时,最好给用户显示一个进度条,让用户知道你的程序没有死掉,正在运行 属性:

Maximum:最大值指示进度条满时Value的值

Minimum: 最小值指示进度条一格都没有时value的值

Value:进度条当前处于的位置 Application.DoEvents();

其他控件:

DateTimePicker:日期选择控件 webBrowser1.Navigate(网址);

This.Activate(); Application.Exit();

IO操作:

与文件相关的类的命名空间 System.IO.File类;

GetFiles: 获得目录里的所有文件 GetDirectories:获得所有目录 CreateDirectory:创建目录 Copy 复制文件

Exists 文件是否存在 Delete:删除目录、文件

OpenFileDialog\\SaveFileDialog: ShowDialog:显示对话框的方法

Fileter: 过滤器openFileDialog1.Filter = \图片|*.ico|all|*.*\

Filename: 用户选中的文件名及路径

复制单个文件:

复制目录文件

读一个文本文件:

1) 声明一个文件流对象 FileStream,用于创建内存与文件的对应关系

2) 创建读写器 StreamReader,用于操作文件流FileStream,把文件File读取进来 3) 读取操作 EndOfStream:指示是否独到末尾 ReadLine(),ReadToEnd() 4) 关闭读取器

5) 关闭文件流对象,释放对所操作文件的锁定

写一个文件步骤:

1) 声明一个文件流对象

2) 创建读写器 StreamWriter

3) 写入操作Write方法和WriteLine()方法 4) 关闭写入器

5) 关闭文件流对象

当不关闭的时候,有时候写入,有时候写入部分 Fs缓冲区满了以后或Close才会从内存写入文件

记事本程序: using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms; using System.IO;

namespace TEST8 {

public partial class Form1 : Form {

bool isTextChanged = false; string textFileName = \

public Form1() {

InitializeComponent(); }

private void newNToolStripMenuItem_Click(object sender, EventArgs e) {

txtText.Clear();

isTextChanged = false; textFileName = \ }

private void openOToolStripMenuItem_Click(object sender, EventArgs e) {

OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = \打开文件\

ofd.Filter = \文本文件|*.txt|所有文件|*.*\ if (ofd.ShowDialog() == DialogResult.OK) {

string[] strs = File.ReadAllLines(ofd.FileName); FileStream fs = FileStream(ofd.FileName,FileMode.Open,FileAccess.Read,FileShare.None); StreamReader sr = new StreamReader(fs,Encoding.Default); while (!sr.EndOfStream) {

string line = sr.ReadLine(); txtText.Text += line + \ }

//txtText.Text = sr.ReadToEnd(); sr.Close(); fs.Close();

textFileName = ofd.FileName; }

isTextChanged = false;

}

new

private void saveSToolStripMenuItem_Click(object sender, EventArgs e) {

SaveText(); }

private void txtText_TextChanged(object sender, EventArgs e) {

isTextChanged = true; }

private void exitEToolStripMenuItem_Click(object sender, EventArgs e) {

this.Close(); }

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {

if (isTextChanged == true) {

DialogResult dr= MessageBox.Show(\文本内容已经改变,是否保存\询问\ if (dr == DialogResult.Yes) {

SaveText(); }

else if(dr==DialogResult.No) { } else {

e.Cancel = true; } }

int x = this.Location.X; int y = this.Location.Y; int w = this.Size.Width; int h = this.Size.Height;

//规定一个配置文件 app.dll

StreamWriter sw= new StreamWriter(Application.StartupPath + \false);

sw.WriteLine(x.ToString());

sw.WriteLine(y.ToString()); sw.WriteLine(w.ToString()); sw.WriteLine(h.ToString());

sw.WriteLine(txtText.ForeColor.R); sw.WriteLine(txtText.ForeColor.G); sw.WriteLine(txtText.ForeColor.B); sw.Close(); }

private void SaveText() {

if (textFileName == \ {

SaveFileDialog sfd = new SaveFileDialog();

sfd.Title = \保存文件\

sfd.Filter = \文本文件|*.txt|所有文件|*.*\ if (sfd.ShowDialog() == DialogResult.OK) {

FileStream fs = new FileStream(sfd.FileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.Write(txtText.Text); sw.Close(); fs.Close();

isTextChanged = false;

textFileName = sfd.FileName; } } else {

FileStream fs = new FileStream(textFileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.Write(txtText.Text); sw.Close(); fs.Close();

isTextChanged = false; } }

private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) {

txtText.Copy(); }

private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)

{

txtText.Cut(); }

private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e) {

txtText.Paste(); }

private void 查找ToolStripMenuItem_Click(object sender, EventArgs e) {

int currentPos = txtText.SelectionStart;

int findPos = txtText.Text.IndexOf(\ if (findPos != -1) {

txtText.Select(findPos, 2); }

}

private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) {

FontDialog fd = new FontDialog(); fd.Font = txtText.Font;

if (fd.ShowDialog() == DialogResult.OK) {

txtText.Font= fd.Font; } }

private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e) {

ColorDialog cd= new ColorDialog(); cd.Color=txtText.ForeColor;

if (cd.ShowDialog() == DialogResult.OK) {

txtText.ForeColor = cd.Color; } }

private void 背景颜色ToolStripMenuItem_Click(object sender, EventArgs e) {

ColorDialog cd = new ColorDialog(); cd.Color = txtText.BackColor;

if (cd.ShowDialog() == DialogResult.OK) {

txtText.BackColor = cd.Color; } }

private void Form1_Load(object sender, EventArgs e) {

string appPath = Application.StartupPath + \

} } }

if (File.Exists(appPath)) {

StreamReader sr = new StreamReader(appPath); int x =Convert.ToInt32( sr.ReadLine()); int y = Convert.ToInt32(sr.ReadLine()); int w = Convert.ToInt32(sr.ReadLine()); int h = Convert.ToInt32(sr.ReadLine()); int r = Convert.ToInt32(sr.ReadLine()); int g = Convert.ToInt32(sr.ReadLine()); int b = Convert.ToInt32(sr.ReadLine()); this.Location = new Point(x, y); this.Size = new Size(w, h);

txtText.ForeColor = Color.FromArgb(r, g, b); sr.Close(); }

本文来源:https://www.bwwdw.com/article/tzlo.html

Top