--------------封装输入框类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Model;
namespace WinForm
{
public class CityTextBox : System.Windows.Forms.TextBox
{
int sd = -1;
private int m_cityValue = 0;
//private System.ComponentModel.Container components = null;
private DoubleBufferListView m_lstShowChoice = null;
private ListViewItem[] m_list = null;
public CityTextBox()
{
this.Leave += new EventHandler(TextBoxExt_Leave);
this.KeyDown += new KeyEventHandler(TextBoxExt_KeyDown);
this.KeyUp += new KeyEventHandler(TextBoxExt_KeyUp);
this.DoubleClick += new EventHandler(TextBoxExt_DoubleClick);
this.Font = new Font("黑体", 10, FontStyle.Italic);
this.ForeColor = Color.Gray;
this.TextAlign = HorizontalAlignment.Center;
this.BorderStyle = BorderStyle.FixedSingle;
}
private void lstBox_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
DoubleBufferListView box = (DoubleBufferListView)sender;
if ((box.SelectedItems.Count > 0) && !this.ReadOnly)
{
this.Text = box.SelectedItems[0].Text;
//选择后文本框失去了焦点,这里移回来
this.Focus();
}
}
private void lstBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
DoubleBufferListView box = (DoubleBufferListView)sender;
#region 设置移动颜色
if (box.GetItemAt(e.X, e.Y) == null)
{
if (sd != -1)
{
if (box.Items.Count >sd)
{
box.Items[sd].BackColor = PromptBackColor;
box.Items[sd].ForeColor = PromptForeColor;
sd = -1;
}
}
}
else if (sd != box.GetItemAt(e.X, e.Y).Index)
{
if (sd != -1)
{
if (box.Items.Count > sd )
{
box.Items[sd].BackColor = PromptBackColor;
box.Items[sd].ForeColor = PromptForeColor;
}
}
sd = box.GetItemAt(e.X, e.Y).Index;
if (box.Items.Count > sd)
{
box.Items[sd].BackColor = Color.Green;
box.Items[sd].ForeColor = Color.White;
}
}
#endregion
}
#region 设置提示框的背景
private Color m_lstForColor = System.Drawing.SystemColors.InfoText;
private Color m_lstBackColor = System.Drawing.SystemColors.InfoText;
/// <summary>
/// 设置/获取提示的背景色
/// </summary>
public Color PromptForeColor
{
get
{
return m_lstForColor;
}
set
{
m_lstForColor = value;
//lstPrompt的创建见下面的代码
DoubleBufferListView box = this.lstPrompt;
if (box != null)
box.BackColor = m_lstForColor;
}
}
public Color PromptBackColor
{
get
{
return m_lstBackColor;
}
set
{
m_lstBackColor = value;
//lstPrompt的创建见下面的代码
DoubleBufferListView box = this.lstPrompt;
if (box != null)
box.BackColor = m_lstBackColor;
}
}
#endregion
public DoubleBufferListView lstPrompt
{
get
{
//如果没有列表用于显示提示的列表框,则创建一个
if ((m_lstShowChoice == null) && this.Parent != null)
{
m_lstShowChoice = new DoubleBufferListView();
m_lstShowChoice.Visible = false;
m_lstShowChoice.Left = this.Left + this.Width;
m_lstShowChoice.Top = this.Top;
m_lstShowChoice.Width = this.Width * 2;
m_lstShowChoice.Height = this.Height * 6;
m_lstShowChoice.TabStop = false;
m_lstShowChoice.Sort();
m_lstShowChoice.ForeColor = this.m_lstForColor; //前景
m_lstShowChoice.BackColor = this.m_lstBackColor; //背景(参见m_lstForColor的创建
m_lstShowChoice.BorderStyle = BorderStyle.FixedSingle;
m_lstShowChoice.MultiSelect = false;
//如果提示框过低,则显示到上面
//if (m_lstShowChoice.Bottom > this.Parent.Height)
// m_lstShowChoice.Top = this.Top - m_lstShowChoice.Height + 8;
m_lstShowChoice.MouseUp += new MouseEventHandler(this.lstBox_MouseUp);
m_lstShowChoice.MouseMove += new MouseEventHandler(this.lstBox_MouseMove);
; m_lstShowChoice.SelectedIndexChanged += new EventHandler(lstBox_SelectedIndexChanged);
this.Parent.Controls.Add(m_lstShowChoice);
this.Parent.ResumeLayout(false);
m_lstShowChoice.BringToFront();
}
return m_lstShowChoice;
}
}
private ArrayList m_ForChoice = new ArrayList();
public ArrayList ChoiceArray
{
get
{
return m_ForChoice;
}
set
{
m_ForChoice = (ArrayList)(value.Clone());
DoubleBufferListView box = this.lstPrompt;
if (box != null)
{
box.Items.Clear();
m_list = new ListViewItem[m_ForChoice.Count];
int index = 0;
foreach (EnCity o in m_ForChoice)
{
m_list[index++] = new ListViewItem(o.Name, o.Bin);
}
box.Items.AddRange(m_list);
}
}
}
private bool m_AllowSpace = false;
public bool AllowSpace
{
get
{
return m_AllowSpace;
}
set
{
m_AllowSpace = value;
}
}
private bool m_bChoiceOnly = false;
public bool ChoicOnly
{
get
{
return this.m_bChoiceOnly;
}
set
{
this.m_bChoiceOnly = value;
}
}
private int m_nOldPos = 0;
private bool bKeyDown = false;
private string m_strOldText = "";
private void TextBoxExt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
m_nOldPos = this.SelectionStart;
bKeyDown = true;
m_strOldText = this.Text;
}
private void FillPrompt(string p_strText)
{
DoubleBufferListView box = this.lstPrompt;
if (box != null)
{
box.Items.Clear();
if (p_strText.Length == 0)//没有内容,显示全部
{
if (m_list != null)
{
box.Items.AddRange(m_list);
}
}
else
{
foreach (EnCity o in this.ChoiceArray)
{
if (o.Name.ToLower().IndexOf(p_strText.Replace(" ", "").ToLower()) >= 0 || o.NameDetail.ToLower().IndexOf(p_strText.Replace(" ", "").ToLower()) >= 0 || o.PinYin.Replace(" ", "").ToLower().IndexOf(p_strText.Replace(" ", "").ToLower()) >= 0 || o.ShortPY.ToLower().IndexOf(p_strText.Replace(" ", "").ToLower()) >= 0)
box.Items.Add(o.Name,o.Bin);
}
}
}
}
private void TextBoxExt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (!bKeyDown)//忽略掉多余的KeyUp事件
return;
bKeyDown = false;
DoubleBufferListView box = this.lstPrompt;
switch (e.KeyCode)
{
//通过上下箭头在待选框中移动
case System.Windows.Forms.Keys.Up:
case System.Windows.Forms.Keys.Down:
if ((box != null) && !this.Multiline)//多行文本通过上下箭头在两行之间移动
{
if ((e.KeyCode == System.Windows.Forms.Keys.Up) && box.SelectedItems.Count > 0)//↑
{
box.Items[box.SelectedItems[0].Index].BackColor = PromptBackColor;
box.Items[box.SelectedItems[0].Index].ForeColor = PromptForeColor;
box.Items[box.SelectedItems[0].Index > 0 ? box.SelectedItems[0].Index - 1 : box.Items.Count-1].Selected = true;
}
else if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
if (box.SelectedItems.Count > 0)
{
box.Items[box.SelectedItems[0].Index].BackColor = PromptBackColor;
box.Items[box.SelectedItems[0].Index].ForeColor = PromptForeColor;
box.Items[box.SelectedItems[0].Index+1 < box.Items.Count ? box.SelectedItems[0].Index + 1 : 0].Selected = true;
}
else
{
box.Items[0].Selected = true;
}
}
//上下箭头不能移动当前光标,因此,还原原来位置
this.SelectionStart = m_nOldPos;
//显示提示框
if (!box.Visible)
{
//if (box.Width != this.Width)
// box.Width = this.Width;
box.Visible = true;
}
}
break;
case System.Windows.Forms.Keys.Escape://ESC隐藏提示
if ((box != null) && box.Visible)
box.Hide();
break;
case System.Windows.Forms.Keys.Return://回车选择一个或跳到下一控件
if ((box == null) || this.Multiline)
break;
//没有显示提示框时,移动到下一控件
if (!box.Visible)
{
SendKeys.Send("{TAB}");
}
else
{ //有提示,关闭提示{
if (box.SelectedItems.Count > 0)
{
//有选择,使用当前选择的内容
this.Text = box.SelectedItems[0].Text;
}
this.SelectionStart = this.Text.Length;
this.SelectAll();
box.Hide();
}
break;
default: //判断文本是否改变
string strText = this.Text;
//不允许产生空格,去掉文本中的空格
if (!this.AllowSpace)
strText = this.Text.Replace(" ", "");
int nStart = this.SelectionStart;
if (strText != m_strOldText)//文本有改变
{
//设置当前文本和键盘光标位置
this.Text = strText;
if (nStart > this.Text.Length)
nStart = this.Text.Length;
this.SelectionStart = nStart;
//修改可供选择的内容,并显示供选择的列表框
if (box != null)
{
FillPrompt(strText);
if (!box.Visible)
{
//if (box.Width != this.Width)
// box.Width = this.Width;
box.Visible = true;
}
}
}
break;
}
}
private void TextBoxExt_Leave(object sender, EventArgs e)
{
//对于只选字段,必须输入同待选相匹配的值
if (this.ChoicOnly)
{
int nIndex = this.ChoiceArray.IndexOf(this.Text);
if (nIndex < 0)
this.Text = "";
else
{
ListViewItem item = m_lstShowChoice.FindItemWithText((this.ChoiceArray[nIndex] as EnCity).Name, false, 0, true);
if (item!=null)
{
item.Selected = true;
}
}
}
//失去焦点后,必须隐藏提示
DoubleBufferListView box = this.lstPrompt;
if (box != null)
box.Visible = false;
}
private void TextBoxExt_DoubleClick(object sender, EventArgs e)
{
if (this.ReadOnly)
return;
if (this.Text.Length>0)
{
FillPrompt(this.Text);
}
DoubleBufferListView box = this.lstPrompt;
if ((box != null) && (!box.Visible))
{
//if (box.Width != this.Width)
// box.Width = this.Width;
box.Visible = true;
int nIndex = this.ChoiceArray.IndexOf(this.Text);
if (nIndex < 0)
this.Text = "";
else
{
ListViewItem item = m_lstShowChoice.FindItemWithText((this.ChoiceArray[nIndex] as EnCity).Name, false, 0, true);
if (item != null)
{
item.Selected = true;
}
}
}
}
private void lstBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (m_lstShowChoice.SelectedItems.Count > 0)
{
m_lstShowChoice.SelectedItems[0].BackColor = Color.Green;
m_lstShowChoice.SelectedItems[0].ForeColor = Color.White;
m_cityValue = m_lstShowChoice.SelectedItems[0].ImageIndex;
}
}
public int CityValue
{
get { return m_cityValue; }
set { m_cityValue = value; }
}
}
/// <summary>
///双缓冲DoubleBufferListView,解决闪烁
/// </summary>
public class DoubleBufferListView :ListView
{
public DoubleBufferListView()
{
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
}
}
}
城市 对象 Model -------------------------------
using System;
using System.Data;
using System.Globalization;
namespace Model
{
#region 字段枚举
public enum FdArticles
{
Bin,
Name,
NameDetail,
PinYin,
ShortPY
}
#endregion
[Serializable]
public class EnCity
{
#region 私有变量
private Int32 _Bin;
private String _Name;
private String _NameDetail;
private String _PinYin;
private String _ShortPY;
#endregion
#region 构造函数
/// <summary>
/// 默认构造函数
/// </summary>
public EnCity()
: base()
{
this._Name = String.Empty;
this._NameDetail = String.Empty;
this._PinYin = String.Empty;
this._ShortPY = String.Empty;
}
/// <summary>
/// 使用只读只进的数据流的构造函数
/// </summary>
/// <param name="dr">只读只进的数据流</param>
public EnCity(IDataReader dr)
: this()
{
if (dr == null) throw new ArgumentNullException("dr");
int drlen = dr.FieldCount;
for (int i = 0; i < drlen; i++)
{
if (dr.IsDBNull(i))
{
continue;
}
string columnName = dr.GetName(i).ToUpper(CultureInfo.InvariantCulture);
switch (columnName)
{
case "BIN":
this._Bin = dr.GetInt32(i);
break;
case "NAME":
this._Name = dr.GetString(i);
break;
case "NAMEDETAIL":
this._NameDetail = dr.GetString(i);
break;
case "SHORTPY":
this._ShortPY = dr.GetString(i);
break;
}
}
}
#endregion
#region 实体属性
public Int32 Bin
{
get { return this._Bin; }
set { this._Bin = value; }
}
public String Name
{
get { return this._Name; }
set { this._Name = value; }
}
public String NameDetail
{
get { return this._NameDetail; }
set { this._NameDetail = value; }
}
public String PinYin
{
get { return this._PinYin; }
set { this._PinYin = value; }
}
public String ShortPY
{
get { return this._ShortPY; }
set { this._ShortPY = value; }
}
#endregion
public override bool Equals(object obj)
{
return this.Name.Equals(obj.ToString()) || this.NameDetail.Equals(obj.ToString()) || this.PinYin.Equals(obj.ToString()) || this.ShortPY.Equals(obj.ToString());
}
public override int GetHashCode()
{
return _Name.GetHashCode() + _NameDetail.GetHashCode() + _PinYin.GetHashCode() + _ShortPY.GetHashCode();
}
}
}
---------------数据库返回省市数据 只加载一次
public static ArrayList CityListView()
{
ArrayList result = new ArrayList();
DbAccess db = new DbAccess();
using (db)
{
DbCommand cmd = db.CreateProcCommand(@"CSCommon_GetCityInfo"); // CSCommon_GetCityInfo 为存储过程 分会省市 ID,名字,拼音简写
using (cmd)
{
IDataReader dr = db.ExecuteReader(cmd);
using (dr)
{
while (dr.Read())
{
EnCity city = new EnCity(dr);
city.PinYin = ToPin.Convert(city.NameDetail);
result.Add(city);
}
}
}
}
return result;
}
-------------------------名字转换拼音全称类
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace Commons
{
public class ToPin
{
#region 数组信息
private static int[] pyValue = new int[]
{ -20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242,
-20230, -20051, -20036, -20032, -20026, -20002, -19990, -19986, -19982,
-19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746,
-19741, -19739, -19728, -19725, -19715, -19540, -19531, -19525, -19515,
-19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270,
-19263, -19261, -19249, -19243, -19242, -19238, -19235, -19227, -19224,
-19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977,
-18961, -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735,
-18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490,
-18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220,
-18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988, -17970,
-17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752,
-17733, -17730, -17721, -17703, -17701, -17697, -17692, -17683, -17676,
-17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202,
-17185, -16983, -16970, -16942, -16915, -16733, -16708, -16706, -16689,
-16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448,
-16433, -16429, -16427, -16423, -16419, -16412, -16407, -16403, -16401,
-16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171,
-16169, -16158, -16155, -15959, -15958, -15944, -15933, -15920, -15915,
-15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659,
-15652, -15640, -15631, -15625, -15454, -15448, -15436, -15435, -15419,
-15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362,
-15183, -15180, -15165, -15158, -15153, -15150, -15149, -15144, -15143,
-15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109,
-14941, -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921,
-14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857,
-14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594,
-14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353, -14345,
-14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125,
-14123, -14122, -14112, -14109, -14099, -14097, -14094, -14092, -14090,
-14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896,
-13894, -13878, -13870, -13859, -13847, -13831, -13658, -13611, -13601,
-13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367,
-13359, -13356, -13343, -13340, -13329, -13326, -13318, -13147, -13138,
-13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060,
-12888, -12875, -12871, -12860, -12858, -12852, -12849, -12838, -12831,
-12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359,
-12346, -12320, -12300, -12120, -12099, -12089, -12074, -12067, -12058,
-12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589,
-11536, -11358, -11340, -11339, -11324, -11303, -11097, -11077, -11067,
-11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018,
-11014, -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587,
-10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309,
-10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254
};
private static string[] pyName = new string[]
{ "A", "Ai", "An", "Ang", "Ao", "Ba", "Bai", "Ban", "Bang", "Bao", "Bei",
"Ben", "Beng", "Bi", "Bian", "Biao", "Bie", "Bin", "Bing", "Bo", "Bu",
"Ba", "Cai", "Can", "Cang", "Cao", "Ce", "Ceng", "Cha", "Chai", "Chan",
"Chang", "Chao", "Che", "Chen", "Cheng", "Chi", "Chong", "Chou", "Chu",
"Chuai", "Chuan", "Chuang", "Chui", "Chun", "Chuo", "Ci", "Cong", "Cou",
"Cu", "Cuan", "Cui", "Cun", "Cuo", "Da", "Dai", "Dan", "Dang", "Dao", "De",
"Deng", "Di", "Dian", "Diao", "Die", "Ding", "Diu", "Dong", "Dou", "Du",
"Duan", "Dui", "Dun", "Duo", "E", "En", "Er", "Fa", "Fan", "Fang", "Fei",
"Fen", "Feng", "Fo", "Fou", "Fu", "Ga", "Gai", "Gan", "Gang", "Gao", "Ge",
"Gei", "Gen", "Geng", "Gong", "Gou", "Gu", "Gua", "Guai", "Guan", "Guang",
"Gui", "Gun", "Guo", "Ha", "Hai", "Han", "Hang", "Hao", "He", "Hei", "Hen",
"Heng", "Hong", "Hou", "Hu", "Hua", "Huai", "Huan", "Huang", "Hui", "Hun",
"Huo", "Ji", "Jia", "Jian", "Jiang", "Jiao", "Jie", "Jin", "Jing", "Jiong",
"Jiu", "Ju", "Juan", "Jue", "Jun", "Ka", "Kai", "Kan", "Kang", "Kao", "Ke",
"Ken", "Keng", "Kong", "Kou", "Ku", "Kua", "Kuai", "Kuan", "Kuang", "Kui",
"Kun", "Kuo", "La", "Lai", "Lan", "Lang", "Lao", "Le", "Lei", "Leng", "Li",
"Lia", "Lian", "Liang", "Liao", "Lie", "Lin", "Ling", "Liu", "Long", "Lou",
"Lu", "Lv", "Luan", "Lue", "Lun", "Luo", "Ma", "Mai", "Man", "Mang", "Mao",
"Me", "Mei", "Men", "Meng", "Mi", "Mian", "Miao", "Mie", "Min", "Ming", "Miu",
"Mo", "Mou", "Mu", "Na", "Nai", "Nan", "Nang", "Nao", "Ne", "Nei", "Nen",
"Neng", "Ni", "Nian", "Niang", "Niao", "Nie", "Nin", "Ning", "Niu", "Nong",
"Nu", "Nv", "Nuan", "Nue", "Nuo", "O", "Ou", "Pa", "Pai", "Pan", "Pang",
"Pao", "Pei", "Pen", "Peng", "Pi", "Pian", "Piao", "Pie", "Pin", "Ping",
"Po", "Pu", "Qi", "Qia", "Qian", "Qiang", "Qiao", "Qie", "Qin", "Qing",
"Qiong", "Qiu", "Qu", "Quan", "Que", "Qun", "Ran", "Rang", "Rao", "Re",
"Ren", "Reng", "Ri", "Rong", "Rou", "Ru", "Ruan", "Rui", "Run", "Ruo",
"Sa", "Sai", "San", "Sang", "Sao", "Se", "Sen", "Seng", "Sha", "Shai",
"Shan", "Shang", "Shao", "She", "Shen", "Sheng", "Shi", "Shou", "Shu",
"Shua", "Shuai", "Shuan", "Shuang", "Shui", "Shun", "Shuo", "Si", "Song",
"Sou", "Su", "Suan", "Sui", "Sun", "Suo", "Ta", "Tai", "Tan", "Tang",
"Tao", "Te", "Teng", "Ti", "Tian", "Tiao", "Tie", "Ting", "Tong", "Tou",
"Tu", "Tuan", "Tui", "Tun", "Tuo", "Wa", "Wai", "Wan", "Wang", "Wei",
"Wen", "Weng", "Wo", "Wu", "Xi", "Xia", "Xian", "Xiang", "Xiao", "Xie",
"Xin", "Xing", "Xiong", "Xiu", "Xu", "Xuan", "Xue", "Xun", "Ya", "Yan",
"Yang", "Yao", "Ye", "Yi", "Yin", "Ying", "Yo", "Yong", "You", "Yu",
"Yuan", "Yue", "Yun", "Za", "Zai", "Zan", "Zang", "Zao", "Ze", "Zei",
"Zen", "Zeng", "Zha", "Zhai", "Zhan", "Zhang", "Zhao", "Zhe", "Zhen",
"Zheng", "Zhi", "Zhong", "Zhou", "Zhu", "Zhua", "Zhuai", "Zhuan",
"Zhuang", "Zhui", "Zhun", "Zhuo", "Zi", "Zong", "Zou", "Zu", "Zuan",
"Zui", "Zun", "Zuo" };
#endregion
#region 方法调用_只有一个参数
public static string Convert(string hzString)
{
return Convert(hzString, 10);
}
#endregion
#region 方法调用_有两个参数
public static string Convert(string hzString, int maxLength)
{
char str = '"';//英文状态双引号处理
if (string.IsNullOrEmpty(hzString))//输入为空
return null;
if (maxLength <= 1)
maxLength = 10;
//字符处理
hzString = hzString.Trim().Replace(" ", "").Replace("?", "_").Replace("\\", "_").Replace("/", "_").Replace(":", "").Replace("*", "").Replace(">", "").Replace("<", "").Replace("?", "").Replace("|", "").Replace("\"", "'").Replace("(", "_").Replace(")", "_").Replace(";", "_");
hzString = hzString.Replace(",", ",").Replace(str.ToString(), "").Replace(str.ToString(), "").Replace(";", "_").Replace("。", "_").Replace("[", "").Replace("]", "").Replace("【", "").Replace("】", "");
hzString = hzString.Replace("{", "").Replace("}", "").Replace("^", "").Replace("&", "_").Replace("=", "").Replace("~", "_").Replace("@", "_").Replace("¥", "");
if (hzString.Length > maxLength)
{
hzString = hzString.Substring(0, maxLength);
}
Regex regex = new Regex(@"([a-zA-Z0-9\._]+)", RegexOptions.IgnoreCase);
if (regex.IsMatch(hzString))
{
if (hzString.Equals(regex.Match(hzString).Groups[1].Value, StringComparison.OrdinalIgnoreCase))
{
return hzString;
}
}
// 匹配中文字符
regex = new Regex("^[\u4e00-\u9fa5]$");
byte[] array = new byte[2];
string pyString = "";
int chrAsc = 0;
int i1 = 0;
int i2 = 0;
char[] noWChar = hzString.ToCharArray();
for (int j = 0; j < noWChar.Length; j++)
{// 中文字符
if (regex.IsMatch(noWChar[j].ToString()))
{
array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
i1 = (short)(array[0]);
i2 = (short)(array[1]);
chrAsc = i1 * 256 + i2 - 65536;
if (chrAsc > 0 && chrAsc < 160)
{
pyString += noWChar[j];
}
else
{
// 修正部分文字
if (chrAsc == -9254) // 修正"圳"字
pyString += "Zhen";
else
{
for (int i = (pyValue.Length - 1); i >= 0; i--)
{
if (pyValue[i] <= chrAsc)
{
pyString += pyName[i];
break;
}
}
}
}
}
// 非中文字符
else
{
pyString += noWChar[j].ToString();
}
}
return pyString;
}
#endregion
}
}
-----------------------效果
调用 ---------------
CityTextBox cityStart = new CityTextBox();
cityStart.Name = "txtStartCity";
cityStart.Width = 120;
cityStart.Height = 21;
cityStart.Left = lbStart.Right + 20;
cityStart.Top = lbStart.Top - 5;
cityStart.ChoicOnly = true;
groupBox1.Controls.Add(cityStart);
cityStart.PromptForeColor = Color.Gray;
cityStart.PromptBackColor = Color.WhiteSmoke;
设置数据源 cityStart.ChoiceArray = arraylist(EnCity);