第十周作业

28 篇文章 1 订阅

一、完善上周的“记事本”程序的功能。

1、加上复制、剪切、粘贴、撤消等菜单及功能。

a.编写【编辑】菜单下的各功能菜单项的具体功能代码。

        private void mi_Edit_Copy_Click(object sender, EventArgs e)
        {
            //复制菜单项
            textBox1.Copy();

            //if (textBox1.SelectedText!="")
            //    Clipboard.SetDataObject(textBox1.SelectedText);
        }

        private void mi_Edit_Cut_Click(object sender, EventArgs e)
        {
            //剪切菜单项
            textBox1.Cut();
        }

        private void mi_Edit_Paste_Click(object sender, EventArgs e)
        {
            //粘贴菜单项
            textBox1.Paste();
        }

        private void mi_Edit_Cancel_Click(object sender, EventArgs e)
        {
            //撤消菜单项
            if (textBox1.CanUndo)
                textBox1.Undo();
        }

b.完成【编辑】主菜单项的DropDownOpened(即【编辑】菜单项被打开之后)事件函数mi_Edit_DropDownOpened(...)的代码,让各菜单项具备智能感知能力,如已选择文本则复制和剪切功能可用,否则此两项菜单不可用;如果剪切板中已有复制的文本内容,则粘贴菜单项自动设置为可用等等。

        private void mi_Edit_DropDownOpened(object sender, EventArgs e)
        {
            mi_Edit_Undo.Enabled = textBox1.CanUndo;

            mi_Edit_Copy.Enabled = textBox1.SelectedText != "";// textBox1.SelectionLength > 0; //判断是否选中了文本信息
            mi_Edit_Cut.Enabled = mi_Edit_Copy.Enabled;
            mi_Edit_Paste.Enabled = Clipboard.ContainsText();//判断剪切板中最后一次复制或剪切的内容是否是文本信息
        }

2、增加打开文件时对文件的字符编码格式自动判断的功能。

a.创建一个单独的类文件:TextEncodingType.cs,如何定义这个类看这里:https://blog.csdn.net/xieyunc/article/details/89819831

b.在Form1类中增加一个私有字段,用于存储和说明TextBox中当前文件的字符编码类型: private Encoding _FileEncode = Encoding.UTF8;

    public partial class Form1 : Form
    {
        private string _FileName = "";//文件名
        private Encoding _FileEncode = Encoding.UTF8;//系统默认编码为ANSI:Encoding.Default
        private bool _IsSaved = true;//是否已保存

        ……
        ……
    }

c.在状态栏中增加一个提示标签:statusBar_Encode,放置在状态栏的最右边,在新建或打开文件时显示当前文件的字符编码类型。

d.在打开文件和保存文件功能函数中加入判断文件编码类型的相关功能,为此需要设计一个自定义静态类来处理完成这一功能。如何判断文件的编码格式?

        private void mi_OpenFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                _FileName = openFileDialog1.FileName;
                this.Text = _FileName;
                _FileEncode = TextEncodingType.GetType(_FileName);//调用自定义静态类,自动获取文件的编码类型
                System.IO.StreamReader sr;
                sr = new System.IO.StreamReader(_FileName, _FileEncode);//加入了自动判断文件编码类型的功能
                //sr = new System.IO.StreamReader(_FileName);
                textBox1.Text = sr.ReadToEnd();
                sr.Close();
                statusBar_Encode.Text = _FileEncode.EncodingName;
                _IsSaved = true;
            }
        }

 

二、几个常用编辑类控件功能演示

3、ListBox控件功能演示

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 Demo_ListBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //增加项
            listBox1.Items.Add(textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //按内容删除项
            listBox1.Items.Remove(textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //插入项
            int i = listBox1.SelectedIndex;//获取ListBox中当前选中的行
            if (i==-1) //如果没有行被选中
                i = 0;

            listBox1.Items.Insert(i, textBox1.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //按索引删除项
            if (listBox1.SelectedIndex!=-1) //如果选中了项目
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //获取ListBox的状态
            textBox2.Clear();

            textBox2.AppendText(string.Format("选中项数量:{0}\r\n", listBox1.SelectedItems.Count));

            if (!checkBox1.Checked && listBox1.SelectedItems.Count > 0)//如果是单选模式
            {
                textBox2.AppendText(string.Format("选中项索引号:{0}\r\n", listBox1.SelectedIndex));//如果为多选模式,SelectedIndex返回第一个选中项的索引号
                textBox2.AppendText(string.Format("选中项内容:{0}\r\n", listBox1.SelectedItem.ToString()));
            }
            else
            {
                foreach (string str in listBox1.SelectedItems)
                {
                    textBox2.AppendText(string.Format("选中项内容:{0}\r\n", str));
                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //是否允许ListBox多选
            if (checkBox1.Checked)
                listBox1.SelectionMode = SelectionMode.MultiSimple;
            else
                listBox1.SelectionMode = SelectionMode.One;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //清空全部项
            listBox1.Items.Clear();
        }
    }
}

4、ComboBox控件功能演示

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 Demo_ComboBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //增加项
            comboBox1.Items.Add(textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //按内容删除项
            comboBox1.Items.Remove(textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //插入项
            int i = comboBox1.SelectedIndex;//获取ListBox中当前选中的行
            if (i==-1) //如果没有行被选中
                i = 0;

            comboBox1.Items.Insert(i, textBox1.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //按索引删除项
            if (comboBox1.SelectedIndex!=-1) //如果选中了项目
                comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
                //textBox2.AppendText(comboBox1.SelectedIndex.ToString() + "\r\n"); //上一条命令等价于本条
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //获取ListBox的状态
            textBox2.Clear();

            textBox2.AppendText(string.Format("选中项索引号:{0}\r\n", comboBox1.SelectedIndex));
            if (comboBox1.SelectedIndex != -1)
                textBox2.AppendText(string.Format("选中项内容:{0}\r\n", comboBox1.SelectedItem.ToString()));
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //清空全部项
            comboBox1.Items.Clear();
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.Simple;
        }

        private void radioButton2_Click(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDown;

        }

        private void radioButton3_Click(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

        }
    }
}

5、ListView控件功能演示

6、演示DEMO源代码在github上的仓库地址:

https://github.com/xieyunc/csharp_teach.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值