Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

textbox kontrola unosa

[es] :: .NET :: textbox kontrola unosa

[ Pregleda: 1189 | Odgovora: 6 ]

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

west_herc

Član broj: 103549
Poruke: 142
*.tel.net.ba.



Profil

icon textbox kontrola unosa28.11.2006. u 15:21

Na koji način kontrolirate TextBox. Tj ako unesem float number u textbox ili ako unesem bilo koji broj u textbox da on javi grešku da se tu može unijet samo text. Naravno i obratno.

Hvala
28.11.2006. u 15:21 

negyxo
Aleksandar Perkuchin
unknown

Član broj: 29751
Poruke: 635
*.sksyu.net.



Profil

icon Re: textbox kontrola unosa28.11.2006. u 16:18
Code:

public class TextBoxEx : TextBox
    {
        public enum TextBoxExFilterType : byte
        {
            NoFilter,
            WholeNumber,
            DecimalNumber,
            PositiveWholeNumber,
            PositiveDecimalNumber,
            NegativeWholeNumber,
            NegativeDecimalNumber,
            CustomFilter
        }

        bool _changeInPorgress = false;
        byte _decimalPlaces = 255;
        string _currentFilterPattern = string.Empty;
        string _customFilterPattern = string.Empty;        
        TextBoxExFilterType _filterType = TextBoxExFilterType.NoFilter;

        public TextBoxEx()
        {            
        }

        protected override void OnEnter(EventArgs e)
        {
            this.SelectionStart = 0;
            this.SelectionLength = this.Text.Length;
            
            base.OnEnter(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            if (_filterType == TextBoxExFilterType.NoFilter)
                goto End;

            if (_changeInPorgress)
            {                
                _changeInPorgress = false;
                goto End;
            }

            Regex regex = new Regex(_currentFilterPattern);
            string match = regex.Match(this.Text).Value;
            if (match != this.Text)            
            {
                _changeInPorgress = true;
                this.Text = match;            
            }

            End:
                base.OnTextChanged(e);
        }

        // ovo je da se ne bi cuo onaj glubi beep kada se pritisne enter
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Enter)
                e.SuppressKeyPress = true;
        }


        protected override void OnKeyPress(KeyPressEventArgs e)
        {            
            if (e.KeyChar == '\b' || _filterType == TextBoxExFilterType.NoFilter)
                goto End;

            StringBuilder sb = new StringBuilder();
            sb.Append(this.Text.Substring(0, this.SelectionStart));
            sb.Append(e.KeyChar);
            sb.Append(this.Text.Substring(this.SelectionStart + this.SelectionLength));
            string text = sb.ToString();

            Regex regex = new Regex(_currentFilterPattern);

            if (regex.Match(text).Value != text)
            {
                e.Handled = true;
                System.Media.SystemSounds.Beep.Play();
            }
            else
                _changeInPorgress = true;

            End:
                base.OnKeyPress(e);
        }        

        [Category("Filter")]
        [DefaultValue(TextBoxEx.TextBoxExFilterType.NoFilter)]
        public TextBoxExFilterType FilterType
        {
            get
            {
                return _filterType;
            }
            set
            {
                _filterType = value;
                _currentFilterPattern = GetFilterPattern(_filterType, _decimalPlaces);
                
                //refresh
                Regex regex = new Regex(_currentFilterPattern);
                this.Text = regex.Match(this.Text).Value;
            }
        }

        [Category("Filter")]
        [DefaultValue("")]        
        public string CustomFilter
        {
            get
            {
                return _customFilterPattern;
            }
            set
            {
                _customFilterPattern = value;
                if (_filterType == TextBoxExFilterType.CustomFilter)
                {
                    _currentFilterPattern = _customFilterPattern;
                    //refresh
                    Regex regex = new Regex(_currentFilterPattern);
                    this.Text = regex.Match(this.Text).Value;
                }
            }
        }

        [Category("Filter")]
        [DefaultValue((byte)255)]        
        public byte DecimaPlaces
        {
            get
            {
                return _decimalPlaces;
            }
            set
            {
                if (value < 1)
                {
                    throw new Exception("Decimal places can not be smaller then 1.");
                }
                _decimalPlaces = value;
                _currentFilterPattern = GetFilterPattern(_filterType, _decimalPlaces);
                
                //refresh
                Regex regex = new Regex(_currentFilterPattern);
                this.Text = regex.Match(this.Text).Value;
            }    
        }

        string GetFilterPattern(TextBoxExFilterType filter, byte decimalPlaces)
        {
            NumberFormatInfo nfi = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat;

            switch (filter)
            {
                case TextBoxExFilterType.NoFilter:
                    return "";

                case TextBoxExFilterType.WholeNumber:
                    return @"[" + nfi.NegativeSign + @"]?\d*";

                case TextBoxExFilterType.DecimalNumber:
                    return @"[" + nfi.NegativeSign + @"]?\d*[" + nfi.NumberDecimalSeparator + @"]?\d{0," + decimalPlaces.ToString() + "}";

                case TextBoxExFilterType.NegativeWholeNumber:
                    return @"[" + nfi.NegativeSign + @"]\d*";

                case TextBoxExFilterType.NegativeDecimalNumber:
                    return @"[" + nfi.NegativeSign + @"]\d*[" + nfi.NumberDecimalSeparator + @"]?\d{0," + decimalPlaces.ToString() + "}";

                case TextBoxExFilterType.PositiveWholeNumber:
                    return @"\d*";

                case TextBoxExFilterType.PositiveDecimalNumber:
                    return @"\d*[" + nfi.NumberDecimalSeparator + @"]?\d{0," + decimalPlaces.ToString() + "}";

                case TextBoxExFilterType.CustomFilter:
                    return _customFilterPattern;
            }

            return "";
        }
    }

Evo ti cela kontrola.
Only Time Will Tell
28.11.2006. u 16:18 

west_herc

Član broj: 103549
Poruke: 142
*.tel.net.ba.



Profil

icon Re: textbox kontrola unosa28.11.2006. u 20:25
ne znam za ovo, ali mislim da postoji nešto lakše za korištenje. MASKED TEXTBOX, da se izbjegne logika programiranja uvjeta nego je već sve urađeno.
28.11.2006. u 20:25 

colke77
Dragan Colić
ERP Consultant
Beograd

Član broj: 74309
Poruke: 75
195.252.107.*



Profil

icon Re: textbox kontrola unosa29.11.2006. u 09:03
Ja sam napravio custom kontroe koje su nasledjene iz TextBox kontrole, za ove potrebe
Idemo dalje
29.11.2006. u 09:03 

sstanko78
Stanislav Šimunec
Novi Sad

Član broj: 19139
Poruke: 302
*.tippnet.co.yu.



Profil

icon Re: textbox kontrola unosa29.11.2006. u 09:32
ili vec gotove TextBoxKontrole koje imaju sve te osobine sto trazis :

http://www.codeproject.com/cs/.../ValidatingTextBoxControls.asp

i to sve sa source-om
29.11.2006. u 09:32 

bobanM
Boban Mikšin
Levi9 Global Sourcing
Novi Sad

Član broj: 61491
Poruke: 39
212.200.221.*



Profil

icon Re: textbox kontrola unosa29.11.2006. u 21:49
Ako imas potrebu da cesto koristis ovu proveru prilikom unosa, onda je najbolje da poslusas savet od negyxo.

Ako ti treba resenje na brzinu onda west_herc ti je dao super brzo resenje.

Jedino bih predlozio da se u celu pricu iskoristi OnValidating event zajedno sa ErrorProvider-om. Recimo da u datom dogadjaju proveris da li je uneti text validan, ako nije dodaj error text u error provider. Sve to stavis u try catch blok i time na brzinu dobijes resenje koje "lepo izgleda". Mada sam vise za resenje koje je dao negyxo.
nije lepo da budem lud a da to ne znam
29.11.2006. u 21:49 

logic_rabbit
Radenko Zec
banjaluka

Član broj: 74458
Poruke: 181
*.lanaco.com.

Sajt: www.developers.ba


Profil

icon Re: textbox kontrola unosa30.11.2006. u 14:04
Ja to odradjujem na validate textboxa.
logic_rabbit (MCAD,MCSD,MCT,MCTS-Windows development,MCPD)
www.sqlpass.rs.ba
30.11.2006. u 14:04 

[es] :: .NET :: textbox kontrola unosa

[ Pregleda: 1189 | Odgovora: 6 ]

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.