簡體   English   中英

如何僅將數字從蒙版文本框復制到標簽?

[英]How to copy only numbers from masked textbox to a label?

我制作了一個帶掩碼的文本框,用於使用掩碼(999)000-0000保存數字,我只想在標簽中顯示數字,但是這樣做時,它還會復制括號和線條。
我知道它會復制所有文本。 我怎樣才能只復制不帶掩碼輸入的數字? (Windows窗體)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = maskedTextBox1.Text;
        }
    }
}

一個解決方案是設置TextMaskFormatExcludePromptAndLiterals只是閱讀它的前值:

maskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
Console.WriteLine(maskedTextBox1.Text); 
//will print 3123 when value in the mask textbox is (31) 23 for mask (00) 00

然后將此格式設置回去:

maskedTextBox1.TextMaskFormat = MaskFormat.IncludeLiterals;

即使您不會將格式設置回IncludeLiterals ,UI控件仍然會顯示帶遮罩的文本(31) 23並將照常工作。 如果您的其他邏輯依賴於掩碼的“ Text字段,則可以執行此操作。

因此,如果沒有這種依賴性,則可以在Visual Studio設計器中的maskedTextBox1屬性窗口中設置此值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM