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 sender, EventArgs e)
        {
            winTextBox1.LostFocus += new EventHandler(winTextBox1_LostFocus);
        }
        //重命名选中的项目
        private void winTextBox1_LostFocus(object sender, EventArgs 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 sender, EventArgs 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);
        }
    }
}

文件TEXTBOX的更多相关文章

  1. C#中使用Log4net日志输出到本地文件、Textbox或Listview

    网上很多配置log4net的方法,但是排行靠前的 根本就没有说明清除,导致浪费了两个小时来搞清楚如何配置,真是无语,特写此文,给那些刚接触log4net的朋友 1.参考链接:http://blog.s ...

  2. 传智播客--数据绑定--INotifyPropertyChanged(小白内容)

    INotifyPropertyChanged一般在数据绑定的时候使用. InotifyPropertyChanged是.net内置的接口,数据绑定时会检测DataContext是否实现了Inotify ...

  3. ASP.NET加载主题和皮肤样式的各种方式

    一.加载主题(皮肤.样式表)的多种方式 除了在页面指令中采用Theme或者StylesheetTheme为单个页面加载主题外,还可以通过配置文件为多个页面批量加载主题,另外,还可以通过改变页面的The ...

  4. Wpf解决TextBox文件拖入问题、拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同, 解放方法如下: 使用Previ ...

  5. WPF中TextBox文件拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...

  6. 解决WPF中TextBox文件拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...

  7. c#打开txt文件并导入到textbox中

    OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = " 请选择您要导入的模板文件:&qu ...

  8. c#保存textbox中的字符串到txt文件中

    /********************** 保存接收按钮 *****************************/ private void SavetxData_Click(object s ...

  9. C#按键打开文件选择对话框,并把选择好的路径保存/显示到textBox

    1.选择文件夹 FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.SelectedPath = "D:";//默认路 ...

随机推荐

  1. Angularjs中input的指令和属性

    建议添加 novalidate属性(可无值)到form标签上,这样可以保证在表单不合法的情况下阻止浏览器继续提交数据. 可以给表单元素 input 添加 required 属性(可无值),让该表单成为 ...

  2. 【 D3.js 高级系列 — 4.0 】 矩阵树图

    矩阵树图(Treemap),也是层级布局的扩展,根据数据将区域划分为矩形的集合.矩形的大小和颜色,都是数据的反映.许多门户网站都能见到类似图1,将照片以不同大小的矩形排列的情形,这正是矩阵树图的应用. ...

  3. 【 D3.js 进阶系列 】 进阶总结

    进阶系列的文章从去年10月开始写的,晃眼又是4个多月了,想在年前总结一下. 首先恭祝大家新年快乐.今年是羊年吧.前段时间和朋友聊天,聊到十二生肖里为什么没猫,我张口就道:不是因为十二生肖开会的时候猫迟 ...

  4. 一致性hash算法 - consistent hashing

    consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛: 1 ...

  5. c语言编译预处理和条件编译执行过程的理解

    在C语言的程序中可包括各种以符号#开头的编译指令,这些指令称为预处理命令.预处理命令属于C语言编译器,而不是C语言的组成部分.通过预处理命令可扩展C语言程序设计的环境. 一.预处理的工作方式 1.1. ...

  6. Java [leetcode 36]Valid Sudoku

    题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  7. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

  8. 【转】Android项目中编译 C的模块

    原文网址:http://blog.csdn.net/Harrison_zhu/article/details/4057738 Android编译环境本身比较复杂,且不像普通的编译环境:只有顶层目录下才 ...

  9. 错误 1 在应用程序级别之外使用注册为 allowDefinition='

    原文:错误 1 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的,银流沙 昨天运行一个.NET网站项目时,出现了以下问题: 在应 ...

  10. 17、手势(Gesture)

    课程目标: 学习Android必不可少的手势的功能 了解手势识别原理 , 掌握制作,加载以及识别手势 写出自己的手势Demo 重点难点:手势机制的了解     手势库的制作 考核目标:请说一下手势库的 ...