| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Common
- {
- public class ComBoxHelper
- {
- public static void ChangeString(object sender)
- {
- if (sender is TextBox)
- {
- TextBox tb = (TextBox)sender;
- for (int i = 0; i < tb.Text.Length; i++)
- {
- int isChange = 0;
- char newChar = FullCodeToHalfCode(tb.Text[i], ref isChange);
- if (isChange == 1)
- {
- tb.Text = tb.Text.Replace(tb.Text[i], newChar);
- tb.SelectionStart = i + 1;
- }
- }
- }
- else if (sender is ComboBox)
- {
- ComboBox cb = (ComboBox)sender;
- for (int i = 0; i < cb.Text.Length; i++)
- {
- int isChange = 0;
- char newChar = FullCodeToHalfCode(cb.Text[i], ref isChange);
- if (isChange == 1)
- {
- cb.Text = cb.Text.Replace(cb.Text[i], newChar);
- cb.SelectionStart = i + 1;
- }
- }
- }
- }
- public static char FullCodeToHalfCode(char c, ref int isChange)
- {
- //得到c的编码
- byte[] bytes = System.Text.Encoding.Unicode.GetBytes(c.ToString());
- int H = Convert.ToInt32(bytes[1]);
- int L = Convert.ToInt32(bytes[0]);
- //得到unicode编码
- int value = H * 256 + L;
- //是全角
- if (value >= 65281 && value <= 65374)
- {
- int halfvalue = value - 65248;//65248是全半角间的差值。
- byte halfL = Convert.ToByte(halfvalue);
- bytes[0] = halfL;
- bytes[1] = 0;
- isChange = 1;
- }
- else if (value == 12288)
- {
- int halfvalue = 32;
- byte halfL = Convert.ToByte(halfvalue);
- bytes[0] = halfL;
- bytes[1] = 0;
- isChange = 1;
- }
- else
- {
- isChange = 0;
- return c;
- }
- //将bytes转换成字符
- string ret = System.Text.Encoding.Unicode.GetString(bytes);
- return Convert.ToChar(ret);
- }
- }
- }
|