C#之WinForm——基础知识

MessageBox的几种常见用法

  1. 简单
MessageBox.Show("你确定想要删除吗?");

在这里插入图片描述
2. 增加上面提示标题

MessageBox.Show("你确定想要删除吗?", "提示");

在这里插入图片描述
3. 增加取消按钮

if (MessageBox.Show("你确定想要删除吗?", "提示",
   	MessageBoxButtons.OKCancel) == DialogResult.OK)
   	{
   		// delete
   	}

在这里插入图片描述
4. 增加图标

if (MessageBox.Show("你确定想要删除吗?", "提示", MessageBoxButtons.OKCancel, 
   MessageBoxIcon.Question) == DialogResult.OK)
    {
      //delete
    }

在这里插入图片描述
5. 定义默认选择取消

if (MessageBox.Show("你确定想要删除吗?", "提示", MessageBoxButtons.OKCancel, 
	MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) 
	== DialogResult.OK)
     {
      //delete
     }

在这里插入图片描述

事件参数object sender, EventArgs e的理解

参数sender

sender表示触发事件的那个控件。若同一类控件处理方法相同,可以只写一个事件处理。(如:多个Button处理方法相同)就需要用到sender这个参数,这时需要把sender转换为相应类型的控件即可。
如:TextBox textbox = sender as TextBox;

例:四个Button单击,显示Button内容

private void button1_Click(object sender, EventArgs e)
       {
           Button mybtn = sender as Button;
           MessageBox.Show(mybtn.Text);
       }

参数e

e参数包含了事件所携带的信息,它用来辅助处理事件。
若e不是EventArgs 类本身,而是其子类(如:KeyPressEventArgs e),那么在写事件过程的时候往往会用到该对象e的属性或方法。如:e.Handled、 e.KeyChar。

例:控制文本框只能输入数字、.或退格键。
PS:e.Handled=true表示阻止输入字符, e.KeyChar表示用户输入的字符

private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9')
            {
                e.Handled = false; //不阻止,表示输入的是数字,则可以输入
            }
            else
            {
                if ((e.KeyChar == '.') || e.KeyChar == '\b')
                {
                    e.Handled = false; //表示退格键或.可以输入
                }
                else
                {
                    e.Handled = true; //其他阻止,即是不可以输入
                }
            }
}

遍历窗体控件

 foreach (Control item in this.Controls)
            {
                if (item is TextBox) 	// 清空TextBox内容
                {
                    item.Text = "";
                }
                if (item is RadioButton)	// 重置RadioButton(选择男)
                {
                    RadioButton r = (RadioButton)item;
                    r.Checked = false;
                    rdMale.Checked = true;
                }
                if (item is CheckBox)	// 重置CheckBox
                {
                    CheckBox ck = (CheckBox)item;
                    ck.Checked = false;
                }
            }

ListBox列表框常用属性

  • Items属性
    — Items .Add(object item) 向选项列表添加选项(添加一项)
    —Items .AddRange(object[] items) 向选项列表添加选项(添加多项)
    —Items .Remove(object value) 从集合中移除指定对象
    —Items .RemoveAt(int index) 移除集合中指定索引的项
    —Items .Insert(int index, object item) 向选项列表添加选项(添加到指定位置)
    —Items .Clear() 清除列表框的所有项
    —Items[int index] 获取或设置集合中指定索引处的项
    —Items .Count() 获取集合中的项数

例:listBox1.Items.Add(“aa”);
例:listBox1.Items.AddRange(new string[] {“aa”, “bb”});
例:listBox1.Items.Remove(“aaa”);
例:listBox1.Items.RemoveAt(0);
例:listBox1.Items.Insert(1, “bbb”);
例:listBox1.Items.Clear();
例:listBox1.Items[0] --> “aaa”
例:listBox1.Items[0] = “aaa”;
例:listBox1.Items.Count(); --> 3

  • SelectedItems属性
    获取包含在当前选定的项的集合
    —SelectedItems.Count 表示被选择的项目的数目
    —SelectedItems[i] 根据指定索引获取被选择的项,即获取被选择的i项

例:listBox1.SelectedItems.Count // 10个里面选择了3个 则值为3
例:listBox1.SelectedItems[2] // 从被选择的3个里面,取第三个的值

  • SelectedItem属性
    获取或设置当前被选择的1项

例:listBox.SelectedItem // 获取选择中的第一项的值
例:listBox.SelectedItem = “aaa” // ????

  • SelectedIndex
    获取或设置选中项的索引(从0开始)

例:listBox.SelectedIndex --> 2 (选择了第3、5、6条数据)
例:listBox.SelectedIndex = 2 // 将第3条数据设置为选中状态

TreeView控件

—TreeNode tn1 = new TreeNode(string text, TreeNode[] children) 设置节点
—treeView1.Nodes.Add(string text / TreeNode node) 添加节点
—treeView1.SelectedNode.Level 获取深度
—treeView1.ExpandAll() 展开所有节点

  • 31
    点赞
  • 153
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 C# WinForm 中实现修改密码功能,可以按照以下步骤进行: 1. 在 WinForm 中添加两个文本框和一个按钮,一个文本框是用于输入旧密码,另一个文本框是用于输入新密码,按钮是用于提交修改密码的操作。 2. 在按钮的 Click 事件中,编写代码实现密码修改的逻辑。首先,可以将用户输入的旧密码和新密码保存到字符串变量中,然后可以使用一些条件语句对密码进行验证,例如,旧密码是否正确,新密码是否符合要求等等。如果密码验证通过,可以调用相应的方法将新密码保存到数据库中。 下面是一个简单的密码修改示例代码: ```csharp private void btnChangePassword_Click(object sender, EventArgs e) { string oldPassword = txtOldPassword.Text.Trim(); string newPassword = txtNewPassword.Text.Trim(); // 验证旧密码是否正确 if (!ValidateOldPassword(oldPassword)) { MessageBox.Show("旧密码不正确!", "提示"); return; } // 验证新密码是否符合要求 if (!ValidateNewPassword(newPassword)) { MessageBox.Show("新密码不符合要求!", "提示"); return; } // 将新密码保存到数据库中 if (!SaveNewPassword(newPassword)) { MessageBox.Show("密码修改失败!", "提示"); return; } MessageBox.Show("密码修改成功!", "提示"); } private bool ValidateOldPassword(string password) { // TODO: 根据需要实现旧密码验证逻辑 return true; } private bool ValidateNewPassword(string password) { // TODO: 根据需要实现新密码验证逻辑 return true; } private bool SaveNewPassword(string password) { // TODO: 根据需要实现保存新密码到数据库的逻辑 return true; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值