C# WPF How to use only numeric value in a textbox [duplicate] - c#

This question already has answers here:
How do I get a TextBox to only accept numeric input in WPF?
(33 answers)
Closed 5 years ago.
I use a WPF application I would like to allow only numbers in a textbox. In a WindowsForm application I would know how I would have to make it.
Unfortunately, there is a KeyPress not in WPF and the "code" functioned also no longer.
How is it right and what is the event?
Here you can see the old code that has worked in Windows Form:
private void tbKreisdurchmesser_KeyPress(object sender, KeyEventArgs e)
{
if (char.IsNumber(e.Key) || e.KeyChar==".")
{
}
else
{
e.Handled = e.KeyChar != (char)Keys.Back;
}
}

You can add Previewtextinput event for textbox and validate the value inside that event using Regex,
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = sender as TextBox;
e.Handled = Regex.IsMatch(e.Text, "[^0-9]+");
}

Related

How can I immediately/reactively determine if any CheckedBoxListItem has been selected? [duplicate]

This question already has answers here:
No ItemChecked event in a CheckedListBox?
(4 answers)
Closed 7 years ago.
I want to enable a button only if valid criteria have first been selected (C# Windows Forms app). I have this code (I tried the IndexChanged and ValueChanged events first, but this answer indicates the ItemCheck event is the one to monitor:
private void checkedListBoxUnits_ItemCheck(object sender, ItemCheckEventArgs iceargs)
{
buttonGenRpts.Enabled = ValidSelections();
}
private bool ValidSelections()
{
bool OneUnitSelected = checkedListBoxUnits.CheckedItems.Count == 1;
. . .
OneUnitSelected is always false, even after selecting an item (checkbox control) in the checkedListBoxUnits control. It seems that these events fire before the checkbox is actually checked. So what event can I tap into to verify an item has been checked in a CheckedListBox?
This is a bit hacky, but you could defer running ValidSelections until the checking is complete:
private void checkedListBoxUnits_ItemCheck(object sender, ItemCheckEventArgs iceargs)
{
BeginInvoke(() => {
buttonGenRpts.Enabled = ValidSelections();
});
}

partially editable string in a textbox control

is there a way to allow editing a string partially in c# and wpf textbox?
somthing , if the contents of the TextBox were for example
"http://xxxx.xxx/xx/path?param1=xxx&param2=xxx"
the x can be replaced with whatever length but any thing else is constant and cannot be edited in the textbox, any way to achive such thing?
There are two relevant events that you can handle on the TextBox; the PreviewKeyDown and the PreviewTextInput events. By handling these two events, you will have complete control over what the user can and can't edit in the TextBox. Of course you will need to work out the logic inside, but the event handlers are the tool to enable you to do what you want:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
// Do your text filtering here using e.Key and e.Handled
}
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
// Do your text filtering here using e.Text and e.Handled
}

C# WPF Disable the exit/close button [duplicate]

This question already has answers here:
How to hide close button in WPF window?
(23 answers)
Closed 9 years ago.
Is it possible to disable the close button in a WPF form?
How can I disable the close button?
I have been searching around and found the solution below. But that works only in Windows Form!
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
in wpf this event called Closing :
public Window4()
{
InitializeComponent();
this.Closing += new System.ComponentModel.CancelEventHandler(Window4_Closing);
}
void Window4_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
You need to implement a windows hook to accomplish that. See this MSDN post for details.

How to Set a data type for a textbox [duplicate]

This question already has answers here:
How do I make a textbox that only accepts numbers?
(41 answers)
Closed 9 years ago.
I'm building a small application that is connected to a private database. In my application, I input data inside textboxes that record data inside a database after clicking a button. The problem I'm facing is that I want to make a certain textbox accept only Integers to prevent entering wrong types of data.
Try like below it will help you...
The below code accepts only Numbers and dots(.)
Designer.CS
this.txtNumbers.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtNumbers_KeyPress);
Code Behind :
private void txtNumbers_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
to Handle Copy and Paste...
private void textBox1_TextChanged(object sender, EventArgs e)
{
int result;
if (txtNumbers.Text != "")
{
if (!int.TryParse(txtNumbers.Text, out result))
{
txtNumbers.Text = "";
MessageBox.Show("Invalid Integer");
}
}
}
At it's simplest.
Add a keypress event handler to the textbox
The eventarg argument has the key that was pressed. (e.KeyChar)
If it's not a legal key set e.Handled on the eventargs argument to true.
Don't forget to allow backspace, cut, copy and paste etc through.
If you want negatives, minus sign is allowed but only as the first character.
If you want formatting / formatted display, e.g. negatives in brackets, thousand separators, decimal separators, currency symbols etc, better off knocking up a user control and wrap it all up in there.
You can use a control designed for numbers, NumericUpDown. It gives you added control over decimal places and min / max.

C# - preparing event after pushing two buttons on keyboard [duplicate]

This question already has answers here:
How to use multiple modifier keys in C#
(9 answers)
Closed 8 years ago.
i've got problem. If I push one key I can get event for example:
if (e.KeyCode == Keys.F4)
{
Method();
}
How could I do the same if I push two keys? For example Enter + F4?
FormLoad()
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
//Works for Ctrl+F4
if (e.Control && e.KeyCode == Keys.F4)
{
//Do something
}
}
See if this work for you.

Categories