F
F
Frip2019-04-05 22:46:13
C++ / C#
Frip, 2019-04-05 22:46:13

The KeyPress event of the TextBox. How to make one format for different TextBoxes?

There are many TextBoxes that require only numbers to be entered and no other characters.
On the Internet, I found this code that almost completely satisfies my needs

spoiler
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Правильными символами считаются цифры,
            // запятая, <Enter> и <Backspace>.
            // Будем считать правильным символом
            // также точку, на заменим ее запятой.
            // Остальные символы запрещены.
            // Чтобы запрещенный символ не отображался 
            // в поле редактирования,присвоим 
            // значение true свойству Handled параметра e
 
            if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
            {
                // цифра
                return;
            }
 
            if (e.KeyChar == '.')
            {
                // точку заменим запятой
                e.KeyChar = ',';
            }
 
            if (e.KeyChar == ',')
            {
                if (textBox1.Text.IndexOf(',') != -1)
                {
                    // запятая уже есть в поле редактирования
                    e.Handled = true;
                }
                return;
            }
 
            if (  Char.IsControl (e.KeyChar) )
            {
                // <Enter>, <Backspace>, <Esc>
                if ( e.KeyChar == (char) Keys.Enter)
                    // нажата клавиша <Enter>
                    // установить курсор на кнопку OK
                    button1.Focus(); 
                return;
            }
 
            // остальные символы запрещены
            e.Handled = true;
        }

but at the same time, I would like to be able to pass this function to the TextBox in which the input is made, and button1 , to which the focus should shift (so as not to create separate KeyPress events for each TextBox in the program)
But I don’t know how I can do this do, and make it easy to specify an event through VisualStudio

Answer the question

In order to leave comments, you need to log in

4 answer(s)
#
#, 2019-04-06
@mrFrip

1 - https://duckduckgo.com/?q=winforms+TextBox+validat...
2 - https://duckduckgo.com/?q=wpf+TextBox+validation&t...
choose the right upd and yes, there is no you can enter only numbers, you can set formats for only positive numbers, the number of decimal places, monetary values, dates and / or times in the desired format, emails, urls, full name, etc.

for studying
I almost forgot the most
upd 2 - one validator can easily be hung on a pack of the same type of input fields, practice (study the properties window in the designer more carefully, if it's winforms)

L
LiptonOlolo, 2019-04-05
@LiptonOlolo

Why invent crutches?

I
iBird Rose, 2017-01-29
@Emmet1

UPD:
use flex for .item

.item {
    display: flex;
    align-items: center;
    justify-content: center;
}

https://jsfiddle.net/uvpgwgan/2/
or with translate:
.item img{
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}

https://jsfiddle.net/uvpgwgan/3/

Y
ya_yshel_rabotati_v_teleg, 2017-01-29
@ya_yshel_rabotati_v_teleg

.item{
   background-size: cover;
   background-position: 50%;
}

<div class="item" style="background-image:url(img.png);">
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question