C#控件基本1

第一大内容窗体:

一、控制标题栏

Text - 控制标题栏的文字

Icon - 控制标题栏的图标

MaximizeBox - 窗体最大化

MinimizeBox  -窗体最小化

ControlBox - 所有的最大化、最小化、关闭按钮

 

二、控制背景

BackColor - 控制背景色

BackgroundImage - 背景图

BackgroundImageLayout - 背景的布局方式

Tile-平铺;Center-居中;Stretch-拉伸;Zoom-按比例缩放

 

三、控制边框

FormBorderStyle - 控制边框的样式

Sizable-可调边框;FixedSingle-不可调边框;None-无边框。

Size (Width,Height):窗体的大小。

MinimumSize Width  Height)最小的大小

MaximumSize (Width Height) 最大的大小

 

四、控制前景

ForeColor - 文字颜色

Font-文字字体

 

五、辅助属性:

WindowState - 窗体的启动状态。

Normal-设计时的大小。Maximized-窗体启动最大化。Minimized-启动最小化。

StartPosition -窗体的启动位置。

CenterScreen - 在屏幕中央

ShowInTaskbar - 窗体运行时,是否在任务栏显示。

TopMost - 是否置于顶层显示

Opacity -- 透明度。100%完全不透明。0%完全透明。

TransparencyKey - 窗体的透明色。

 

案例一:做一个遮罩窗体:

1.把边框去掉。 FormBorderStyle = none;

2.设为启动最大化。 WindowState = Maximized

3.设背景色为黑色。 BackColor=black

4.设为顶层窗体 TopMost = true

5.设置不在任务栏显示。 ShowInTaskbar=false

 

 

技巧:添加一个新窗体并设为启动窗体:

1.在解决方案管理器的项目右击,选择添加-windows窗体。

2.在解决方案管理器中,把Program.cs打开,修改后面的Application.Run().

 

案例二:做一个不规则的窗体:

1.找一个不规则的透明底色的图片。

2.把窗体背景设为这张图片。

3.设置窗体的透明色TransparencyKey

4.把边框去掉。FormBorderStyle=none

 

第二大内容:三个最常用的控件:

标签:Label

Text - 标签文字

Image - 图像

TextAlign - 文字的位置9个位置

ImageAlign - 图像的位置9个位置

AutoSize - True-自动调整大小,只有一个控制点。False-手动调整大小,有8个控制点。

 

ForeColor——文字色

BackColor——背景色

Font——字体

 

 

按钮:Button

Text - 按钮文字

Image - 按钮图片

TextAlign - 文字的位置9个位置

ImageAlign - 图像的位置9个位置

FlatStyle- 按钮的形态。 Standard-标准。Flat-平面状态。Popup-稍微凸出一点。

FlatAppearence - 按钮形成的详细设置。

ForeColor——文字色

BackColor——背景色

Font——字体

Dock---按钮占据锁在空间的位置,fill--占满位置

(代码写的Button btn=new Button ();     btn.Dock = DockStyle.Fill;)

 

注:窗体中有两个很重要的属性: AcceptButton,CancelButton分别用来指定两个按钮。在窗体上按回车键会自动调用AcceptButton中的代码,按ESC键会调用CancelButton的代码。

 

 

 

 

 

 

 

文本框:TextBox

Text - 按钮文字

MultiLine-是否是多行文本框。true-多行文本,false-单行文本

PasswordChar-密码框中的字符样式

ReadOnly-只读,不能改

MaxLength-最多输入多少

ScrollBars----Both出现滚动条

Dock----上下左右中占满的方式

Anchor---上下左右,选中的方向的距离,将会不随边框的改变而改变

 

案例三:做一个登录窗体,并实现登录功能。

 

 

单选按钮+复选按钮

RadioButtonCheckBox

Text-单选按钮上的文字

Checked - 单选按钮是否被选中true/false

Tag - 一般是用保存单选按钮背后的值。

Image - 图像 TextAlgin,ImageAlign

Appearance:按钮外观。Normal-普通的单选按钮。Button-按钮形式的单选按钮

 

 

下拉列表combobox

       DropDownStyle - DropDown--既可以选,又可以填写。DropDownList--只能选 

       Items--编辑项(静态时使用,一般情况下都是用函数代码连接数据库里的表)

  使用代码放:

(1).使用代码逐项添加

a.造一个实体类 :重写父类的ToString()方法,返回要显示的属性。

b.使用实体造对象

c.把对象添加到下拉列表的items集合中去。

 

案例:把民族表中的数据读取出来加载到下拉列表中去。

a.准备实体类、链接类、数据访问类。

b.在界面后台代码中,调用数据访问类,获取返回来的民族列表数据。

c.遍历列表中的每一项,把它每个加进下拉列表的Items集合中。

 

 

(2).直接使用数据绑定

需要用到的属性:

DataSource - 下拉列表的数据来源,一般是实体类的集合。

DisplayMember - 要作为显示项的属性名。

ValueMember - 要作为值项的属性名。

案例:把民族表中的数据读取出来加载到下拉列表中去。

a.准备实体类、链接类、数据访问类。

b.调用数据访问类,把返回来的数据,赋给下拉列表的DataSource

c.设置下拉列表的DisplayMemberValueMember属性。

 

案例:如何为下拉列表中加上“请选择”的项

(1).使用代码逐项添加

只需在逐项添加的代码之前,加上一个“请选择”的项即可

(2).直接使用数据绑定

需要事选在数据源(即列表集合)中添加一个“请选择”的项。绑定即可上去。

 

2.如何把选中项获取出来。

相关属性:SelectedItem--选中的项

使用下拉列表的SelectedItem属性来获取,获取出来的类型是Object,需要强制转换成相应的类型。然后再获得某相应属性的值。

 

案例:获取选中的民族的名称和Code值出来。

 

 List<ChinaStates> list = new ChinaStatesDA().Select();

//填进去

            list.Insert(0,new ChinaStates(“-1”,”请选择”));

            Combobox1.DataSource = list;

            Combobox1.DisplayMember = "AreaName";

            Combobox1.ValueMember = "AreaCode";

第一项加“请选择”,此图比较全。此图为取表中数据放到combobox中,下拉列表

以此函数放到整个窗体的Load

 

  private void FillCounty()

        {

            string parent = "";

            if (cbCity.SelectedItem != null)//判断前面那一个combobox中的数据是否为空

            {

                //取数据

                parent = (cbCity.SelectedItem as ChinaStates).AreaCode;

                //因其需要前面的数据的id引导此combobox中的数据,所以去前一个数据的code

            }

 

            List<ChinaStates> list = new ChinaStatesDA().Select(parent);

            //填进去

            cbCounty.DataSource = list;

            cbCounty.DisplayMember = "AreaName";

            cbCounty.ValueMember = "AreaCode";

        }

 

ComboboxC#中用的主要函数:

 

SelectedItem:获取或设置当前combobox所选定的项(0,1,2,3....),即第几项

SelectedIndex:获取或设置指定当前选定项的索引

            (即当comboBox1.SelectedIndex为多少时,执行什么---相应的函数)

 

 

时间表控件 DateTimePicker

 

Value----时间值---显示的默认值

 

 

 

 

ListView

主要是用来显示的控件。

view有五种显示的视图LargeIcon大图标、SmallIcon小图标、Tile平铺、List列表、

                       Details详细(以列显示的情况,用Details

 

注意:

1.如果要使用Details视图,一定需要先添加列。

2.进行修改或删除的时候,一般需要把主键放在每个ListViewItemTag里。使用窗体的构造函数把主键传给目标窗体。

 

 

View 属性五种视图状态

SmallImageList,LargeImageList(组件里边的imagelist,设置两个,编辑大小和选择图像;然后在listview属性里选择SmallImageList,LargeImageList,挑选下拉项里边的大小imagelist

例一:  private void Show()//在listview中显示表中想要显示的数据

        {

           listView1.SelectedItems.Clear();

            //取数据

            List<Info> list = new InfoDA().Select();

            //加进去

            foreach (Info data in list)

            {

                ListViewItem li = new ListViewItem(data.Name);

                li.Tag = data.Code;

                li.ImageIndex = 0;//用第几个图像,因为imagelist里边可以放多个图像

                li.SubItems.Add(data.Sex ? "男" : "女");

                li.SubItems.Add( new NationDA().Select( data.Nation).Name );

                //根据Info表里的nation搜索Nation表里的Name,输出

                li.SubItems.Add(data.Birthday.ToString("yyyy年MM月dd日"));

                if (data.Sex == true)//呈现分组现象

                {

                    li.Group = listView1.Groups[0];

                }

                else

                {

                    li.Group = listView1.Groups[1];

                }

                listView1.Items.Add(li);//添加到listview中

            }

        }

例二:

   private void ShowAll()

        {

            listView1.SelectedItems.Clear();

            List<TeacherData> list = new TeacherDA().Select();

            ShowSelect(list);

        }

 

        private void ShowSelect(List<TeacherData> list)

        {

            listView1.Items.Clear();

            if (list == null)

                return;

            foreach (TeacherData da in list)

            {

                ListViewItem li = new ListViewItem(da.Tname);

                li.Text = da.Tname;

                li.SubItems.Add(da.Tsex);

                li.SubItems.Add(da.Tbirthday.ToString("yyyy年MM月dd日"));

                li.SubItems.Add(da.DepartmentData.Name);

                listView1.Items.Add(li);

 

            }

        }

 

 

 

 

 

 

FullRowSelect - 选择整行,鼠标点击

GridLines -  是否显示格式,网格式的显示方式

HoverSelection--鼠标移动上去就有颜色变化,表示被选中,易观看数据

HotTracking--鼠标选中后移开,有颜色变化(颜色变淡)--------------------

-----------------------------HoverSelectiontrue时方可使用

SelectedItems - 选中项的集合

    Tag - 项的值

ImageIndex - 项所使用的图片索引。——需要事先给ListView设置SmallImageList和                                                                                       LargemageList

 

Columns - 列的集合。

Items - 项的集合。

Groups- 组的集合---NameText

 

 

事件:SelectIndexChanged---------选中发生更改

 

 

传出主键值,给窗体二:

 

窗体一:

Button按钮--- 

private void button2_Click(object sender, EventArgs e)

        {

            if (listView1.SelectedItems.Count > 0)

            {

                string key = listView1.SelectedItems[0].Tag.ToString();

                Form2 f2 = new Form2(key);

                f2.Show();

            }

        }

 

窗体二:

private string _Key = "";

public Form2(string key)//构造函数

            : this()

        {

            _Key = key;//得到主键值,_Key可当成主键值来用

        }

 

Listview主要用到的C#函数:

SelectedItems:获取在控件中选定的项

Items:获取包含空间中所有项的集合

 

 

 

 

DataGridView显示网格中可以自定义数据的行和列(不全)

 

Anchor----定义边框距离窗体的边的距离

AutoSizeColumnsMode--确定可见列的自动调整大小模式----fill

 

 

 

 

 

 

对话框:

 

一、提示对话框:

DialogResult result = MessageBox.Show("提示文字","标题文字",按钮设置,图标设置)

if(result == 枚举)

{

}

 

二、其它对话框:

(一)ColorDialog

1、属性:

Color - 选中的颜色。

2、方法:

ShowDialog() - 把颜色对话框显示出来。返回一个DialogResult对象.

3、案例:

            DialogResult result = colorDialog1.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)

            {

                label1.ForeColor = colorDialog1.Color;

            }

(二)FontDialog

1.属性:

Font - 选中的字体

2.方法:

ShowDialog() - 把字体对话框显示出来。返回一个DialogResult对象

3.案例:

            DialogResult result = fontDialog1.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)

            {

                label1.Font = fontDialog1.Font;

            }

(三)OpenFileDialog,SaveFileDialog

1.属性:

FileName - 打开文件的全名(包括路径)

InitialDirectory - 初始路径。默认在“我的文档”

Filter - 打开过滤。显示名|通配名|显示名|通配名  如:文本文件|*.txt|C#源文件|*.cs|所有文件|*.*

2.方法:

ShowDialog() - 显示打开对话框,返回DialogResult

3.举例:

            DialogResult result = openFileDialog1.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)

            {

                label1.Text = openFileDialog1.FileName;

            }

(四)FolderBrowserDialog - 路径选择对话框

1.属性

SelectedPath - 选中的路径。

RootFolder - 默认打开的路径。

Description - 对话框中的提示信息

 

2.方法

ShowDialog()

3.举例

            DialogResult result = folderBrowserDialog1.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)

            {

                label1.Text = folderBrowserDialog1.SelectedPath;

            }

 

三、自定义对话框:

自己做个窗体,用来作为对话框显示出来。

第一大步:做对话框窗体

1.做一个窗体,把要实现的功能做出来。

2.放两个按钮,设置这两个按钮的DialogResult属性。

 

 

第二大步:调用对话框显示,并获取值。

1.把对话框窗体给new出来。

2.使用对话框窗体的ShowDialog()显示出来。返回DialogResult对象

3.根据返回的DialogResult,判断操作。

            TestDialog td = new TestDialog();

            DialogResult result = td.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)

            {

                label1.Text = td.Password;

            }

 

 

 

 

 

 

 

 

 

 

布局:

一、默认布局(空布局):

1.Location - 位置距离上方和左方的距离

2.Anchor - 固定边距上下左右的距离不变的设置,哪边被选中,那边距离边框的距离就不变

 

二、边界布局:

Dock - 上右下左中,靠在边框里边的相应位置

 

三、网格布局:

TableLayoutPanel控件,使用它来格式化行和列。右上方黑色小三角点击,编辑行和列

使用内部放置的控件的RowSpanColSpan来设置控件的跨行与跨列。

Column-----跨行

Row------跨列

Appearance-----指示选项卡是绘制成按钮还是绘制成常规选项卡

 

网格布局:(容器中)tableLayoutPanel

 

用代码编辑tableLayoutPanel的行和列(在整个窗体的load):

 

 tableLayoutPanel1.ColumnStyles.Clear();//清理原来的列

            tableLayoutPanel1.ColumnCount = 20;//设置新的列(这里的20指的是列数)

            for (int i = 0; i < 20; i++)//循环20次,编辑列的属性

            {

                ColumnStyle style = new ColumnStyle(SizeType.Percent, 5);

                //列的属性,括号中第一个数Percent为列按百分比实现宽,第二个数为这个宽占总体的5%

                tableLayoutPanel1.ColumnStyles.Add(style);//添加列

            }

            tableLayoutPanel1.RowStyles.Clear();//清理原来的行

            tableLayoutPanel1.RowCount = 20;//设置新的行(这里的20指的是行数)

            for (int i = 0; i < 20; i++)//循环20次,编辑行的属性

            {

                RowStyle style = new RowStyle(SizeType.Percent, 5);

                //列的属性,括号中第一个数Percent为行按百分比实现高,第二个数为这个高占总体的5%

                tableLayoutPanel1.RowStyles.Add(style);//添加行

            }

 

 

 

 

 

 

四、流式布局:

FlowLayoutPanel控件。

它有个重要的属性:FlowDirection - 流的方向(从左往右,从上往下,从右往左,从下往上)

 

五、卡片布局:

TabControl控件。

它里面包含多个TabPage,放在TabPages集合中。

Appearance------Buttons设置成按钮形式;FlatButtons设置成平面选项卡;

              Normal正常的图片显示形式

Enabled----true可用;false不可用

 

六、分割布局:

SplitContainer控件。把窗口横向或纵向拆分。

Orientation (选中整个SplitContainer方可出现)

                              - 分割的方向。Vertical纵向分割;Horizontal横向分割

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

托盘图标:

 

NotifyIcon - 做拖 盘图标的。

属性:

Text - 拖盘文字(即在下方的显示图标在鼠标移动上是现实的文字)

Icon - 拖盘图标(图像:ico格式)

ContextMenuStrip (属性)右击时候的菜单(即点击图标右键,出现选择项菜单)。

 

 

右键菜单。

ContextMenuStrip(容器)

1.学会编辑菜单项。  是分隔线。

2.如何设置热键  &字母

3.如何设快捷键。右击菜单项,在属性中选择ShortCutKey中的组合键即可

4.如何给菜单项加图标?

5.右击菜单项,选择属性,设置Image(菜单选择项前的图像)

 

把右键菜单挂到拖盘图标上。

右击NotifyIcon1,选择属性。设置ContextMenuStrip

 

工具条:(容器)ToolStrip

1.会加按钮并设置按钮的图像及文字

2.会调整按钮的图像大小。把ToolStripImaegScalingSize调整大。再把每个按钮的Size属性设大即可

3.会显示文字和图标。DisplayStyle,TextImageRelation这两个属性来设置。

 

 

 

 

文件及文件夹操作:

一、流:

文件流:FileStream。用来操作文件。

命名空间:System.IO;

使用流的步骤:——最原生的流的操作。——通用性最强的。

1.创建流。

FileStream fs = new FileStream("文件路径",FileMode枚举);

FileMode.Appen - 如果存在,就打开追加;如果不存就新建

FileMode.Create - 如果存在,覆盖。如果不存在就新建。

FileMode.OpenOrCreate。如果存在就打开,不存在就新建。

2.读取或写入流。

 

(1)..写入流:

 第一步:需要把字符串,变成二进制数组:

 byte[] aaa = System.Text.Encoding.Default.GetBytes(txt.Text);

 第二步:写入流

     fs.Write(aaa, 0, aaa.Length);

//第一个参数:要被写进去的二进制数据;

//第二个参数:开始的位置,一般都是0

//第三个参数:要往流里面写入的长度。一般都是数组的length

 

(2).读取流:

第一步:准备一个二进制的数组,用来接收二进制数据。

byte[] aaa = new byte[fs.Length];  //数组的长度,一般使用流的长度来定义,因为这个数组就是用来存储流中的数据的。

第二步:使用进行读取,把读出来的内容放在上面的二进制数组中。

fs.Read(aaa,0,aaa.Length);

//第一个参数:被读出来的二进制数据;

//第二个参数:开始的位置,一般都是0。从哪开始读

//第三个参数:要读取的长度。一般都是数组的length。即流的长度

 

第三步:把读出的二进制数组变成字符串。

string s = System.Text.Encoding.Default.GetString(aaa);

 

3.关闭流。

fs.Close();

 

文件操作时,防止内存泄漏的方法:

法一:try...finally

法二:using(){ }

案例:

        private void btnSave_Click(object sender, EventArgs e)

        {

            FileStream fs = null;

            try

            {

                //创建流

                fs  = new FileStream("aaa.txt", FileMode.Create);

 

                //把文本框的字符串,变成1010101二进制byte[]

                byte[] aaa = System.Text.Encoding.Default.GetBytes(txt.Text);

                //写入流

                fs.Write(aaa, 0, aaa.Length);

            }

            finally

            {

                //关闭流

                if (fs != null)

                {

                    fs.Close();

                }

            }

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            //创建流

            using (FileStream fs = new FileStream("aaa.txt", FileMode.Open))

            {

                //准备一个空的二进制数组,用来接收读取出来的内容

                byte[] aaa = new byte[fs.Length];

                //读取流

                fs.Read(aaa, 0, aaa.Length);

                //把二进制转为字符串

                txt.Text = System.Text.Encoding.Default.GetString(aaa);

 

                //关闭流

                fs.Close();

            }

        }

 

对于文本(字符串)形式的文件流可以使用StreamWriter和StreamReader来简化操作。

1.创建流

FileStream fs = new FileStream("bbb.txt",FileMode.Create);

//FileStream fs = new FileStream("bbb.txt",FileMode.Open);

2.套读取器或写入器。

StreamWriter writer = new StreamWriter(fs);

//StreamReader reader = new StreamReader(fs);

3.使用读取器或写入器对文件流进行操作。

writer.Writer("字符串");

//string s = reader.ReadToEnd();

4.关闭读取器和文件流。

writer.Close();

//reader.Close();

fs.Close();

 

对于文本(字符串)形式的文件还可以再简化。——不用写流的创建,关闭,直接使用流的读取器和写入器

        private void button4_Click(object sender, EventArgs e)

        {

            StreamWriter writer = new StreamWriter("ccc.txt",true, Encoding.Default);

            writer.Write(txt.Text);

            writer.Close();

        }

 

        private void button5_Click(object sender, EventArgs e)

        {

            StreamReader reader = new StreamReader("ccc.txt",Encoding.Default);

            txt.Text =  reader.ReadToEnd();

            reader.Close();

        }

 

新建文本文档:

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO;

namespace WindowsFormsApplication1

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        private string _FileName="";

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)

        {

            txt.Text = "";

            _FileName = "";

        }

 

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)

        {

            DialogResult result = open.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)

            {

                _FileName = open.FileName;

                StreamReader reader =  null;

                try

                {

                    reader = new StreamReader(_FileName, Encoding.Default);

                    txt.Text = reader.ReadToEnd();

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message,"提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

                finally

                {

                    if (reader != null)

                    {

                        reader.Close();

                    }

                }

 

            }

        }

 

        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)

        {

            SaveAsFile();

        }

 

        private void SaveAsFile()

        {

            DialogResult result = save.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)

            {

                _FileName = save.FileName;

                SaveFile();

            }

        }

 

        private void SaveFile()

        {

            StreamWriter writer = null;

            try

            {

                writer = new StreamWriter(_FileName, false, Encoding.Default);

                

                writer.Write(txt.Text);

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            finally

            {

                if (writer != null)

                {

                    writer.Close();

                }

            }

        }

 

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)

        {

            if (_FileName.Trim().Length == 0)   //新建的还没有保存过的

            {

                SaveAsFile();

            }

            else  //打开的或者已经保存过的

            {

                SaveFile();

            }

        }

 

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)

        {

            Close();

        }

 

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)

        {

            txt.SelectAll();

        }

 

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

        {

            txt.Cut();

        }

 

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)

        {

            txt.Paste();

        }

 

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)

        {

            txt.Copy();

        }

 

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

        {

            DialogResult result = font.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)

            {

                txt.Font = font.Font;

            }

        }

 

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)

        {

 

        }

    }

}

 

 

 

 

 

控件的动态生成与操作:

事件三要素:事件源,事件数据,事件处理程序

//sender-事件源,EventArgs-事件数据,函数体就是处理程序

        private void button1_Click(object sender, EventArgs e)

        {

            Button btn = sender as Button; //把事件源具像化。

            MessageBox.Show(btn.Text); //显示事件源的文本。

        }

 

 

 

 

 

 

多按钮自动生成(模拟扫雷系统):

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace 计算器去

{

    public partial class 多按钮操作 : Form

    {

        public 多按钮操作()

        {

            InitializeComponent();

        }

 

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)

        {

            

        }

 

        private void 多按钮操作_Load(object sender, EventArgs e)

        {

            Shezhitablelayout();

            Addbutton();

        }

        private void Addbutton()

        {

            Random rand=new Random ();

            for(int i=1; i<401;i++)

            {

                Button btn=new Button ();

                btn.Text = i.ToString();

                btn.Dock = DockStyle.Fill;

                tableLayoutPanel1.Controls.Add(btn);

                if (rand.Next(100) % 5 == 0)

                {

                    btn.Tag = 10;

                }

                else if (rand.Next(100) % 7 == 0)

                {

                    btn.Tag = 50;

                }

                else if (rand.Next(100) % 9 == 0)

                {

                    btn.Tag = 100;

                }

                else { btn.Tag = 0; }

                btn.Click += btn_Click;

            }

        }

 

        void btn_Click(object sender, EventArgs e)

        {

            Button btn = sender as Button;

            int shu = (int)btn.Tag;

            if (shu != 0)

            {

                btn.BackColor = Color.Red;

                btn.ForeColor = Color.Yellow;

                btn.Text = btn.Tag.ToString();

            }

            btn.Enabled = false;

        }

        private void Shezhitablelayout()

        {

            tableLayoutPanel1.ColumnStyles.Clear();//清理原来的列

            tableLayoutPanel1.ColumnCount = 20;//设置新的列(这里的20指的是列数)

            for (int i = 0; i < 20; i++)//循环20次,编辑列的属性

            {

                ColumnStyle style = new ColumnStyle(SizeType.Percent, 5);

                //列的属性,括号中第一个数Percent为列按百分比实现宽,第二个数为这个宽占总体的5%

                tableLayoutPanel1.ColumnStyles.Add(style);//添加列

            }

            tableLayoutPanel1.RowStyles.Clear();//清理原来的行

            tableLayoutPanel1.RowCount = 20;//设置新的行(这里的20指的是行数)

            for (int i = 0; i < 20; i++)//循环20次,编辑行的属性

            {

                RowStyle style = new RowStyle(SizeType.Percent, 5);

                //列的属性,括号中第一个数Percent为行按百分比实现高,第二个数为这个高占总体的5%

                tableLayoutPanel1.RowStyles.Add(style);//添加行

            }

        }

    }

}

 

加载数据库中的数据,如果内容非常多,就显示出滚动条。类似于QQ好友列表的效果。

1.先放一个Panel在窗体中Dock布局一下。

2.在这个Panel里面放一个FlowLayoutPanel,设置它从上到下流式布局。

3.外面的Panel需设置AutoScroll=true .超出范围就显示滚动条。**

(AutoScroll指示控件内容大于它的可见区域时是否自动显示滚动条)

4.里面的FlowLayoutPanel,设置它Dock=Top,AutoSize=true

(AutoSize指控件是否自动调节自身的大小以适应其内容的大小)

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using DA;

using Data;

 

namespace WindowsFormsApplication1

{

    public partial class Form5 : Form

    {

        public Form5()

        {

            InitializeComponent();

        }

 

        private void Form5_Load(object sender, EventArgs e)

        {

            flowLayoutPanel1.Controls.Clear();

 

            List<CarData> list = new CarDA().Select();

 

            foreach (CarData data in list)

            {

                CheckBox cb = new CheckBox();

                cb.Text = data.Name;

                cb.Tag = data.Code;

                cb.Width = 200;

                cb.Appearance = Appearance.Button;

 

                flowLayoutPanel1.Controls.Add(cb);

            }

        }

    }

}

 

 

 

 

 

窗体类型:(重,0515AM,不熟)

SDI  MDI

单文档窗体界面,多文档窗体界面

 

一、单文档窗体界面:

有一个主窗体,其它的窗体是辅窗体和对话框。

主窗体一旦关掉,整个程序全部结束。

 

案例一:如何做登录?

1.确定好主窗体。在Program.cs文件中的Main函数,使用Application.Run()启动主窗体。

Application.Run(new Main());

2.做一个登录窗体。

登录验证完成后,不要关闭窗体,也不要显示主窗体。只需要给this.DialogResult赋个值即可。

        private void button1_Click(object sender, EventArgs e)

        {

            //验证用户名和密码是否正确

            if (textBox1.Text == "aaa" && textBox2.Text == "bbb")

            {

                this.DialogResult = System.Windows.Forms.DialogResult.OK;

            }

            else

            {

                textBox1.Text = "";

                textBox2.Text = "";

            }

        }

3.在Program.cs的Main函数中,在Application.Run()运行主窗体之前,启动登录窗体(用对话框的形式启动)

如果对话框返回成功的标识时,就运行下面的Application.Run();

            Form1 f = new Form1();

            DialogResult result = f.ShowDialog();

            if (result == DialogResult.OK)

            {

                Application.Run(new Main());

            }

 

二、多文档窗体界面:

一个父窗体,里面包含多个子窗体。子窗体无法移到窗体外面,父窗体关掉子窗体全部关闭。

1.确定父窗体。设置父窗体的属性:IsMdiParent = true

2.做一个子窗体。

3.在窗体的代码中,实例化子窗体。

4.设置子窗体的属性:MdiParent,设置成父窗体对象

ChildForm f = new ChildForm();

            f.MdiParent = this; //这里的this代表的就是父窗体对象。

5.显示子窗体

f.Show();

 

父窗体中的重要属性:

IsMdiParent - 是否是父窗体 bool

MdiChildren - 所有的子窗体的集合。 Form[]

ActiveMdiChild - 当前正在顶层的子窗体。 Form

 

案例一:使用菜单添加多个子窗体,关闭所有子窗体,关闭当前子窗体。

案例二:只生成一个子窗体。如果已经有了一个子窗体的话,就不再添加新的子窗体。

 

posted on 2015-05-22 08:39  冰冥寒流  阅读(8151)  评论(0编辑  收藏  举报