程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> csharp:datagridview enter Half Width and Full Width characters,csharpdatagridview

csharp:datagridview enter Half Width and Full Width characters,csharpdatagridview

編輯:C#入門知識

csharp:datagridview enter Half Width and Full Width characters,csharpdatagridview


        /// <summary>
        /// 全角
        /// </summary>
        /// <param name="unicodeString"></param>
        /// <returns></returns>
        public static string FullWidthConvertor(string unicodeString)
        {

            StringBuilder sb = new StringBuilder(256);
            LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_FULLWIDTH, unicodeString, -1, sb, sb.Capacity);
            return sb.ToString();

        }
        /// <summary>
        /// 半角
        /// </summary>
        /// <param name="unicodeString"></param>
        /// <returns></returns>
        public static string HalfWidthConvertor(string unicodeString)
        {

            StringBuilder sb = new StringBuilder(256);
            LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_HALFWIDTH, unicodeString, -1, sb, sb.Capacity);
            return sb.ToString();


        }
        private const uint LCMAP_FULLWIDTH = 0x00800000;
        private const uint LOCALE_SYSTEM_DEFAULT = 0x0800;
        private const uint LCMAP_HALFWIDTH = 0x00400000;

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        public static extern int LCMapString(uint Locale, uint dwMapFlags, string lpSrcStr, int cchSrc, StringBuilder lpDestStr, int cchDest);



  /// <summary>
        /// DataGridView單位格輸入全角轉半角
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dridViewRowNumberNo_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            GridViewRowNumberNo dt = sender as GridViewRowNumberNo;
            //獲取列序號
            int columnIndex = dt.CurrentCell.ColumnIndex;
            //單位格轉化成文本框
            TextBox tb = e.Control as TextBox;
            //委托單位格KeyPress事務
            tb.KeyPress += new KeyPressEventHandler(geovindu_KeyPress);
        }
        /// <summary>
        /// 單位格KeyPress事務
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void geovindu_KeyPress(object sender, KeyPressEventArgs e)
        {
            Control ctl = sender as Control;
            if (ctl == null)
                return;
            if (ctl.ImeMode != ImeMode.Hangul)
                ctl.ImeMode = ImeMode.Hangul;
            //全角轉半角
            if (e.KeyChar >= 65296 && e.KeyChar <= 65305)
            {
                e.KeyChar -= Convert.ToChar(65248);
            }

        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved