winform窗体应用之IO流操作、单/复选框、自定义事件、下拉框和多级联动

IO流

FileStream类-->文件流 l--->Input输入读取 读取本地文件中的数据到程序内存当中 O--->Output输出 把程序中的数据写入到本地文件中

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
​
namespace _04_FileStream文件流
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
​
        // 文件读取
        private void button1_Click(object sender, EventArgs e)
        {
            // 指定文件路径
            string filePath = @"E:\aa\a\day20\resource\mp3s\a.txt";
            FileStream fs = new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.Read);// 读
            // 读取a.txt文件中的内容
            // 准备一个数组
            byte[] bytes = new byte[1024];// 1024  100个有字节数  1kb
            // 循环读取
            // 弄一个更大的容器
            List<byte> list = new List<byte>();
            string str = "";
            while (true)
            {
                int readNumber = fs.Read(bytes, 0,bytes.Length );// 返回值:代表从流中读取到的有效字节个数
                if (readNumber == 0)
                {
                    break;
                }
                for (int i = 0; i <= readNumber-1; i++)
                {
                    list.Add(bytes[i]);
                }
                //string message = Encoding.UTF8.GetString(bytes, 0, readNumber);
                ////MessageBox.Show(message+"-------------");
                //str += message;
            }
            //byte[] newBytes = new byte[list.Count];
            byte[] newBytes = list.ToArray();
            string message = Encoding.UTF8.GetString(newBytes, 0, newBytes.Length);
            // 转换成string字符串
            MessageBox.Show(message);
​
        }
    }
}

FileStream

public static void Main(string[] args)
{
string path1=@"E:\'aaa\aa\day21\resource\users.txt";
string path2=@"E:\'aaa\aa\daay21\users.txt";
//创建两个对象
FileStream fsRead = new FileStream(path1,FileMode.OpenOrCreate, FileAccess.Read);
FileStream fsWrite = new FileStream(path2,FileMode.OpenOrCreate,FileAccess.Write);
//构建一个数组
byte[] buffer = new byte[1024];
int num = fsRead.Read(buffer,0,buffer.Length);// admin?admin
//把读取到的字节数据转换成字符串类型的数据
string str = Encoding.Default.GetString(buffer,0, num);//
/*
解码:使用某种编码字符集把字节数据转换成字符数据。字符串
编码:把字符数据按照某种编码字符集转换成字节数据进行格式转换
UTF-8字符集:字母,数字,英文标点符号都是占一个字节,沙字占3个字节
GBK字符集:汉字占2个字节
*/
/*
  在系统当中,I0流(文件流)不会自动关闭或者释放占用的内存资资源
一般情况下需要我们手动释放和关闭资源
Close()---->关闭流对象
//10流在内存当中数据存储方式一般情况是按照栈的结构进行存储
//先进后出First in last out---->FILO
//先打开的流对象最后释放和关闭,后打开的流对象先释放和关闭
Depose()--->释放资源,从内存清除掉
*/
   fswrite.Close();
   fsRead.Close();
   fsRead.Dispose();
    */
 //要想IO流对象像窗口对象一样当关闭的时候自动释放资源
 //C#当中提供了IO流语法糖   using(){} 语句块
 //foreach------>for
 using(FileStream fsWrite = new FileStream(path1, FileMode.OpenOrCreate,FileAccess.Write){
    string str2="天气好";
        //Write()
        byte[] bytes=Encodiong.Default.GetBytes(str2)
        fswrite.Write(bytes ,0,bytes.Length);
 }
      // 当它跳出using语句块时,在using语句块中创建的文件输出流对象从内存中释放掉了。
       }

总结:

1.可以把创建读入文件流和写入文件流对象放到using语句块中,当跳出using语句块时,自动关闭并释放打开的文件流对象

2.FileStream当我们想要实现在文件的末尾追加文件的字符数据时,可以把FileMode文件模式设置Append(如果文件不存在,直接新建一个新文件然后写入对应的字符数据,如果文件存在并且里面有内容,把后写入的内内容写在文件的末尾位置,不会对文件的原内容进行覆盖)。

3.在对文件进行读写时,如果涉及到中文字符,写入时一定要保证正写入的字符编码集和文件中原来的编码字符集保持一致,否则就会会出现中文乱码。

4.在对文件中读取内容时,涉及到有中文字符时,需要注意不要人为把汉字切割成不同大小的字节数据,否则就会不能识别乱码。

Stream Reader和StreamWriter

//文件读取
private void button1_Click(object sender, EventArgse)
{
    //StreamWriter和StreamReader
    using (StreamReader sr = new StreamReader(path,Encoding.UTF8)
    //stringstr=sr.ReadToEnd();//读取文件中的全部内容
    //MessageBox.Show(str);
    string str2 = sr.ReadLine();//读取的是第一行信息
    string str3 = sr.ReadLine()//读取的是第一行信息
    MessageBox.Show(str2+"\n"+str3);
    string content = "";
    wwhile(true)
    {
        string str = sr.ReadLine
        if(str == null)
        {
            break;
        }
        /*
        if(sr.EndOfStream)
        {
           break;
        }
        */
        content += str + "\n"
    }
}
//文件写入--StreamWriter
​
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter sw = new StreamWriter(path,treu,Encoding.UTF8)
 {
     string message="艳阳高照天气好"
     sw.WriteLine(EnvirONment.newLine+message);
 }

总结

1.StreamReader类:

  • 构造方法: StreamReader(string path,Encoding.UTF8)

  • ReadToEnd():从当前位置读取到文件的末尾

  • ReadLine():从当前位置读取到文件的下一行内容,如果没有下一行内容,返回null

  • EndOfSteam属性:判断流当前位置是否为文件的末尾,如果是,返回true,如果不是返回false

2.StreamWrite类

  • 构造方法:主要有两个

    • StreamWriter(string path):给定指定文件路径,往指定文件中写入内容

    • StreamWrite(string path,bool append,Encoding encoding):创建字符输出流对象,指定写入的文件地址,设置是否为追加模式,并且还可敬设置写入到文件中的编码字符集。

  • Write():普通写入

  • WriteLine():写入后末尾添加有换行符

互斥单选按钮RadioButton和多选按钮CheckBox

namespace_04_RadioButton和CheckBox单选和多选按钮
{
public partial class Form1 : Form
{
public Form1()
InitializeComponent();
private void radioButton1_CheckedChanged(objectsender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;//不会报转换异常
if (radioButton != null & radioButton.Checked)
{
string text = radioButton. Text;
switch (text)
{
case"男":
MessageBox.Show("打游戏");
break;
case"女":
MessageBox.Show("去购物");
break;
case"人妖":
MessageBox.Show("看表演");
break;
}
}
}
}
//多选
//查看选中的兴趣爱好
​
private void button1_Click(object sender, EventArgs
{
    先获取GroupBox控件容器中添加的所有的CheckedBox控件
IEnumerable<CheckBox> checkBoxes
groupBox1.Controls.OfType<CheckBox>(
//Checked属性是否为true
//定义一个字符串变量
string str = "";
 //查看选中的个数
     int a=0;
foreach (CheckBox checkBox in checkBoxes)
{
if(checkBox.Checked)
{
//代表等于true
str += checkBox. Text+"\n";
    a++;
}
MessageBox.Show(str);
​
         }
//查看集合复选框的选定内容
// 查看电子产品
​
private void button3 click(object sender, EventArgs e)
{
// Items
CheckedListBox.0bjectcollection items = checkedListBox1.Items;
//查看容器列有多少个   Count属性
MessageBox.Show("电子产品个数为:"+items.Count.ToString());
 //查看选中的条目个数
MessageBox.Show("选中的电子产品个数为:"+checkedListBox1.Checkeditems.Count);
CheckedListBox.CheckedItemCollection checkedItems = checkedListBox1.CheckedItems;
//  查看都有哪些电子产品被选中了
    string str="";
foreach (CheckBox checkBox in items)
{
    str+=item+"\n";
}
 MessageBox.Show("选中的电子产品内容为:\n"+str);   
//给CheckedListBox容器添加选项
​
private void button4_Click(object sender, EventArgse)
{
string item= textBox1.Text;
checkedListBox1.Items.Add(item);
}
// 删除选项
​
private void button5_Click(object sender, EventArgse)
{
string item = textBox1.Text;
checkedListBox1.Items.Remove(item);
}

自定义事件绑定

  • 双击控件绑定事件,绑定的事件是默认事件,对于button控件是点击事件,对于Form窗体是Load事件

  • 找到小闪电图标,选择一个事件双击绑定,或者是从定义的方法中选择一个进行关联绑定

  • 通过代码编辑的方式进行事件绑定。

通过代码的方式,往Form窗体当中添加控件,同时给控件绑定事件

Control---顶级组件容器

//通过代码的方式,点击button按钮给Form窗体添加TextBox控件
int num=1;
private void button1_Click(object sender, EventArgs e)
{
    TextBox textBox = new TextBox();
//设置文本输入框的大小
    textBox.Size = new Size(100,20);
//给textBox控件添加name属性
    textBox.Name="textBox"+num++;
    指定textBox存放的位置
textBox. Location= new Point(num*100,20);
    //给输入框绑定一个双击事件
textBox.DoubleClick += textBoxDoubleClick;
    //添加到Form控件中
    this.Controls.Add(textBox)
}
//绑定一个双击事件
​
private void textBoxDoubleClick(object sender,EventArgs e)
{
    TextBoxtextBox= sender as TextBox;
    MessageBox.Show(textBox.Name),
}

Combox下拉框

常用属性和方法

  • SelectedIndex----->选中的索引

  • Selectedltem---->选中项的Text文本值

  • SelectedindexChanged--->选中项索引更改事件

  • Items----->列表项中的集合

  • Items集合中:Add、Remove

  • DorpDpwwnStyle--->控制下拉框中的文本 Simple, DropDown, DropDownList

//往下拉框添加
private void button1_Click(object sender, EventArgs e)
{
string message = textBox1.Text;
    comboBox1.Items.Add(message);//链式调用
}
​
//删除
private void button2_Click(object sender, EventArgs e)
{
string message = textBox1.Text;
comboBox1.Items.Remove(message);
}

二级联动

//二级联动
//定义一个数组:表示省份
public string[] pros={"河南省","河北省","山东省"};
//添加省份对应的市-->需要一个二维数组
public string[,]cts={{"郑州市","开封市","洛阳市")},{"石家庄市","邯郸市","雄安市"},{"济南市","青岛市","威海市"}};
//绑定加载事件
private void Form1_Load(object sender, EventArgs e)
{
foreach (string s in pros)
{
comboBox2.Items.Add(s);
}
}
//双击绑定选中索引更改事件
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    //先清空原来的市信息
    comboBox3.Items。Clear();
    //获取对应的省份索引
int index = comboBox2.SelectedIndex;
   // 取出二维数组中对应索引中元素值放到第二个下拉框中
    for (int j = 0; j <= cts.GetLength(1)-1; j++)
    {
        comboBox3.Items.Add(cts[index,j]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值