I've got issues with NumericUpDown control. When I try to input a value, the control seems to reject it, despite the value being well withing Minimum and Maximum.
numericField.Hexadecimal = true
numericField.Minimum = 0;
numericField.Maximum = decimal.MaxValue;
Now, when I input FFFFFFF, it works fine. But adding another F will make the control automatically change the value to 0 upon losing focus.
What might be the reason?
Related
I am using a bindingsource whose datasource is set to a Datatable that is loaded from the database. The columns for integer and float in the database have default values assigned as 0. The query returns either 0 or the actual value for those columns.
I have several Textboxes bound to integer columns and also float columns.
My Textboxes that are bound to the float columns show 0 when ever there is a 0 value. My Integer Textboxes show EMPTY when there is a 0 value. I want it to show 0.
EDIT For the Down Vote and Vote to Close.
I have seen examples on SO dealing with this issue - some declare to set it in the datatable - which I used that example in my code and it has not changed the result - I type zero in my text box and the box goes blank, I type 1 and it takes it. I know the issue has something to do with what it is bound to. It is an Integer type which is not really nullable - I know you can. So why is it when I type 0 the box goes blank, but if I type 1 it stays. I know the value gets updated whether 0 or whatever. But why the difference between Single and Integer ?
Single Works using the same Exact code, Integer does not. Please explain.
I read here and other places.
How to set default values for data bound controls for addition in VB.NET
EDIT
I can paste a value 1.25 in to my Textbox with single values, and it works but I cannot type a value of 1.25 in , when I press the decimal point the cursor automatically moves back to the first character position.
I am not sure why this occurs - I do update the source from the text changed event. I have a flag set though when I enter the textbox IsEditing = True. So the text change event is only effective when the user enters the textbox. I do not understand why this happens - I know it is in the databinding but I do not know what to change to fix it.
My Relevant Code is.
Private Sub LoadTable()
dtBins = DatabaseFetch
SetDefaults()
AddBindings()
End Sub
Private Sub SetDefaults
Dim columnDefs As String() = New String()
{"Fill_Part", "Fill_Pulse", "Fill_Fails", "Fill_Value"}
For i As Integer = 0 To columnDefs.Length - 1
If dtBins.Columns.Contains(columnDefs(i)) Then
dtBins.Columns.Item(columnDefs(i)).DefaultValue = 0
End If
Next
End Sub
Private AddBindings()
bindingtxtFillPulse = New System.Windows.Forms.Binding("Text",
bsBins,
"Fill_Pulse",
True,
DataSourceUpdateMode.OnPropertyChanged)
bindingtxtFillPulse.NullValue = 0
bindingtxtFillPulse.DataSourceNullValue = 0
Me.txtFillPulse.DataBindings.Add(bindingtxtFillPulse)
End Sub
Situation: A C# Windows Forms application with a TextBox and a ComboBox. AutoValidate is set to EnableAllowFocusChange.
The TextBox represents and is shown as a percentage value e.g. "10 %" which is stored as an int. Both input controls are data bound, the TextBox with a parsing and formatting ConvertEventHandler as well as a Validating CancelEventHandler.
When entering an invalid input like "abc" and leaving the control: My Validation is performed and fails (e.Cancel = true, ErrorProvider ..). And my parsing fails (e.Value stays "abc").
Problem: When I now change the value of the ComboBox and leave it (lost focus/perform validation) or do a ValidateChildren, my format function is called with the last valid percentage value and the wrong input is lost.
Stacktrace: The problem is triggered by a ReportPropertyChanged of the ComboBox and leads to Binding.PushData, FormatObject and OnFormat -> Which calls my format function with the original value.
I want my TextBox to stay invalid and no magical reset. What can I do to prevent a value reset? Or what did I do wrong?
Thanks!
I have a numericupdown in my form and set its maximum value to 2000 although whenever i type a number bigger than 100 in and leave it the value reset to 100 automatically? I try this code to correct that but the behaviour doesn't correct.
private void answer_Enter(object sender, EventArgs e)
{
// Select the whole answer in the NumericUpDown control.
NumericUpDown answerBox = sender as NumericUpDown;
if (answerBox != null)
{
int lengthOfAnswer = answerBox.Value.ToString().Length;
answerBox.Select(0, lengthOfAnswer);
}
}
This code selects all text in the spin box of NumericUpDown control. Why? Because when you use Tab to navigate through controls to NumericUpDown, text will not be selected and input will start in first position. So, if you already have value 5 and type 6, then you will get 65. If all text will be selected, then selected value 5 will be replaces with new value 6.
Resetting value to max value is a default NumericUpDown control behavior. If entered number exceeds allowed maximum, then when you leave NumericUpDown control it's value will be replaces with allowed maximum.
Keep in mind, that actual value of NumericUpDown changed only when you leave control or use arrow keys to change value. When you type in text, value will not change until focus leaves spin box.
Verify to which control you have set Maximum value
NumericUpDown changes his value automatically only if entered value exceeds Maximum or Minimum value of that particular instance. So, it's obvious that your control has Maximum set to 100. Possibly you changed maximum value of some other control.
I haven't done a full analysis of this but I had similar problem when using this code:
<wf:NumericUpDown x:Name="NumericKernalSize" Width="50" Height="22" Visible="True" ValueChanged="NumericKernalSize_ValueChanged" BorderStyle="FixedSingle" Value="201" Minimum="0" Maximum="1000" />
What was happening was that the value = 201 seemed to be exceeding the default maximum of 100 before the maximum value was set to the desired max of 1000.
If you set the maximum before the value, the problem doesn't arise e.g. the following worked for me:-
<wf:NumericUpDown x:Name="NumericKernalSize" Width="50" Height="22" Visible="True" ValueChanged="NumericKernalSize_ValueChanged" BorderStyle="FixedSingle" Minimum="0" Maximum="1000" Value="201"/>
Hope this is of help to someone as it is tricky to catch this problem...
Cheers...
I am implementing search functionality in WinForms and I search by date range. Thus there are dateForm and dateTo date pickers on the form. By default their values are date time now() and if user do not touch date time pickers at all he will not get any results. Because search will be performed between now() and now(), also if I put min and max values as default it would solve first problem but there would be another problem if user wants to search by date range, he would need to click many times to come from default 1700 (something) to now()
Any suggestions to solve this problem?
Thanks a lot.
You can't have a valueless datepicker with the out-of-the-box control. Why? It is backed by DateTime, which is non-nullable.
You can disable it with another control, or leave it disabled until the user clicks (bad UX for keyboard enthusiasts, like myself), or find or create (!) one that uses Nullable<DateTime>.
Edit:
In response to your comment, yes, you can do this; in fact, I've done it.
use fields or private properties to hold the 'from' and 'to' dates, instead of reading them from the dtp, and set their defaults to min and max
use a boolean flag to indicate when you are manipulating the dtp value in code, and in the dtp's ValueChanged event, set the flag's value to false
in the form load event, set the flag to true and dtp value to today's date
also in the ValueChanged event, set the from and to fields to the values of the dtps (you have to set both when either dtp changes, because the user will see the other one as set to today, but the search value will still be min or max).
The problems with this is that once the user has changed the date selection, she can't easily go back to "all dates." Furthermore, the user can't select "today only" without first changing one of the dates and then changing it back.
I think the best solution for you is to have a checkbox, "search by date range," which either enables the two dtps that are otherwise disabled, or displays the dtps that are otherwise hidden. Then you search from min to max unless the checkbox is checked, and when the checkbox is checked, you use the two dtp dates no matter what they are. Don't forget to deal with to and from being out of order, which can be done in several ways.
Have a look here for a nullable datetimepicker on CodeProject, in fact there are a few here.
Put a check box next to each datetime picker, and use the check box to enable/disable the datetime picker.
So if the datetimepicker is disabled, you know the user do not want to specify the datetime.
You can set DateDateTimePicker.Format property to Custom.
Then set DateDateTimePicker.CustomFormat property to your default text (e.g "N/A" or a space " ")
After the user has selected a certain date value, you should set back the format property to short etc.
Hope this helps!
The DateTimePicker has a ShowCheckBox property that "Determines whether a check box is displayed in the control. When the box is unchecked, no value is selected."
I've used something like the following for date range selectors that can be empty. One user told me that at first, she didn't know what the check box was for, but she figured it out on her own after using it a couple of times.
public DateTime? EndDate
{
get
{
DateTime? returnValue = null;
if (endDateDateTimePicker.Checked)
{
returnValue = endDateDateTimePicker.Value;
}
return returnValue;
}
set
{
if (value.HasValue)
{
endDateDateTimePicker.Checked = true;
endDateDateTimePicker.Value = value.Value;
}
else
{
endDateDateTimePicker.Checked = false;
}
}
}
linking a trackbar and a textfield is very easy in windows forms.
it is like this:
textBox.DataBindings.Add("Text", trackBar, "Value");
the problem is, that trackbars only allow for integer values but i want to have floating point values.
so i usually just divide the value by 100, since on the trackbar the value is not directly visible to the user.
but in the textbox it is.
so is it possible to link these two with a factor of 100?
thanks!
The line of code you have adds a Binding object to the text box's DataBindings collection.
The Binding class has events called Format and Parse, which you can use to perform the division (the Format event takes a value from the trackbar and formats it for the text box) and the multiplication (the Parse event takes a value from the text box and scales it for the trackbar).
You can use intermediate variables like below:
public double v{set;get;}
public int v100
{
set { v = value / 100D; }
get { return (int)(v* 100D); }
}
and blind them with Controls.
trackBar.DataBindings.Add(new Binding("Value", PtParams, "v100"));
textBox.DataBindings.Add(new Binding("Text", PtParams, "v"));