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. C#中的预处理器指令

    C#中有许多名为“预处理器指令”的命令.这些命令从来不会转化为可执行代码中的命令,但会影响编译过程的各个方面. 例如,使用预处理器指令可以禁止编译器编译代码的某一部分.如果计划发布两个版本的代码,即基 ...

  2. ASP.NET Cache

    ASP.NET为了方便我们访问Cache,在HttpRuntime类中加了一个静态属性Cache,这样,我们就可以在任意地方使用Cache的功能. 而且,ASP.NET还给它增加了二个“快捷方式”:P ...

  3. can't able to update the design capacity in bq27441-G1

    /*************************************************************************** * can't able to update ...

  4. Bootstrap 源码解析(转)

    1.Bootstrap的作用域 2.Bootstrap的类定义 3.Bootstrap的插件定义 4.Bootstrap的事件代理 5.Bootstrap的对象数据缓存 6.Bootstrap的防冲突 ...

  5. 深入理解Arrays.sort()

    两种方法: 1.类本来就实现java.lang.Comparable接口,使类本身就有比较能力.接口实现compareTo方法,次方法接收另一个Object为参数,如果当前对象小于参数则返回负值,如果 ...

  6. Ruby类,模块1

    类的扩展和继承 class Fixnum def dosome(str) puts str end def abs puts "覆盖了原有的方法" end end puts 1.c ...

  7. jQuery.autoComplete 多参数

    query 版本 1.3.2插件默认的 参数 是q 如果需要传递多个参数呢?$("#stylistname").autocomplete("/page/autostyli ...

  8. HDU 5675 ztr loves math

    ztr loves math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. UMDF

    看了一周的UMDF,今天就将这些天的心得记下来. 一开始,老大推荐看<竹林蹊径:深入浅出Windows驱动开发完美补全版.张佩马勇董鉴源.扫描版>.这本书看了前三章,这本书讲的太细,作者又 ...

  10. LA3942-Remember the Word(Trie)

    题意: 有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法 分析: dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len( ...