C#窗口设计之实现多个功能

大概介绍

文章主要是基于一个界面来实现多个功能的实现

登录

登录界面

在这里插入图片描述

登录代码

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

        private void button1_Click(object sender, EventArgs e)
        {
           
            if (textBox1.Text != ""&&textBox2.Text != "")//两个框未输入
            {
                if (textBox1.Text != "2024"&&textBox2.Text != "2023")//设定第一框输入值为2023,第二框为2024
                {
                   
                    MessageBox.Show("登录成功");//符合上面要求,弹出登录成功
                    Form2 fm = new Form2();//定义第二个窗口
                    this.Hide();//隐藏登录窗口
                    fm.ShowDialog();//打开第二个窗口
                }
            }
            else//除了登录成功的情况以外
            {
                MessageBox.Show("账号密码未输入");//弹出账号密码未输入
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.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;

namespace 作业
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            排序 fn = new 排序();//定义排序窗口
            this.Hide();//隐藏功能大选窗口
            fn.Show();//打开功能大选窗口
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            图形 san = new 图形();//定义图形窗口
            this.Hide();//隐藏功能大选窗口
            san.Show();//展示图形界面
        }

        private void button3_Click(object sender, EventArgs e)
        {
            百文买鸡 ji = new 百文买鸡();//定义百文买鸡
            this.Hide();//隐藏功能大选窗口
            ji.ShowDialog();//打开百文买鸡
        }

        private void button4_Click(object sender, EventArgs e)
        {
            简单计算器 qi = new 简单计算器();//定义简单计算器
            this.Hide();//隐藏功能大选窗口
            qi.ShowDialog();//打开简易计算器
        }

        private void button5_Click(object sender, EventArgs e)
        {
            文件读写 xie = new 文件读写();//定义文件读写
            this.Hide();//隐藏功能大选窗口
            xie.ShowDialog();//打开文件读写
        }

        private void button6_Click(object sender, EventArgs e)
        {
            选课系统 xi = new 选课系统();//定义选课系统
            this.Hide();//隐藏功能大选窗口
            xi.ShowDialog();//打开选课系统
        }

        private void button7_Click(object sender, EventArgs e)
        {
            字符转换 huan = new 字符转换();//定义字符变换
            this.Hide();//隐藏功能大选窗口
            huan.ShowDialog();//打开字符变换
        }

        private void button8_Click(object sender, EventArgs e)
        {
            字符查找 zhao = new 字符查找();//定义字符查找
            this.Hide();//隐藏功能大选窗口
            zhao.ShowDialog();//打开字符查找
        }

        private void button9_Click(object sender, EventArgs e)
        {
            this.Hide();//关闭窗口
            Form1 hhh = new Form1();//定义第1个窗口
            hhh.ShowDialog();//打开第一个窗口
        }
    }
}

排序

排序界面

在这里插入图片描述

排序代码

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
    {
        string[] temp;//定义temp
        int[] list;//定义list
        int[] list1;//定义list1
        public 排序()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text.ToString();
            string[] split = str.Split(new Char[] { ',' });//以逗号间隔读取数据
            temp = textBox1.Text.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);//去除逗号格式
            list = Array.ConvertAll<string, int>(temp, s => int.Parse(s));//将读取的数据组从字符串的形式转换成整数形式
            list1 = Array.ConvertAll<string, int>(temp, s => int.Parse(s));//将读取的数据组从字符串的形式转换成整数形式
            if (str == "")//若未输入所排数字,则显示为未输入元素
            {
                MessageBox.Show("未输入元素");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < list.Length - 1; i++)//外层循环是比较的轮数
            {
                for (int j = 0; j < list.Length - i - 1; j++)//内层循环是当前一轮中两个数字之间,比较次数的总和
                {
                    if (list[j] > list[j + 1])//相邻两个数如果逆序,则交换位置,即采用升序来排列
                    {
                        int a = list[j];//定义a为临时值来存储数据,一次达到交换数值的目的
                        list[j] = list[j + 1];
                        list[j + 1] = a;
                    }
                }
            }
            for (int n = 0; n < list.Length; n++)//遍历数组或循环中的字符
            {
                textBox2.Text += Convert.ToString(list[n]) + ",";//输出结果
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < list1.Length - 1; i++)//外层循环是比较的轮数
            {
                for (int j = 0; j < list1.Length - i - 1; j++)//内层循环是当前一轮中两个数字之间,比较次数的总和
                {
                    if (list1[j] > list1[j + 1])//相邻两个数如果逆序,则交换位置,即采用升序来排列
                    {
                        int a = list1[j];// 定义a为临时值来存储数据,一次达到交换数值的目的
                        list1[j] = list1[j + 1];
                        list1[j + 1] = a;
                    }
                }
                foreach (int item in list1)//遍历数组或循环中的字符
                {
                    richTextBox1.Text += (item + ",");//显示结果
                }
                richTextBox1.Text += ("\n");//对每一次你结果进行换行
                int b = 0;//定义整型b
                for (int n = 0; n < list1.Length - 1; n++)
                //遍历list1数组
                {
                    if (list1[n] < list1[n + 1])//采用升序来排列
                    {
                        b = b + 1;
                    }
                }
                if (b == list1.Length - 1)
                {
                    break;//打断
                }
            }
            }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";//清空输入框
            richTextBox1.Text = "";//清空过程显示框
            textBox2.Text = "";//清空排序结果框
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Hide();//关闭排序窗口
            Form2 fm = new Form2();//定义窗口2
            fm.ShowDialog();//打开功能大选
        }
    }
}

星号图形

星号图形界面

在这里插入图片描述

在这里插入图片描述

星号图形代码

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 button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未输入层数");
            }
            else
            {
                int n = Convert.ToInt32(textBox1.Text);
                for (int i = 1; i < n + 1; i++)//外层循环,实现空号的显示过程
                {
                    for (int j = 0; j < i; j++)//内层循环,实现“*”的显示流程
                    {
                        richTextBox1.Text += "*";

                    }
                    richTextBox1.Text += "\n";
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未输入层数");
            }
            else
            {
                int n = Convert.ToInt32(textBox1.Text);
                for (int i = 1; i <= n; i++)//外层循环,实现的空号显示过程,需要在星号前加上空格
                {
                    for (int j = 1; j <= n - i; j++)//内层循环,实现空号的显示流程
                    {
                        richTextBox1.Text += " ";//显示*号

                    }
                    for (int a = 1; a <= n; a++) //内层循环,实现*号的显示流程
                    {
                        richTextBox1.Text += "*";//显示*号
                    }
                    richTextBox1.Text += "\n";//换行
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未输入层数");
            }
            else
            {
                int n = Convert.ToInt32(textBox1.Text);
                for (int i = 1; i <= n; i++)//外层循环,循环的次数
                {
                    for (int j = 0; j <= n - i; j++)//内层循环,实现空号的显示流程
                    {
                        richTextBox1.Text += " ";//显示*号
                    }
                    for (int a = 1; a <= (2 * i - 1); a++) //内层循环,实现*号的显示流程
                    {
                        richTextBox1.Text += "*";//显示*号
                    }
                    richTextBox1.Text += "\n";//换行
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未输入层数");
            }
            int n = Convert.ToInt32(textBox1.Text);
            for (int i = 1; i <= n; i++)//外层循环,实现空号的显示过程
            {
                for (int j = 0; j< i; j++)
                    richTextBox1.Text += " ";//显示空号
                richTextBox1.Text += "*";//显示*号
                richTextBox1.Text += "\n";//换行
            }
           
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未输入层数");
            }
            int n = Convert.ToInt32(textBox1.Text);
            for (int i = n; i>0; i--)//外层循环,实现空号的显示过程
            {
                for (int j = 0; j < i-1; j++)
                { 
                    richTextBox1.Text += " ";//显示空号
                }
                richTextBox1.Text += "*";//显示*号
                richTextBox1.Text += "\n";//换行
            }
           
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未输入层数");
            }
            else
            {
                int n = int.Parse(textBox1.Text) / 2 + 1;  // 这样就算出上部分的总行数了
                for (int i = 1; i <= n; i++)//外层循环,换行的次数
                {
                    for (int j = 1; j <= n - i; j++) //内层循环,实现空号的显示流程
                    {
                        richTextBox1.Text += " ";//显示空格号
                    }
                    for (int k = 1; k <= 2 * i - 1; k++)//内层循环,实现*号的显示流程
                    {
                        richTextBox1.Text += "*";//显示*号
                    }
                    richTextBox1.Text += "\n";//换行
                }//下面显示代码
                for (int i = 1; i < n; i++)//外层循环,换行的次数
                {
                    for (int j = 1; j <= i; j++)//内层循环,实现空号的显示流程
                    {
                        richTextBox1.Text += " ";//显示空格号
                    }
                    for (int k = 1; k <= (2 * n - 1) - 2 * i; k++)//内层循环,实现*号的显示流程
                    {
                        richTextBox1.Text += "*";//显示*号
                    }
                    richTextBox1.Text += "\n";//换行
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == "")
            {
                MessageBox.Show("未输入层数");
            }
            else
            {
                int n = Convert.ToInt32(textBox1.Text);
                for (int i = 0; i <= n ; i++)//外层循环,实现空号的显示过程
                {
                    for (int j = 0; j <= n-i; j++)//内层循环,实现“*”的显示流程
                    {
                        richTextBox1.Text += " ";//显示空格号
                    }
                    for(int a=1;a<= i;a++)
                    {
                        richTextBox1.Text += "*";//显示*号
                    }
                    richTextBox1.Text += "\n";//换行
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";//清空输入框
            richTextBox1.Text = "";//清空过程显示框
        }

        private void button9_Click(object sender, EventArgs e)
        {
            this.Hide();//关闭排序窗口
            Form2 fm = new Form2();//定义窗口2
            fm.ShowDialog();//打开功能大选
        }
    }
}

百文买鸡

百文买鸡界面

在这里插入图片描述

百文买鸡代码

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();
        }
        int scheme = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未输数");
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("未输入数");
            }
            if (textBox3.Text == "")
            {
                MessageBox.Show("未输入数");
            }
            if (textBox5.Text == "")
            {
                MessageBox.Show("未输入数");
            }
            else
            {
                int money = Convert.ToInt32(textBox5.Text);
                int rooster = Convert.ToInt32(textBox1.Text);
                int hen = Convert.ToInt32(textBox2.Text);
                int chicken = Convert.ToInt32(textBox3.Text);
                for (int a = 1; a <= money / rooster; a++)//定义公鸡,循环条件运算,每次循环加1
                {
                    for (int b = 1; b <= money / hen; b++)//定义母鸡,循环条件运算,每次循环加1
                    {
                        for (int c = 1; c <= money / chicken; c++)//定义小鸡,循环条件运算,每次循环加1
                        {
                            if (a * rooster + b * hen + c * chicken == money)//百文买鸡条件
                          { 
                                richTextBox1.Text += "当公鸡" + a + "只" + "母鸡" + b + "只" + "小鸡" + c + "只的时候,正好是100元\n";
                                scheme++;
                            }
                        }
                    }
                }

            }
            textBox4.Text += Convert.ToString(scheme);


        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";//清空输入框
            textBox2.Text = "";//清空输入框
            textBox3.Text = "";//清空输入框
            textBox4.Text = "";//清空输入框
            textBox5.Text = "";//清空输入框
          
            richTextBox1.Text = "";//清空过程显示框
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();//关闭排序窗口
            Form2 fm = new Form2();//定义窗口2
            fm.ShowDialog();//打开功能大选
        }
    }
}

简易计算器

简易计算器界面

在这里插入图片描述

简易计算器代码

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 button1_Click(object sender, EventArgs e)
        {
            string a = richTextBox1.Text;
            double num3;
            a = this.richTextBox1.Lines[0];
            num3 = Convert.ToDouble(a);
            richTextBox1.Text += "\ncos(" + a + ")\n";
            richTextBox1.Text += "=" + Math.Cos(Math.PI * num3 / 180);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string a = richTextBox1.Text;
            double num2;
            a = this.richTextBox1.Lines[0];
            num2 = Convert.ToDouble(a);
            richTextBox1.Text += "\nsin(" + a + ")\n";
            richTextBox1.Text += "=" + Math.Sin(Math.PI * num2 / 180);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string a = richTextBox1.Text;
            double num4;
            a = this.richTextBox1.Lines[0];
            num4 = Convert.ToDouble(a);
            double d = Math.Pow(num4, 2.0);
            richTextBox1.Text += "\n^2(" + a + ")\n";
            richTextBox1.Text += "=" + d;
        }

        private void button4_Click(object sender, EventArgs e)
        {

            string a = richTextBox1.Text;
            double num1;
            a = this.richTextBox1.Lines[0];
            num1 = Convert.ToDouble(a);
            richTextBox1.Text += "\nsqrt(" + a + ")\n";
            richTextBox1.Text += "=" + Math.Sqrt(num1);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";//清空过程显示框
            richTextBox1.Text += "0";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";//清空过程显示框
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string a = richTextBox1.Text;
            string temp = a.Remove(a.LastIndexOf(""), 1);
            richTextBox1.Text = temp;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "\n/\n";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "9";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "8";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "7";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "\n*\n";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "6";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "5";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "4";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "\n-\n";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "3";
        }

        private void button18_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "2";
        }

        private void button19_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "1";
        }

        private void button20_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "\n+\n";
        }

        private void button21_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "\n^n\n";
        }

        private void button22_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "0";
        }

        private void button23_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += ".";
        }

        private void button24_Click(object sender, EventArgs e)
        {
            double a = Convert.ToDouble(this.richTextBox1.Lines[0]);
            double b = Convert.ToDouble(this.richTextBox1.Lines[2]);
            string c = this.richTextBox1.Lines[1];
            if (c == "+")
            {
                double d = a + b;
                richTextBox1.Text += "=\n" + d;
            }

            if (c == "-")
            {
                double d = a - b;
                richTextBox1.Text += "=\n" + d;
            }
            if (c == "*")
            {
                double d = a * b;
                richTextBox1.Text += "=\n" + d;
            }
            if (c == "/")
            {
                double d = a / b;
                richTextBox1.Text += "=\n" + d;
            }
            if (c == "^n")
            {
                double d = Math.Pow(a, b);
                richTextBox1.Text += "=\n" + d;
            }
        
        }

        private void button25_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";//清空过程显示框
        }

        private void button26_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏计算器窗口
            Form2 fm = new Form2();//定义窗口2
            fm.ShowDialog();//打开功能大选
        }
    }
}

文件读写

文件读写界面

在这里插入图片描述

文件读写代码

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 作业
{
    public partial class 文件读写 : Form
    {
        public 文件读写()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // 打开文件成功,获取文件名
                string fileName = openFileDialog1.FileName;

                // 使用 StreamReader 类来读取文件内容
                using (StreamReader sr = new StreamReader(fileName))
                {
                    textBox1.Text = "";
                    textBox1.Text = Path.GetFullPath(openFileDialog1.FileName);
                    string[] a = File.ReadAllLines(textBox1.Text);
                    foreach (string b in a)
                    {
                        richTextBox1.Text += b + "\n";
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 创建 SaveFileDialog 对象
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            // 设置默认文件类型
            saveFileDialog.Filter = "文本文件 (*.txt)|*.txt";
            // 设置默认文件名(可选)
            saveFileDialog.FileName = "未命名文件";

            // 打开文件保存对话框
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 获取选择的文件路径
                string filePath = saveFileDialog.FileName;

                // 将文本保存到文件
                File.WriteAllText(filePath, richTextBox1.Text);
            }
            textBox2.Text = "";
            textBox2.Text = Path.GetFullPath(saveFileDialog.FileName);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";//清空过程显示框
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏计算器窗口
            Form2 fm = new Form2();//定义窗口2
            fm.ShowDialog();//打开功能大选
        }
    }
}
        

选课系统

选课系统界面

在这里插入图片描述

选课系统代码

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 作业
{
    public partial class 选课系统 : Form
    {
        public 选课系统()
        {
            InitializeComponent();
        }

        private void 打开_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();//打开文件
            openFileDlg.Filter = "txt(*.txt)|*.txt";
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    StreamReader srFile = new StreamReader(openFileDlg.FileName);
                    FiletextBox1.Clear();
                    FiletextBox1.Text = Path.GetFullPath(openFileDlg.FileName);
                    string[] stringLines = File.ReadAllLines(openFileDlg.FileName);//读取打开的文件
                    foreach (string s in stringLines)//将文件里的课程导入 comboBox1
                    {
                        comboBox1.Items.Add(s);
                    }
                    srFile.Close();
                }
                catch (Exception)//检查读取是否出错
                {
                    MessageBox.Show("发生错误");
                }
            }
        
    }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.SelectedIndex >= 0)
                    listBox1.Items.Add(comboBox1.Items[comboBox1.SelectedIndex].ToString());
                if (listBox1.Items.Count >= 2)
                    MessageBox.Show("已经选择了!", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (comboBox1.SelectedIndex < 0)
                    MessageBox.Show("请选择课程!", "添加确认", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                else
                {
                    MessageBox.Show("选课成功!", "添加确认", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                }
            }
            catch
            {
                MessageBox.Show("请选择课程!", "添加确认", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Text = "";//清空过程显示框
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏字符查找窗口
            Form2 fm = new Form2();//定义窗口2
            fm.ShowDialog();//打开功能大选
        }

        private void FiletextBox1_TextChanged(object sender, EventArgs e)
        {
            listBox1.Text = comboBox1.Text;//将 comboBox1中选中的内容在listBox1中输出
        }
    }
}

字符查找

字符查找界面

在这里插入图片描述

字符查找代码

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 button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")//如果输入框中未输入即跳出未输入的窗口
            {
                MessageBox.Show("未输入");
            }
            string a = textBox1.Text;//令a等于textbox1的内容
            string[] b = a.Split(' ');//对其进行矩阵识别数字位置,以逗号隔开
            int[] c = new int[b.Length];//创建一个新矩阵为c,c与b的长度相当
            for (int i = 0; i < b.Length; i++)//定义整型i=0,当i小于b的长度时进入循环
            {
                int d = Convert.ToInt32(b[i]);//定义一个整型d为输入的值,并将输入值转化为整型
                c[i] = d;//将d存储在c当中
            }
            int g = Convert.ToInt32(textBox2.Text);//定义一个整型g为输入的值,并将输入值转化为整型
            int f = -1;//定义整型f=-1
            for (int i = 0; i < c.Length; i++)//定义整型i=0,当i小于c的长度时进入循环
            {
                if (g == c[i])//如果g==c[i]
                {
                    f = i + 1;//如果f=i+1是计算位置结束
                    break;//打断
                }
            }
            textBox3.Text += f;//输出f,代表数字所在的位置
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";//清空输入框
            textBox2.Text = "";//清空输入框
            textBox3.Text = "";//清空输入框

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏字符查找窗口
            Form2 fm = new Form2();//定义窗口2
            fm.ShowDialog();//打开功能大选
        }
    }
}

字符变换

字符变换界面

在这里插入图片描述

字符变换代码

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 button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")//如果输入框中未输入即跳出未输入的窗口
            {
                MessageBox.Show("未输入");
            }
            string a = textBox1.Text;//令a等于textbox1的内容
            int b = 'A' - 'a';//b为遍历ASCLL码的差值
            for (int i = 0; i < a.Length; i++)//记录字符串的长度
            {
                if (a[i] >= 'a' && a[i] <= 'z')
                {
                    Char c = (Char)(a[i] + b);//小写转大写
                    textBox2.Text += c;//输出c
                }
                else if (a[i] >= 'A' && a[i] <= 'z')
                {
                    Char c = (Char)(a[i] - b);//大写转小写
                    textBox2.Text += c;
                }
                else
                {
                    textBox2.Text += a[i];//如果不是字母则输出为输入的字符串本身
                }
            }
            textBox2.Text += "\n";//对输出进行换行

        }
    

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏字符查找窗口
            Form2 fm = new Form2();//定义窗口2
            fm.ShowDialog();//打开功能大选
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";//清空输入框
            textBox2.Text = "";//清空输入框
        }
    }
}

创建窗口

创建窗口步骤图

在这里插入图片描述

这里以简易计算器来举例

在这里插入图片描述

打开窗口关键代码

简单计算器 qi = new 简单计算器();//定义简单计算器
this.Hide();//隐藏功能大选窗口
qi.ShowDialog();//打开简易计算器

文章总结

随着文章接近尾声,我的作业记录也完成了。学习C#也先告一段落。博主毕竟是新手,文章难免有些错误之处,若路过的友友们发现了,欢迎大家在评论区中指正,互相学习,互相进步,在此共勉。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值