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. 函数buf_page_get

    /**************************************************************//** NOTE! The following macros shoul ...

  2. Vs 引用第三方DLL文件 版本不一致问题 (npoi与memcached中的ICSharpCode.SharpZipLib版本冲突的解决方案)

    最近在 做 MailChimp 与网站功能 集成时,发现 MailChimp 2API 中的 MailChimp.dll  中的依赖项 SerivceStack.Text.dll (版本为3.9.71 ...

  3. Session简介

    摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术.本文将详细讨论session的工作机制并且对在Java ...

  4. Welcome to Linux From Scratch!

    /**************************************************************************** * Welcome to Linux Fro ...

  5. log4j和web.xml配置webAppRootKey 的问题

    在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,如下: <!-- 应用路径 --> &l ...

  6. HW6.11

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  7. POJ1038 - Bugs Integrated, Inc.(状态压缩DP)

    题目大意 要求你在N*M大小的主板上嵌入2*3大小的芯片,不能够在损坏的格子放置,问最多能够嵌入多少块芯片? 题解 妈蛋,这道题折腾了好久,黑书上的讲解看了好几遍才稍微有点眉目(智商捉急),接着看了网 ...

  8. linux内存负载分析

    衡量内存负载的一个很重要的指标就是页面置换的频率.当linux系统频繁的对页进行换进换出 的时候,说明物理内存不过,不得不进行频繁的置换页面. 使用vmstat(virtual memory stat ...

  9. 设置IE浏览器代理上网

    在局域网中,服务器可以直接通过IE网上冲浪,而工作站要想通过IE上网,如果是在服务器使用代理软件的情况下,其IE需要设置代理. 步骤一:启动IE浏览器,选择"工具",再" ...

  10. php透明合并png与jpg图片

    源 <?php $png = imagecreatefrompng('./mark.png'); $jpeg = imagecreatefromjpeg('./image.jpg'); list ...