zoukankan      html  css  js  c++  java
  • 文件TEXTBOX





    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    namespace 文件名
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object senderEventArgs e)
            {
                winTextBox1.LostFocus += new EventHandler(winTextBox1_LostFocus);
            }
            //重命名选中的项目
            private void winTextBox1_LostFocus(object senderEventArgs e)
            {
                //文件不能存在
                //_files和_attachments不能存在
                int index = listBox1.Items.IndexOf(winTextBox1.Text);
                int i = listBox1.SelectedIndex;
                if (i == -1)
                    return;
                if (index == -1)
                    listBox1.Items[i] = winTextBox1.Text;
                else
                    winTextBox1.Text = (string)listBox1.Items[i];
            }
            private void listBox1_SelectedIndexChanged(object senderEventArgs e)
            {
                int i = listBox1.SelectedIndex;
                if (i != -1 && !winTextBox1.Focused)
                    winTextBox1.Text = (string)listBox1.Items[i];
                Text = i.ToString();
            }
        }
    }

    /*
     移掉非法字符
     
     WIN8的文件重命名 符合以下条件才会被重命名
      文件名不能包含以下字符
      \ / : * ? " < > |
     
     WIN8的文件重命名 隐藏功能
     1 当在键盘上按非法字符的按键时,textBox是没反映的
     2 当按CTRL+V键时,textBox只粘贴  剪切板中的非法字符 被 去掉后的字符串  
     3 当使用右键快捷菜单 粘贴的时候  剪切板中的非法字符 被 去掉后的字符串  
     4 当使用输入法输入带有非法字符时 非法字符也会被去掉
     5 当光标失去焦点时   当输入的文件名称 为空字符时 textBox会将名称变成 上次名称不为空的字符
     6 当光标失去焦点时   名称的结尾为英文点号.  点号会被移除
     7 textBox会自动移掉文件名的 开头和结尾的空格
     8 按Enter自动 执行步骤6
     2014年7月9日11:03:32 BY roman
     */
    namespace System.Windows.Forms
    {
        public class WinTextBox : TextBox
        {
            //文件名非法字符
            private char[] invalidChar = new char[] { '\''/'':''*''?''"''<''>''|' };
            
           /// <summary>
           /// 最后一次不为空的字符串
           /// </summary>
            public string LAST = "";
            private string RemoveInvalidChar(string objText)
            {
                //移除非法字符"\/:*?"<>|"
                objText = objText.Replace("\""");
                objText = objText.Replace("/""");
                objText = objText.Replace(":""");
                objText = objText.Replace(""""");
                objText = objText.Replace("*""");
                objText = objText.Replace("?""");
                objText = objText.Replace("/""");
                objText = objText.Replace("<""");
                objText = objText.Replace(">""");
                objText = objText.Replace("|""");
                return objText;
            }
            protected override void WndProc(ref Message m)
            {
                //WM_PASTE
                if (m.Msg == 0x302 && Clipboard.ContainsText())
                {
                    this.SelectedText = RemoveInvalidChar(Clipboard.GetText());
                    this.SelectionStart = this.Text.Length;
                    return;
                }
                base.WndProc(ref m);
            }
            protected override void OnKeyPress(KeyPressEventArgs e)
            {
                if (e.KeyChar == 13)
                {
                    OnLostFocus(e);
                }
                e.Handled = (this.Text.IndexOfAny(invalidChar) != -1);
                base.OnKeyPress(e);
            }
            protected override void OnTextChanged(EventArgs e)
            {
                if (this.Text.IndexOfAny(invalidChar) != -1)
                {
                    this.Text = RemoveInvalidChar(this.Text);
                    this.SelectionStart = this.Text.Length;
                }
                if (Text.Trim() != "")
                    LAST = Text;
                base.OnTextChanged(e);
            }
            protected override void OnLostFocus(EventArgs e)
            {
                string s = this.Text;
                //移动最后的.号 移除空格
                if (s.Trim() == ".")
                    s = "";
                while (s.Trim().EndsWith("."))
                {
                    s = s.Remove(s.Length - 1, 1);
                }
                this.Text = s.Trim();
                if (this.Text.Trim() == "")
                    this.Text = LAST;
                base.OnLostFocus(e);
            }
        }
    }




  • 相关阅读:
    TPM 程序设计基础 2-1 :具体函数调用步骤及解析示例
    TPM 程序设计基础 1-2-4 :新的 PCR 命令
    TPM 程序设计基础 1-2-3 :Tspi_PcrComposite 类定义
    TPM 程序设计基础 1-2-2 :旧的 PCR 命令
    TPM 程序设计基础 1-2-1 :Tspi_Context 类定义
    TPM 程序设计基础 1-1 :所用 Tspi 函数目录
    TPM 程序设计基础 0-0 :课设程序结构说明
    TPM 课设程序运行基础与要求
    jstl和EL表达式混合使用
    在eclipse中从cvs下载项目,再部署到tomcat常见错误!
  • 原文地址:https://www.cnblogs.com/xe2011/p/3833318.html
Copyright © 2011-2022 走看看