如何让XtraInputBox仅接受数值并判断点击的按钮?
Got it, let's tackle your two requirements step by step—restricting input to valid numbers/backspace and detecting which button the user clicked.
1. Restrict Input to Numeric Values & Backspace
You have two straightforward ways to lock the XtraInputBox's text editor to only accept digits and backspace:
Option 1: Use Numeric Mask (Simplest Approach)
Apply a built-in numeric mask directly to the editor in XtraInputBoxArgs—this handles input restriction automatically:
private void btnDown_Click(object sender, EventArgs e) { XtraInputBoxArgs args = new XtraInputBoxArgs(); args.Caption = "Quantité"; args.Prompt = "Entrez la quantité"; args.DefaultButtonIndex = 0; args.DefaultResponse = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Quantité Restante"); // Add numeric mask to restrict input var textEdit = args.Editor as TextEdit; if (textEdit != null) { textEdit.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric; textEdit.Properties.Mask.EditMask = "n0"; // Use "n2" if you need decimal support textEdit.Properties.Mask.UseMaskAsDisplayFormat = true; } var result= XtraInputBox.Show(args); }
Option 2: Handle KeyPress Event (Full Control)
If you want precise control over allowed keys, attach a KeyPress event to the editor to block non-numeric/non-backspace inputs:
private void btnDown_Click(object sender, EventArgs e) { XtraInputBoxArgs args = new XtraInputBoxArgs(); args.Caption = "Quantité"; args.Prompt = "Entrez la quantité"; args.DefaultButtonIndex = 0; args.DefaultResponse = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Quantité Restante"); // Block invalid key presses var textEdit = args.Editor as TextEdit; if (textEdit != null) { textEdit.KeyPress += (s, ke) => { // Allow digits and backspace; add '.' here if decimals are needed if (!char.IsDigit(ke.KeyChar) && ke.KeyChar != '\b') { ke.Handled = true; // Cancel the invalid key press } }; } var result= XtraInputBox.Show(args); }
2. Detect Which Button Was Clicked
The XtraInputBox.Show() method returns a DialogResult that tells you exactly which button the user selected. You can also check args.DialogResult after the box closes:
private void btnDown_Click(object sender, EventArgs e) { XtraInputBoxArgs args = new XtraInputBoxArgs(); args.Caption = "Quantité"; args.Prompt = "Entrez la quantité"; args.DefaultButtonIndex = 0; args.DefaultResponse = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Quantité Restante"); // Add your input restriction code here (either mask or KeyPress) var result = XtraInputBox.Show(args); // Check button click outcome if (result == DialogResult.OK) { // User clicked the default/OK button var inputValue = args.Result; // Process the valid input here } else if (result == DialogResult.Cancel) { // User clicked Cancel // Handle cancellation logic here } }
Quick Notes:
- If you added custom buttons to
args.Buttons, match theresultto the button'sDialogResultproperty to identify which one was clicked. - Even with input restrictions, always validate the final input (e.g., check for empty values or out-of-range numbers) before processing it.
内容的提问来源于stack exchange,提问作者M.Bouabdallah

