How to prevent key entry with NumericUpDown control? - winforms

I have a numericupdown control on a winform and I noticed while testing that not only you have the option of changing the value by pressing up and down key but also simply entering the values from your keyboard.
I don't want that. I only want the user to be able to change the numericupdown's value only by clicking the up and down buttons within the box.
So far I simply can't find a solution.
Does anyone know how to do this?

To disable user from editing, set Readonly property to true.
updown.ReadOnly = true;
For more tailoring, you may refer this answer.

Sounds bad for user experience depending on the range of values you are allowing.
To do this you need to create a control with inherits from NumericUpDown and override the OnKeyPress/OnKeyDown methods.

Using updown.ReadOnly = True; does not work for me. It seems to be a reoccuring bug.
But catching any changes and then undo it does. For this bind the function updown_ValueChanged() to the updown.ValueChanged attribute.
decimal spin = 1;
private void updown_ValueChanged(object sender, EventArgs e)
{
if (updown.ReadOnly)
{
if (updown.Value != spin)
{
updown.Value = spin;
}
}
else spin = updown.Value;
}

Related

How to prevent silverlight validation popup from appearing half off the screen

I have a silverlight 4 application which has some text boxes that are as wide as the page.
When there is a validation error, a popup is displayed when the user clicks in the control.
The problem is - it only shows the popup for these long text boxes to the left of the text box. It wont go above or below and so as a consequence, most of the popup is displayed out of the page so its chopped off.
I know that I can re-template the text box and try to adjust the popup myself, but before doing that I just wanted to check to see if someone knew of a simple property or something that I can use to prevent that from happening?
Cheers
Rod.
Good question. I guess I would try to solve this via a somewhat "intelligent" AttachedProperty.
Pseudo code ahead:
<TextBox ... my:PopupUtils.KeepPopupWithinScreen="True"/>
And the (pseudo c#) code:
public static class PopupUtils
{
// remember: pseudo code, just to get the idea
static AttachedProperty KeepPopupWithinScreen = type: bool, default: false,
onChanged: HandleKeepPopupWithinScreenChanged;
private static void HandleKeepPopupWithinScreenChanged(
DependencyObject obj, bool value)
{
obj.Loaded += HandleTargetElementLoaded;
}
private staic void HandleTargetElementLoaded(object sender, ...)
{
var popup = VisualTreeHelper.GetDecendantOfType<Popup>(sender);
if ( popup != null )
{
var offsetController = new OffsetController();
offsetController.SetBinding(ObservedOffsetProperty,
new Binding("HorizontalOffset"){Source=popup});
offsetController.ControlledTarget = popup;
//now to prevent garbageCollection...
SetAttachedOffsetController(popup,offsetController);
}
}
public static AttachedProperty AttachedOffsetController = type:OffsetController;
}
I do this sometimes, so this pattern is actually working quite nicely. Maybe it feels a bit "unnatural" at first.
Just letting you know of the solution I used to this problem.
It was another StackOverflow question which I have lost the reference to so I do apologise for not referencing it properly, but the problem is caused by the Text Box and other control styles having a fixed position of to the side of the control when displaying the validation message.
I simply had to create a copy of the style and have the popup appear at the top of the controls instead of beside it.
Problem Solved.
Cheers
Rod.

WPF if statement based on radio button checked or not

I'm having difficulty with something which seems like it should be very simple. I'm coming from Windows Forms and starting up with WPF. I think I have a simple syntax issue but I can't seem to find a specific example for this radio button issue.
I have a radio button on my GUI for a query to run either via a map selection or a list. When load is clicked, it should perform one operation if map is selected, a different operation for list. Code looks similar to this:
private void Load_Click(object sender, RoutedEventArgs e)
{
if (rdBtnList.Checked == true)
{
//do this
}
// if rdBtnList not checked (ie if rdBtnMap is checked)
// do this
}
Any help would be greatly appreciated. Thanks.
Change:
if (rdBtnList.Checked == true)
to
if (rdBtnList.IsChecked == true)
Note:
I'm coming from Windows Forms and starting up with WPF
You must forget everything you ever learned in winforms, and embrace MVVM. You should create a ViewModel and bind your rdBtnList.IsChecked property to some boolean value in the ViewModel, then perform your logic in there. The views' code behind is not the right place for application logic.
The property in WPF is called IsChecked hence you just need to update the if statement
if (rdBtnList.IsChecked == true) {
...
}
The IsChecked property is bool? (nullable value) in WPF so you may choose to be more explicit here by doing the following
if (rdBtnList.IsChecked.HasValue && rdBtnList.IsChecked.Value) {
...
}
Are you missing a return statement? As is, the posted code will always execute the if not checked path:
private void Load_Click(object sender, RoutedEventArgs e)
{
if (rdBtnList.IsChecked == true)
{
//do this
return; // return, or the if not checked path below will always win.
}
// if rdBtnList not checked (ie if rdBtnMap is checked)
// do this
}
In WPF, you'll need to use the IsChecked property. See the RadioButton Class for more details.

TextBox Password Char

the textbox in Windows Forms used to have a PasswordChar property. In WPF there is an extra control for that: PasswordBox. This wouldn't be a problem but my application runs on an touchscreen only device. Unfortunately the password box does not support the on screen keyboard. I was wondering if there is a way of adding the password char feature to the standard textbox.
This answer may provide you with what you need.
I made my way around this particular problem by creating two Properties for the Password content and binding both of them to the same Model value. One of them (the visible UI Element) binds to Password. The Get on this property of course then returns an array of characters for display. The functions that must use the password text can use the PlainPassword Property.
Adding "UpdateSourceTrigger=PropertyChanged" to the Binding for the textbox causes the characters to appear in the text box as they are typed.
public string Password
{
set
{
Model.Password = value;
OnPropertyChanged("Password");
}
get
{
return new String('●', Model.Password.Length);
}
}
public string PlainPassword
{
set
{
Model.Password = value;
OnPropertyChanged("Password");
}
get { return Model.Password; }
}
I believe the only way you can achieve this is to create your own control based on textbox. Then just bind the actual text property to a property that returns your password character rather than the actual password. Then you can pull the password as a dependency property (though I've heard this is rather insecure, which is why it is not a dependency property in the password box), or just a regular property and access it by passing the whole textbox object.
A simple way to obfuscate the password in a TextBox is to use the Webdings font.
txtInput.FontFamily = new FontFamily("Webdings");
This is not completely safe, but sufficient in most cases. Note that Webdings works better than Wingdings, because Wingdings does not cover the lower case letters and returns everything in upper case.
helló!
im new here but maybe i can help u. i find this -> can be work whit WPF and passwordbox
private void delete_Click(object sender, RoutedEventArgs e)
{
if (pass_passbox.IsFocused == true)
{
pass_passbox.Password= "";
}
}
ofc u do this pass_passbox.Text if its textbox but when change WPF passwordbox u need to write can pass_passbox.Password and u can do changes from screen keyboard .
not fully tested but u can reset this way
and u can do select like this:
string Query = "Select * from [employeeinfo] where username='" + this.txt_user_name.Text + "' and password='" + this.pass_passbox.Password + "' ";
u can see this.pass_passbox.Password is the same at textbox this.pass_passbox.Text
private void txtBoxPassword_TextChanged(object sender, EventArgs e)
{
//password view protection//
txtBoxPassword.UseSystemPasswordChar = true;
}
That's a way to enable the DEFAULT character used for hiding the password from the system,if you wish to set your own password char just substitute the actual line inside the event function with the following:
txtBoxPassword.PasswordChar='*'; //or any other character

Prevent Silverlight DataGrid from Processing Enter Key

I have a datagrid in a Silverlight application. Clicking on the first row, and then pressing the Enter key causes the selection to move to the next row. I do not want this behavior - the Enter key is used for totally different purposes on this screen.
I realize that this is part of the editing framework, but I need a way to turn it off. I tried setting IsReadOnly to True (even though the control isn't technically read-only) and that didn't have any effect.
I attached to the datagrid KeyDown event but it's not called when the Enter key is pressed. It works fine for other keys.
I'm stumped. Thanks for your help.
Yeah, that's annoying.
As far as I know, the only option is to create your own control that inherits from DataGrid, and do what is needed before passing the event on to the base.
public class NewDataGrid : DataGrid
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//do what is needed here
e.Handled = true;
}
base.OnKeyDown(e);
}
}

How to give an initial value to textBox?

I want my C# program to have initial values for its textboxes. For example, in one of the textboxes, it should say "Please enter your name".
When you click (or tabStop) on the textbox, the initial value should disappear and the user will be able to enter their input to the textbox.
I can do all this with click_event, but using this method the initial text would not have less opacity. How am I able to achieve this?
This is how I finally did it:
Boolean first_time_click = true;
private void Form1_Load(object sender, EventArgs e)
{
textBox1.ForeColor = System.Drawing.Color.Gray;
textBox1.Text = "Enter the Text";
}
private void For_First_Click()
{
if (first_time_click)
{
textBox1.Clear();
textBox1.ForeColor = textBox1.ForeColor = SystemColors.WindowText;
}
first_time_click = false;
}
private void textBox1_Click(object sender, EventArgs e)
{
For_First_Click();
}
I assume you are talking about winform (tabstop) you have to handle it within the event key-press. you can use the below code:
TextBox1.Select(0, TextBox1.Text.Length);
this will select the text and window will remove it for you as soon as the user start to typing
you can use the same code to have this behavior also for TabStop
All you need to do is set the Textbox's .Text property and use GotFocus event to clear the box when the person clicks (or tabs) into it to start typing.
Always remember that there are more ways than the mouse to navigate a form, so use the GotFocus event to determine when the user enters a control, and use the Validated event to determine when they've changed data and exited the control.
For this type of effect you need java script.Because java script provide you functionality of mouse hover and mouse out these are the functions which provide you the same functionality which u seeing in this page of search bar. If you need code reply me i can give you.

Resources