В предыдущем примере было показано, как с помощью нажатия клавиши, например Enter, переходить к нужному элементу. Раздражение вызывал системный звук. В этом примере мы удалим этот звук.
Текстовое поле имеет особенность издавать системный звук, при нажатии клавиши, когда оно в фокусе.
Что бы убрать звук, нужно перехватить нажатие этой клавиши.
Текстовое поле имеет особенность издавать системный звук, при нажатии клавиши, когда оно в фокусе.
Что бы убрать звук, нужно перехватить нажатие этой клавиши.
Counter.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;classCounter {-
ushortshowStart; -
ushortshowEnd; -
decimalprice; -
publicCounter(stringarg0,stringarg1,stringarg2) { -
ushort.TryParse(arg0,outshowStart); -
ushort.TryParse(arg1,outshowEnd); -
decimal.TryParse(arg2,outprice); -
} -
publicushortResultShow() { -
return(ushort)(showEnd - showStart); -
} -
publicdecimalSum() { -
returnMath.Round((decimal)(ResultShow() * price) , 2); -
} }
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;classCounter {-
ushortshowStart; -
ushortshowEnd; -
decimalprice; -
publicCounter(stringarg0,stringarg1,stringarg2) { -
ushort.TryParse(arg0,outshowStart); -
ushort.TryParse(arg1,outshowEnd); -
decimal.TryParse(arg2,outprice); -
} -
publicushortResultShow() { -
return(ushort)(showEnd - showStart); -
} -
publicdecimalSum() { -
returnMath.Round((decimal)(ResultShow() * price) , 2); -
} }
Form1.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0027 {-
publicpartialclassForm1 : Form { -
publicForm1() { -
InitializeComponent(); -
} -
//кнопка Расчитать вычисляет сумму и активирует текстовое поле Сумма -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
Counter C =newCounter(textBox1.Text, textBox2.Text, textBox3.Text); -
textBox4.Text = C.Sum().ToString(); -
textBox4.Select(); -
//textBox4.Focus(); -
} -
//кнопка Очистить очищает все текстовые поля и активирует текстовое поле Начальные показания -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//кнопка очищает все текстовые поля -
foreach(Control iinControls) { -
if(i.GetType() ==typeof(TextBox)) { -
i.Text =string.Empty; -
} -
} -
textBox1.Select(); -
//textBox1.Focus(); -
} -
//текстовое поле Начальные показания активирует текстовое поле Конечные показания -
privatevoidtextBox1_KeyPress(objectsender, KeyPressEventArgs e) { -
if(e.KeyChar == (char)Keys.Enter) { -
textBox2.Select(); -
//textBox2.Focus(); -
//перехватываем нажатие клавиши, удаляется звук -
e.Handled =true; -
} -
} -
//текстовое поле Конечные показания активирует текстовое поле Цена за 1 Квт/час -
privatevoidtextBox2_KeyPress(objectsender, KeyPressEventArgs e) { -
if(e.KeyChar == (char)Keys.Enter) { -
textBox3.Select(); -
//textBox3.Focus(); -
//перехватываем нажатие клавиши, удаляется звук -
e.Handled =true; -
} -
} -
//текстовое поле Цена за 1 Квт/час активирует кнопку Расчитать -
privatevoidtextBox3_KeyPress(objectsender, KeyPressEventArgs e) { -
if(e.KeyChar == (char)Keys.Enter) { -
button1.Select(); -
//button1.Focus(); -
//перехватываем нажатие клавиши, удаляется звук -
e.Handled =true; -
} -
} -
//текстовое поле Сумма активирует кнопку Очистить -
privatevoidtextBox4_KeyPress(objectsender, KeyPressEventArgs e) { -
if(e.KeyChar == (char)Keys.Enter) { -
button2.Select(); -
//button2.Focus(); -
//если в текстовом поле свойство ReadOnly = true, делать перехват клавиши не обязательно -
//перехватываем нажатие клавиши, удаляется звук -
e.Handled =true; -
} -
} -
} }
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0027 {-
publicpartialclassForm1 : Form { -
publicForm1() { -
InitializeComponent(); -
} -
//кнопка Расчитать вычисляет сумму и активирует текстовое поле Сумма -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
Counter C =newCounter(textBox1.Text, textBox2.Text, textBox3.Text); -
textBox4.Text = C.Sum().ToString(); -
textBox4.Select(); -
//textBox4.Focus(); -
} -
//кнопка Очистить очищает все текстовые поля и активирует текстовое поле Начальные показания -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//кнопка очищает все текстовые поля -
foreach(Control iinControls) { -
if(i.GetType() ==typeof(TextBox)) { -
i.Text =string.Empty; -
} -
} -
textBox1.Select(); -
//textBox1.Focus(); -
} -
//текстовое поле Начальные показания активирует текстовое поле Конечные показания -
privatevoidtextBox1_KeyPress(objectsender, KeyPressEventArgs e) { -
if(e.KeyChar == (char)Keys.Enter) { -
textBox2.Select(); -
//textBox2.Focus(); -
//перехватываем нажатие клавиши, удаляется звук -
e.Handled =true; -
} -
} -
//текстовое поле Конечные показания активирует текстовое поле Цена за 1 Квт/час -
privatevoidtextBox2_KeyPress(objectsender, KeyPressEventArgs e) { -
if(e.KeyChar == (char)Keys.Enter) { -
textBox3.Select(); -
//textBox3.Focus(); -
//перехватываем нажатие клавиши, удаляется звук -
e.Handled =true; -
} -
} -
//текстовое поле Цена за 1 Квт/час активирует кнопку Расчитать -
privatevoidtextBox3_KeyPress(objectsender, KeyPressEventArgs e) { -
if(e.KeyChar == (char)Keys.Enter) { -
button1.Select(); -
//button1.Focus(); -
//перехватываем нажатие клавиши, удаляется звук -
e.Handled =true; -
} -
} -
//текстовое поле Сумма активирует кнопку Очистить -
privatevoidtextBox4_KeyPress(objectsender, KeyPressEventArgs e) { -
if(e.KeyChar == (char)Keys.Enter) { -
button2.Select(); -
//button2.Focus(); -
//если в текстовом поле свойство ReadOnly = true, делать перехват клавиши не обязательно -
//перехватываем нажатие клавиши, удаляется звук -
e.Handled =true; -
} -
} -
} }