C# Winform 支援Hex與ASCII輸入和切換的文字框
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
namespace LeafSoft.Units
{
/// <summary>
/// Hex/ASCII輸入文字框
/// 作者:一葉知秋
/// 日期:2013年7月11日
/// 可輸入Hex,ASCII
/// 可切換顯示Hex與ASCII的輸入文字框
/// Hex輸入時可自動每2個字元之間新增空格
/// </summary>
public class BytesBox:TextBox
{
ContextMenuStrip CMenu = new ContextMenuStrip();
ToolStripMenuItem CM_Type = new ToolStripMenuItem();
ToolStripMenuItem CM_Clear = new ToolStripMenuItem();
public BytesBox()
{
#region 快捷選單
CM_Type.Name = "CM_Type";
CM_Type.Size = new System.Drawing.Size(127, 22);
CM_Type.Text = "ASCII";
CM_Type.Click += new System.EventHandler(CM_Type_Click);
CM_Clear.Name = "CM_Clear";
CM_Clear.Size = new System.Drawing.Size(127, 22);
CM_Clear.Text = "清空";
CM_Clear.Click += new System.EventHandler(CM_Clear_Click);
CMenu.Name = "CMenu";
CMenu.ShowImageMargin = false;
CMenu.Size = new System.Drawing.Size(128, 48);
CMenu.Items.AddRange(new ToolStripItem[] {
CM_Type,CM_Clear});
this.ContextMenuStrip = CMenu;
#endregion
}
#region 屬性
bool _IsHex = true;
[Description("True:Hex;False:ASCII"), Category("輸入格式設定")]
public bool IsHex
{
set {
_IsHex = value;
if (_IsHex)
{
CM_Type.Text = "ASCII";
}
else
{
CM_Type.Text = "Hex";
}
}
get {
return _IsHex;
}
}
#endregion
#region 選單操作
/// <summary>
/// HEX/ASCII 格式切換
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CM_Type_Click(object sender, EventArgs e)
{
if (IsHex)
{//切換到ASCII格式
IsHex = false;
if (this.Text.Length > 0)
{
string[] HexStr = this.Text.Trim().Split(' ');
byte[] data = new byte[HexStr.Length];
for (int i = 0; i < HexStr.Length; i++)
{
data[i] = (byte)(Convert.ToInt32(HexStr[i], 16));
}
this.Text = new ASCIIEncoding().GetString(data);
}
}
else
{//切換到Hex格式
IsHex = true;
if (this.Text.Length > 0)
{
byte[] data = new ASCIIEncoding().GetBytes(this.Text.Trim());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.AppendFormat("{0:x2}", data[i]);
}
this.Text = sb.ToString();
}
}
}
/// <summary>
/// 清空資料
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CM_Clear_Click(object sender, EventArgs e)
{
this.Text = "";
}
#endregion
#region 輸入控制
protected override void OnTextChanged(EventArgs e)
{
if (_IsHex)
{//Hex輸入
string Content = this.Text.Replace(" ", "");//獲取去除空格後的字元內容
int SpaceCount = Content.Length / 2;
int StartIndex = 2;
for (int i = 0; i < SpaceCount; i++)
{
Content = Content.Insert(StartIndex, " ");
StartIndex = StartIndex + 3;
}
this.Text = Content.TrimEnd().ToUpper();
}
this.SelectionStart = this.Text.Length;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (_IsHex)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9')//數字0-9鍵
|| (e.KeyChar >= 'A' && e.KeyChar <= 'F')//字母A-F
|| (e.KeyChar >= 'a' && e.KeyChar <= 'f')//字母a-f
|| e.KeyChar == 0x08//退格鍵
|| e.KeyChar == 0x03//拷貝
|| e.KeyChar == 0x18)//剪下
{
e.Handled = false;
return;
}
}
else
{
if ((e.KeyChar >= 0x20 && e.KeyChar <= 0x7E)
|| e.KeyChar == 0x08//退格鍵
|| e.KeyChar == 0x0D//Enter鍵
|| e.KeyChar == 0x03//拷貝
|| e.KeyChar == 0x18)//剪下
{
e.Handled = false;
return;
}
}
if (e.KeyChar == 0x16)//貼上
{//貼上前資料格式檢查
if (CheckPaste())
{
e.Handled = false;
return;
}
}
e.Handled = true;
}
/// <summary>
/// 貼上資料格式檢查
/// </summary>
/// <returns></returns>
private bool CheckPaste()
{
try
{
char[] PasteChar = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString().ToCharArray();
if (_IsHex)
{
foreach (char data in PasteChar)
{
if (!((data >= '0' && data <= '9')//數字0-9鍵
|| (data >= 'A' && data <= 'F')//字母A-F
|| (data >= 'a' && data <= 'f')//字母a-f
|| data == 0x20))//空格
{
MessageBox.Show("貼上資料含有非法字元,只能包含數字0-9,大寫英文字母A-F,小寫英文字母a-f以及空格!", "非法的貼上", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
else
{
foreach (char data in PasteChar)
{
if (!((data >= 0x20 && data <= 0x7E)
|| data == 0x0A
|| data == 0x0D))//Enter鍵
{
MessageBox.Show("貼上資料含有非法字元,只能包含ASCII碼字元!", "非法的貼上", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
#endregion
#region 公共方法
/// <summary>
/// 獲取命令物件
/// </summary>
/// <returns></returns>
public Model.Command GetCMD()
{
try
{
if (this.Text.Trim() == string.Empty)
{
MessageBox.Show("不允許內容為空!");
return null;
}
Model.Command Cmd = new Model.Command();
Cmd.IsHex = _IsHex;
if (Cmd.IsHex)
{//Hex
string[] HexStr = this.Text.Trim().Split(' ');
Cmd.DataBytes = new byte[HexStr.Length];
for (int i = 0; i < HexStr.Length; i++)
{
Cmd.DataBytes[i] = (byte)(Convert.ToInt32(HexStr[i], 16));
}
}
else
{ //ASCII
Cmd.DataBytes = new ASCIIEncoding().GetBytes(this.Text.Trim());
}
return Cmd;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
/// <summary>
/// 設定命令物件
/// </summary>
/// <param name="Cmd"></param>
public void SetCMD(Model.Command Cmd)
{
try
{
this.IsHex = Cmd.IsHex;
if (this.IsHex)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Cmd.DataBytes.Length; i++)
{
sb.AppendFormat("{0:x2}", Cmd.DataBytes[i]);
}
this.Text = sb.ToString();
}
else
{
this.Text = new ASCIIEncoding().GetString(Cmd.DataBytes);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Model
{
/// <summary>
/// 命令物件
/// </summary>
public class Command
{
bool _IsHex = true;
byte[] _DataBytes = null;
/// <summary>
/// 是否16進位制資料
/// </summary>
public bool IsHex
{
set {
_IsHex = value;
}
get {
return _IsHex;
}
}
/// <summary>
/// byte資料
/// </summary>
public byte[] DataBytes
{
set
{
_DataBytes = value;
}
get
{
return _DataBytes;
}
}
}
}
相關文章
- python hex轉ascii轉換Python程式碼的簡單方法PythonASCII
- 文字框輸入文字倒計詳解
- 文字框限制輸入字數的JSJS
- 文字框文字輸入數量倒計效果
- JavaScript 文字框只能輸入數字JavaScript
- 文字框限制輸入字數效果
- 輸入框換行問題
- Bootstrap 支援的一個特性—輸入框組boot
- SwitchKey 1.1.3 (自動切換輸入法利器)支援m1
- Web聊天工具的富文字輸入框Web
- win10輸入法切換方法_win10輸入法怎麼切換Win10
- w10輸入法切換怎麼設定_w10輸入法切換的方法
- 記錄---實現一個支援@的輸入框
- 切換輸入法卡死怎麼辦_win10切換輸入法就卡死的解決方法Win10
- Objective-C:多行文字輸入框(UITextView)ObjectUITextView
- Axure之解決文字框無法輸入
- 實現高度“聽話”的多行文字輸入框
- html input文字輸入框的一些總結HTML
- win10修改輸入法切換方式 win10修改輸入法切換快捷鍵Win10
- Qt 實現文字輸入框,帶字數限制QT
- C# 的輸入輸出,(同一行/換行)C#
- 搜狗輸入法切換到半形
- 輸入一個ASCII碼,輸出對應的字元ASCII字元
- 輸入框
- 利用 Angular Directive 和 @HostBinding 實現輸入文字框隨著鍵盤輸入自動變色效果Angular
- win10怎麼更改輸入法切換鍵_win10如何改輸入法切換快捷鍵Win10
- 文字框填內容寫達到指定長度自動切換
- JavaScript實時計算輸入文字框字元數量JavaScript字元
- imemode 控制輸入法,控制輸入框的輸入法
- QT學習筆記(三)單行文字輸入框與自動補全QT筆記
- SwitchKey 1.1.3最新版 (自動輸入法切換利器)支援M1晶片晶片
- 從一次輸入框無法輸入的bug,談如何限制輸入框輸入型別型別
- Flutter 密碼輸入框 驗證碼輸入框Flutter密碼
- 【新特性速遞】數字輸入框的字首和字尾(位於輸入框內部)
- 瞭解下C# 檔案的輸入與輸出C#
- C#中EXCEL的輸入和倒出C#Excel
- C# 設定Word文字框中的文字旋轉方向C#
- react輸入框輸入中文bugReact
- 切換輸入法神器:自動切換輸入法專業版 for Mac v2.1.3中文啟用版Mac