最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)Winform小數(shù)字鍵盤模擬器

 更新時間:2021年11月14日 15:57:37   作者:中洲少年  
本文主要介紹了C#實(shí)現(xiàn)Winform小數(shù)字鍵盤模擬器,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

文章開始之前,先看一下效果圖,看是不是您正所需要的:

一、構(gòu)建計(jì)算器的界面

要構(gòu)建出一個好看點(diǎn)的計(jì)算器界面,還是需要頗費(fèi)些小心思的,我做這個的時候,也花了兩三個小時的時間構(gòu)建這個界面。

其主要的使用控制是TableLayoutPanel控件。

另外一個小難點(diǎn)則在于內(nèi)容控件Textbox的顯示,要讓文字垂直居中,在沒有重寫Textbox控件的情況下要達(dá)到這個效果,也是花了些小心思。

其它的界面則沒有什么的。至于加減號嘛,則用輸入法的特殊符號即可。

二、構(gòu)建控件的開放屬性

一共開放了3個屬性,不夠自己加。這3個如下,看注釋應(yīng)該能懂:

/// <summary>
/// 可接受的最小值,最小為-3.402823E+38
/// </summary>
[Browsable(true)]
[Category("Zhongzhou")]
[DefaultValue(0)]
[Description("可接受的最小值,最小為-3.402823E+38")]
public float Min { get; set; } = 0;
 
/// <summary>
/// 可接受的最大值,最大為3.402823E+38
/// </summary>
[Browsable(true)]
[Category("Zhongzhou")]
[DefaultValue(0)]
[Description("可接受的最大值,最大為3.402823E+38")]
public float Max { get; set; } = 0;
 
/// <summary>
/// 設(shè)置小數(shù)點(diǎn)的精度位數(shù),默認(rèn)為2位小數(shù)點(diǎn)
/// </summary>
[Browsable(true)]
[Category("Zhongzhou")]
[DefaultValue(2)]
[Description("設(shè)置小數(shù)點(diǎn)的精度位數(shù),默認(rèn)為2位小數(shù)點(diǎn)")]
public int Precision { get; set; } = 2;

三、控件鍵盤輸入

我們的目的是讓小鍵盤來輸入數(shù)字,所以需要禁止實(shí)體鍵盤輸入文字字母等信息,以及小數(shù)字點(diǎn)最多只能出現(xiàn)一次,具體邏輯如下:

/// <summary>
/// 當(dāng)使用實(shí)物鍵盤輸入文本內(nèi)容時觸發(fā)
/// </summary>
/// <param name="e"></param>
private void OnKeyPressed(KeyPressEventArgs e)
{
    //13表示回車
    if (e.KeyChar == 13)
    {
        this.OnEntered();
        e.Handled = true;
        return;
    }
    //48代表0,57代表9,8代表空格,46代表小數(shù)點(diǎn)
    if ((e.KeyChar < 48 || e.KeyChar >= 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
    {
        e.Handled = true;
        return;
    }
 
    //判斷多次輸入小數(shù)點(diǎn),僅允許出現(xiàn)1次小數(shù)點(diǎn)
    if (e.KeyChar == 46)
    {
        this.PointHandle();
        this.SetContentFocus();
        e.Handled = true;
        return;
    }
}
 
/// <summary>
/// 處理小數(shù)點(diǎn)
/// </summary>
/// <returns><see langword="true"/>表示處理成功,<see langword="false"/>表示未處理</returns>
private bool PointHandle()
{
    string content = this.ContentTextBox.Text;
    if (content.IndexOf('.') != -1)
    {
        return false;
    }
 
    if (string.IsNullOrEmpty(content))
    {
        this.SetContent("0.");
        return true;
    }
 
    //取光標(biāo)位置
    int index = this.ContentTextBox.SelectionStart;
    string str = this.ContentTextBox.Text.Substring(0, index);
    if (str == "+" || str == "-")
    {
        return this.SetContent(string.Join(string.Empty, str, "0.", this.ContentTextBox.Text.Substring(index, this.ContentTextBox.Text.Length - index)));
    }
 
    return this.SetContent(string.Join(string.Empty, str, ".", this.ContentTextBox.Text.Substring(index, this.ContentTextBox.Text.Length - index)));
}

四、讓文本框處理焦點(diǎn)狀態(tài)以及光標(biāo)位置的處理

光標(biāo)位置,需要特殊處理的,默認(rèn)參數(shù)cursorPosition=-1時,光標(biāo)位置始終移到最末尾處。但是有些情況,比如你要讓光標(biāo)在數(shù)字中間刪除幾個數(shù)字或者添加幾個數(shù)字,就不能讓光標(biāo)自動跑到最末尾處了。

/// <summary>
/// 設(shè)置新值
/// </summary>
/// <param name="newContent">表示新值</param>
private bool SetContent(string newContent)
{
    int precision = this.Precision;
 
    if (string.IsNullOrEmpty(newContent))
    {
        this.ContentTextBox.Text = string.Empty;
        return true;
    }
 
    var scheme = newContent.Split('.');
    if (scheme.Length == 2)
    {
        var realPrecision = scheme[1].Length;
        if (realPrecision > precision)
        {
            return false;
        }
    }
 
    this.ContentTextBox.Text = newContent;
    return true;
}

五、實(shí)現(xiàn)退格、清除內(nèi)容的功能

 
/// <summary>
/// 清除內(nèi)容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ClearButton_Click(object sender, EventArgs e)
{
    this.SetContent(string.Empty);
    this.SetContentFocus();
}
 
/// <summary>
/// 退格內(nèi)容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BackButton_Click(object sender, EventArgs e)
{
    //取光標(biāo)位置
    int index = this.ContentTextBox.SelectionStart;
    //剪切內(nèi)容
    string cutStr = this.ContentTextBox.Text.Substring(0, index);
    //剩余內(nèi)容
    string remainStr = this.ContentTextBox.Text.Substring(index, this.ContentTextBox.Text.Length - index);
    int position = this.SetContent(string.Join(string.Empty, cutStr.Substring(0, cutStr.Length - 1), remainStr)) ? index - 1 : index;
    this.SetContentFocus(position);
}

六、實(shí)現(xiàn)Enter確認(rèn)得到結(jié)果的功能

原理是通過事件來實(shí)現(xiàn)的。代碼如下:

/// <summary>
/// 當(dāng)按下回車按鈕時的事件委托
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void EnteredEventHandler(object sender, float e);
 
/// <summary>
/// 當(dāng)按下回車按鈕時的事件
/// </summary>
public event EnteredEventHandler Entered;
 
/// <summary>
/// 當(dāng)迷你小鍵盤按下回車時觸發(fā)事件
/// </summary>
protected virtual void OnEntered()
{
    float min = this.Min;
    float max = this.Max;
    var value = string.IsNullOrEmpty(this.ContentTextBox.Text) ? 0 : Convert.ToSingle(this.ContentTextBox.Text);
    if (max != 0 && value > max)
    {
        MessageBox.Show("值不在最大范圍內(nèi)", "提示");
        return;
    }
    if (min != 0 && value < min)
    {
        MessageBox.Show("值不在最小范圍內(nèi)", "提示");
        return;
    }
 
    this.Entered?.Invoke(this, value);
}
 
/// <inheritdoc cref="OnEntered"/>
private void EnterButton_Click(object sender, EventArgs e)
{
    this.OnEntered();
    this.SetContentFocus();
}

到此這篇關(guān)于C#實(shí)現(xiàn)Winform小數(shù)字鍵盤模擬器的文章就介紹到這了,更多相關(guān)C# Winform數(shù)字鍵盤模擬器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

隆尧县| 塘沽区| 兴和县| 黄大仙区| 平远县| 安龙县| 清新县| 若尔盖县| 宁安市| 尖扎县| 新宾| 竹山县| 荔浦县| 达州市| 乡城县| 阳春市| 合水县| 永川市| 铜鼓县| 蓬安县| 手游| 丹棱县| 拉萨市| 广宁县| 长春市| 海安县| 龙里县| 红原县| 鄱阳县| 简阳市| 濮阳市| 宝鸡市| 麻城市| 囊谦县| 新密市| 汉中市| 南投市| 三门县| 桦甸市| 策勒县| 濉溪县|