formcombobox.cs

来自「csharp课本的源代码」· CS 代码 · 共 68 行

CS
68
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ComboBoxExam
{
    public partial class FormComboBox : Form
    {
        private void EditEnable(object sender, EventArgs e)
        {
            //由于nameComBox控件可以由用户输入新姓名,判断时不能使用SelectedIndex属性
            if (comboBoxName.Text != "" && comboBoxDepartment.SelectedIndex > -1)
            {
                richTextBox1.Enabled = true;
                buttonOpenFile.Enabled = true;
                buttonSaveFile.Enabled = true;
            }
        }

        public FormComboBox()
        {
            InitializeComponent();
        }

        private void buttonAddName_Click(object sender, EventArgs e)
        {
            if (comboBoxName.Text != "")
            {
                bool newitem = true;
                //判断当前comboBoxName中用户输入的姓名是否已经存在于下拉列表中
                for (int i = 0; i < comboBoxName.Items.Count; i++)
                {
                    string oneitem = Convert.ToString(comboBoxName.Items[i]);
                    if (oneitem == comboBoxName.Text)
                    {
                        newitem = false;
                    }
                }
                //如果用户输入的姓名不在下拉列表中,则添加
                if (newitem)
                {
                    comboBoxName.Items.Add(comboBoxName.Text);
                }
            }

        }

        private void buttonOpenFile_Click(object sender, EventArgs e)
        {
            richTextBox1.LoadFile("D:\\source.rtf");
        }
        //保存文件,并清除RTFRichBox中的文本,给出提示信息
        private void buttonSaveFile_Click(object sender, EventArgs e)
        {
            richTextBox1.SaveFile("D:\\source.rtf");
            richTextBox1.Clear();
            MessageBox.Show("文件保存完毕!");
            richTextBox1.Enabled = false;
            buttonOpenFile.Enabled = false;
            buttonSaveFile.Enabled = false;

        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?