C#

C#学习

C#的概念

  • C#是一种面向对象的编程语言,首先需要知道的是何为面向对象,语言有面向对象与面向过程两种。一切皆为对象。同时,C#是一种优雅的语言,意在以最简洁的代码解决问题。

C#区别于C++

  • 在C#中的输入与输出:Consolove.ReadLine()表示输入一个数值;Consolove.WriteLine()表示输出,注意在C#中输入的默认为字符串,所以想要输入为整型或者是字符类型或者浮点型,eg:int a=int.Prase(Cosolove.ReadLine());在C#输出中,如果想要输出多个字符串或者说是值的话,eg:Consolove.WriteLine(“a={0},b={1}”,a,b);表示{}中为后边的值。
  • 在C#中的数组,链表,因为C#属于面对对象的编程,区别于C,在C里想要输入或者是输出数组,链表,通常都是需要运用循环,但是在C#中可以直接使用List,eg:List list=new List();list.Add(2);表示已新建一个整型数据的链表,链表名为list,添加了第一个元素为2。
  • C#中的函数的引用,一般在C中使用函数,指针有时候是必不可少的,值传递与无返回值,很多时候都需要用到指针,不过显然在C#中式没有指针的,但是依照个人理解来看,C#中使用的ref在某种意义上就是相当于指针了。因为本人最开始了解的就是C语言了,所以里面很多的指针的内容,取地址还有取值,传参等等就不在此详细解释了。
  • C#的循环,因为继承了C++的各种方法与类,所以C#中对于for ,while ,if ,switch,do等基本循环都是可以适用的,但是在C#中有一个很好用的循环,就是foreach,eg:int[] arry={0,1,2,3}; foreach(int i in arry) {Consolove.WriteLine(i)};表示的就是打印出arry整个数组里的所有元素,遍历法,foreach中的理解为,in表示在里面,也就是说在arry这个歌数组里面,从i开始,逐渐执行下面的循环,这种循环方法在多位数组中显得格外简洁和快速,但是同时foreach的限制表现为无法进行修改数据。
  • 理解重载。
  • 泛型。我们在写程序的时候,很多时候会有一定的重复,但是一般来说我们都是要避免重复工作的,所以有了泛型。
  • 在C#中使用累加的时候,区别于C的地方在于,C#中有一个params函数。
    #话不多说,说多无益,先给大家看看我的第一周做出来的一个小玩意吧,一个简单的记事本。

记事本的开始界面

记事本的开始界面

记事本的运行界面

记事本的运行界面

代码实现

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.Drawing.Text;
using System.Collections;
using System.IO;
using System.Drawing.Text;
using System.Collections;
using System.IO;

namespace 记事本
{
    public partial class 子窗体 : Form
    {
        public 子窗体()
        {
            InitializeComponent();
        }
        //窗口加载时间
        private void toolStripTextBox1_LocationChanged(object sender, EventArgs e)
        {
            //窗口加载时,要加载字体
            InstalledFontCollection myFonts = new InstalledFontCollection();
            //获取InstalledFontCollection对象的数组
            FontFamily[] ff = myFonts.Families;
            //声明一个ArrayList数组
            ArrayList list = new ArrayList();
            //获取系统数组的列表中集合的长度
            int count = ff.Length;

            for(int i=0;i<count;i++)
            {
                string FontName = ff[i].Name;
                
            }
        }
        //窗体加载事件
        private void toolStripComboBoxFonts_LocationChanged(object sender, EventArgs e)
        {
            //窗体加载时,加载系统字体
            InstalledFontCollection myFonts = new InstalledFontCollection();
            //获取 InstalledFontCollection的数组
            FontFamily[] ff = myFonts.Families;
            //声明一个ArrayList变量
            ArrayList list = new ArrayList();
            int count = ff.Length;
            //使用for循环把字体写入toolStripComboBoxFonts控件中
            for (int i=0;i<count;i++)
            {
                string FontName = ff[i].Name;
                toolStripComboBoxFonts.Items.Add(FontName);
            }
        }
        //加粗按钮
        private void toolStripButtonBold_Click(object sender, EventArgs e)
        {
            //点击按钮是加粗,加粗是再点击按钮取消加粗
            if (textBoxNote.Font.Bold == false)
            {
                textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Bold);
            }
            else
            {
                textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Regular);
            }
        }
        //倾斜按钮
        private void toolStripButtonItalic_Click(object sender, EventArgs e)
        {
            if(textBoxNote.Font.Italic==false)
            {
                textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Italic);
            }
            else
            {
                textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Regular);
            }
        }
        //改变选择字体的索引事件
        private void toolStripComboBoxFonts_SelectedIndexChanged(object sender, EventArgs e)
        {
            string fontName = toolStripComboBoxFonts.Text;
            float fontSize = float.Parse(toolStripComboBoxSize.Text);
            textBoxNote.Font = new Font(fontName, fontSize);
        }
        //改变字体大小
        private void toolStripComboBoxSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            string fontName = toolStripComboBoxFonts.Text;
            float fontSize = float.Parse(toolStripComboBoxSize.Text);
            textBoxNote.Font = new Font(fontName, fontSize);
        }
        //人为输入数字
        private void toolStripComboBoxSize_TextChanged(object sender, EventArgs e)
        {
            string fontName = toolStripComboBoxFonts.Text;
            float fontSize = float.Parse(toolStripComboBoxSize.Text);
            textBoxNote.Font = new Font(fontName, fontSize);
        }
        //保存文档
        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            if(textBoxNote.Text.Trim() !="")
            {
                if (this.Text == "")
                {
                    //新建一个保存文件的路径
                    //创建一个过滤器,只保存这种格式
                    saveFileDialog1.Filter = ("文本文档(*.txt)|*.txt");
                    //判断用户保存的是保存按钮还是取消按钮
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        //保存文件到用户指定的目录
                        //获取用户选择的文件以及文件路径
                        string path = saveFileDialog1.FileName;
                        //保存文件到指定路径
                        StreamWriter sw = new StreamWriter(path, false);
                        sw.WriteLine(textBoxNote.Text.Trim());
                        sw.Flush();
                        sw.Close();
                    }
                }
                else
                {
                    //保存文件到用户指定的目录
                    //获取用户选择的文件以及文件路径
                    string path = this.Text;
                    //保存文件到指定路径
                    StreamWriter sw = new StreamWriter(path, false);
                    sw.WriteLine(textBoxNote.Text.Trim());
                    //把窗体的属性设置为保存后的文件路径
                    this.Text = path;
                    sw.Flush();
                    sw.Close();
                }
            }
            else
            {
                MessageBox.Show("空文档不能保存!", "信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
        //打开文档
        private void toolStripButtonOpen_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = ("文本文档(*.txt)|*.txt");
            //判断用户保存的是打开按钮还是取消按钮
            if(openFileDialog1.ShowDialog()==  DialogResult.OK )
            {
                //获取打开文档的路径
                string path = openFileDialog1.FileName;
                //通用编码UTF8
                StreamReader st = new StreamReader(path,Encoding.UTF8);
                //读取文档中的数据流
                string txt = st.ReadToEnd();
                textBoxNote.Text = txt;
                //将打开的文件路径写到当前text属性中
                this.Text = path;
                打开文件,清空记号
                //toolStripLabel1.Text = " ";

                st.Close();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            textBoxNote.Text = "";
            this.Text = "";

        }

        //private void toolStripLabel1_TextChanged(object sender, EventArgs e)
        //{
        //    toolStripLabel1.Text = "*";
        //}
        //窗体关闭事件
        //private void 子窗体_FormClosing(object sender, FormClosingEventArgs e)
        //{
        //    if(toolStripLabel1.Text=="*")
        //    {
        //        DialogResult dr= MessageBox.Show("文档尚未保存,确定要继续退出吗?", "信息询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        //        if(dr ==  DialogResult.Yes)
        //        {
        //            this.Dispose();
        //        }
        //        else
        //        {
        //            e.Cancel = true;
                }
            }
        //}


    }
}

以上就是本周的主要内容还有一个记事本的代码实现。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值