csharp:datagridview enter Half Width and Full Width characters

  /// <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);

          }

 

      }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值