アプリで、または少なくとも特定のイベントハンドラーで、キーボードのビープ音を抑制したいと思います。これは可能ですか?
アップデート
さて、あなたはそれを求めました(コードサンプル):
プレビューキーダウン:
private void textBoxDuckbill_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
switch (e.KeyCode) {
case Keys.Down:
case Keys.Up:
e.IsInputKey = true;
break;
}
}
キーダウン:
private void textBoxDuckbill_KeyDown(object sender, KeyEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Up)) {
SetFocusOneRowUp(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Down)) {
SetFocusOneRowDown(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Left)) {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Right)) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
}
キープレス:
private void textBoxDuckbill_KeyPress(object sender, KeyPressEventArgs e) {
TextBox tb = (TextBox)sender;
errorProviderCRLogins.SetError(tb, String.Empty);
// If user presses "%" (37) move back/left one TextBox column;
// if user presses "'"(39) move forward/right one TextBox column.
// Also now allowing navigational arrows to do the same thing (KeyDown event)
if (e.KeyChar == '%') {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyChar == Convert.ToChar(@"'")) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
// Preclude values (1,2,3) that would normally be allowed (see below) but do
// not have a value in the corresponding PlatypusID TextBox
if (((e.KeyChar == '1') && (String.IsNullOrWhiteSpace(textBoxPlatypusNum1.Text))) ||
((e.KeyChar == '2') && (String.IsNullOrWhiteSpace(textBoxPlatypusNum2.Text))) ||
((e.KeyChar == '3') && (String.IsNullOrWhiteSpace(textBoxPlatypusNum3.Text)))) {
e.Handled = true;
return;
}
// Now, having gotten to here, we can assume that 1, 2, and 3 are valid (as are
// Space and Backspace all the time).
if ((e.KeyChar != '1') &&
(e.KeyChar != '2') &&
(e.KeyChar != '3') &&
(e.KeyChar != (char)Keys.Space) &&
(e.KeyChar != (char)Keys.Back)) {
e.Handled = true;
return;
}
// Added Space as an allowable entry so user can delete a val with that key
// (which will automatically happen on tabbing into the TextBox, as it is
// now being highlighted)
if ((e.KeyChar == (char)Keys.Space) || (e.KeyChar == (char)Keys.Back)) {
tb.Text = String.Empty;
buttonSave.Enabled = true;
// Don't return here, as they might continue to hit Space to zero out
// subsequent cells
}
// Now, if there is already a value in the cell (this is a repeated val, as shown
// by TextLength being 1 instead of 0), move it to the next cell and give it the
// value just entered (even if space for "delete")
if ((tb.TextLength == 1) || (e.KeyChar == (char)Keys.Space)) {
buttonSave.Enabled = true;
MoveToNextCellAndEnterVal(e.KeyChar.ToString(), tb.Name);
}
// Although KeyChar has a val such as 49/("1"), TextLength == 0
if ((e.KeyChar == '1') ||
(e.KeyChar == '2') ||
(e.KeyChar == '3')) {
buttonSave.Enabled = true;
}
}
テキストが変更されました:
private void textBoxDuckbill_TextChanged(object sender, EventArgs e) {
TextBox tb = (TextBox)sender;
if (tb.Text == "1") {
tb.BackColor = PlatypusID1_BACKCOLOR;
tb.ForeColor = PlatypusID1_FORECOLOR;
return;
}
if (tb.Text == "2") {
tb.BackColor = PlatypusID2_BACKCOLOR;
tb.ForeColor = PlatypusID2_FORECOLOR;
return;
}
if (tb.Text == "3") {
tb.BackColor = PlatypusID3_BACKCOLOR;
tb.ForeColor = PlatypusID3_FORECOLOR;
return;
}
tb.BackColor = System.Drawing.SystemColors.Window;
tb.ForeColor = System.Drawing.SystemColors.WindowText;
}
private void MoveToNextCellAndEnterVal(string APlatypusID, string ATextBoxName) {
String numericPortionOfTextBoxName = ATextBoxName.Remove(0, LENGTH_OF_TEXT_BOX_BASE);
String sTextBoxToFind;
int textBoxNumber = 0;
int nextTextBoxNumber;
int.TryParse(numericPortionOfTextBoxName, out textBoxNumber);
nextTextBoxNumber = ++textBoxNumber;
// "wrap around"
if (nextTextBoxNumber > NUMBER_OF_QUARTER_HOURS) {
nextTextBoxNumber = nextTextBoxNumber - NUMBER_OF_QUARTER_HOURS;
}
sTextBoxToFind = String.Format("textBoxDuckbill{0}", nextTextBoxNumber);
TextBox tb = (TextBox)this.Controls.Find(sTextBoxToFind, true).First();
tb.Focus();
tb.Text = APlatypusID;
}
private void SetFocusOneRowDown(string ATextBoxName) {
String numericPortionOfTextBoxName = ATextBoxName.Remove(0, LENGTH_OF_TEXT_BOX_BASE);
String sTextBoxToFind;
int textBoxNumber = 0;
int nextTextBoxNumber;
int.TryParse(numericPortionOfTextBoxName, out textBoxNumber);
if (!(textBoxNumber == NUMBER_OF_QUARTER_HOURS)) {
nextTextBoxNumber = ++textBoxNumber;
} else {
nextTextBoxNumber = 1;
}
sTextBoxToFind = String.Format("textBoxDuckbill{0}", nextTextBoxNumber);
TextBox tb = (TextBox)this.Controls.Find(sTextBoxToFind, true).First();
tb.Focus();
}