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. poj 2031 Building a Space Station(prime )

    这个题要交c++, 因为prime的返回值错了,改了一会 题目:http://poj.org/problem?id=2031 题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能 ...

  2. poj 1159 Palindrome(dp)

    题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...

  3. RazorEngine(未解决,留底)

    TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration { BaseTemplateType = ...

  4. UVa 1643 Angle and Squares

    题意: 如图,有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 分析: 直观上来看,当这n个正方形的对角线在一条直线上时,封闭区域的面积最大.( ...

  5. hdu 3367 Pseudoforest

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  6. 【Java基础之容器】Iterator

    Iterator: ->所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象 ->Iterator对象称作迭代器,用以方便的实 ...

  7. linq里面似in的查询

    1.linq里面似in的查询 List<string> source = new List<string>{ "aaa", "bbb" ...

  8. ASP.NET工作笔记之一:图片上传预览及无刷新上传

    转自:http://www.cnblogs.com/sibiyellow/archive/2012/04/27/jqueryformjs.html 最近项目里面涉及到无刷新上传图片的功能,其实也就是上 ...

  9. java快速获取大图片的分辨率(大图片格式JPG,tiff ,eg)

    问题描述:怎样快速获取一个20MB图片的分辨率? 程序代码: package test; import java.awt.Dimension; import java.awt.image.Buffer ...

  10. mexopencv问题:Invalid MEX file GLIBCXX_3.4.15 error

    参考:http://blog.sina.com.cn/s/blog_74112f030101cmxt.html root@debian-yuliyang:/opt/matlab/sys/os/glnx ...