I have an application in WinForms where I have many controls. These controls represents settings of application and user can save this settings by clicking on a button.
I am trying to resolve a problem with NumericUpDown (I will call it Num) control: Lets say that Num has these properties:
Minimum: 10
Maximum: 60
Step: 1
If a user want to change value, there are two ways how to do that: clicking on arrows (right side of the Num) or manually typing value directly to Num. First way is OK but most most of users are using second way and there is a little problem.
If a user type some value out of the interval for example 1, it is OK because he could continue in typing with 5 so final value is 51 and this is inside the interval. But if he stop typing after value 1, it means he type value out of the interval (1). If he click somewhere out of the Num, value (which is out of the interval) is automatically changed to the closest allowed value (in case of 1, value will be changed to 10).
But he probably will not notice this automatic change so I want to handle it somehow and inform him that he put there invalid value. But this situation is not handled by any event of Num (there is no way how to find out this invalid value he put there - if I try to read value inside ValueChanged event, it reads automatically changed value, not the invalid one).
I know I can add TextChanged event, but there is a problem that if he type some invalid value (5), it can be changed to valid value (by adding 1 so it makes 15).
Do you have any ideas how to resolve this issue? I know this is stupid but this doesn't depend on me, I have to do this and I don't know how.
use Lost Focus and instead of setting the minimum at design stage check it with an If statement.
Private Sub Num_LostFocus(sender As Object, e As System.EventArgs) Handles Num.LostFocus
If Num.Value < 10 Then MsgBox("Number is incorrect")
End Sub
alternatively you can probably create a custom control where you can override the event that changes the value to the minimum but i have never tried.
So this is my solution:
I handle TextChanged event and inside this method I assign text to its tag:
Private Sub NUDTextChanged()
Integer.TryParse(NUD.Text, NUD.Tag)
End Sub
And then:
Private Sub NUD_LostFocus() Handles NUD.LostFocus
If NUD.Tag < NUD.Minimum Or NUD.Tag > NUD.Maximum Then
' show message
End If
End Sub
Handling Validating event is useless for me because automatic change to allowed value is before validating and this automatic change fires TextChange event so after validating I have new value instead invalid one.
LostFocus is before automatic change so I can easily control if the value is valid or not.
Related
Help! I'm trying to navigate previous/next through records (MS Access backend database) in a bound form. Note, this is a DataRowView (individually bound fields), not a DataGridView. I've got two related issues, but I'll handle the second issue in another post. I've got two key fields in the underlying database table. The second key field, Carrier_No, is defined as 5 characters. When I attempt to move to the previous record, the application populates the second key field with 'System.Data.DataRowView.' Then, when I click the Previous (or Next) button again, I get the following error: System.ArgumentException: 'Cannot set column 'Carrier_No'. The value violates the MaxLength limit of this column.' What am I doing wrong? This seems to be such a simple task.
Thanks all for your responses. After much troubleshooting, I stumbled upon the solution. It turns out the combobox 'DropDownStyle' property was the culprit. I had originally set the property to 'DropDownList' to prevent users from entering a value. When I set the property to 'DropDown,' the problem went away. I have no idea why this property would have any effect on record navigation, but it does. To prevent users from entering data, I employed some code in the combobox 'KeyPress' event. As it turns out, this solution also solved a related issue (combobox values being copied to other records, separate post).
Private Sub cmb_TPA_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cmb_TPA.KeyPress MsgBox("Please select a TPA value from the pick list.", vbExclamation) e.Handled = True 'cancel event End Sub
I'm using Microsoft Office Professional Plus (64 bit) on a Windows 10 (64 bit) platform. I have a subroutine that is processed when I make a change to a Userform field called MyDate. It's called Private Sub MyDate_AfterUpdate(). It's the second field on a form. It works fine as long as the contents of the MyDate field are edited. However, if the user doesn't need to update the contents of the MyDate field because they accept the default of the field and just presses the tab key past that second field, I'd still like the subroutine to be executed. What event can I use to activate code when I simply tab through the field and don't necessarily edit the contents? Thanks for looking at this.
If you look at the top of the code panes, you'll notice two dropdowns. The left one contains all interfaces and event providers you can implement in that class (a UserForm is a class).
Select your MyDate control from that dropdown; the right-side dropdown is now listing every event you could handle for this MyDate control:
In this particular case, the Exit event seems a good candidate:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'make Cancel.Value = True to prevent exiting the control.
'...ideally... make that conditional...
End Sub
By consistently using these dropdowns to let the VBE generate event handler procedures for you (instead of typing them up from memory), you avoid getting it wrong... and getting an event handler signature wrong can do anything from literally nothing at all, to compile errors if you're lucky, or weird and hard-to-diagnose behavior if you're less lucky.
So i want to be able to prevent people increasing the number in a numericupdown after an assigned point is met.
e.g. in rugby the amount of conversions cant be larger then the amount of tries. so say i assigned one numericupdown as tries and had it at 4 then another as conversions. how do i limit so people cant put in anymore then 4 tries.
At the moment i have have NumericUpDowncon.Maximum = try
but it still allows the user to input more conversions then tries but then when the calculate button is pressed it puts the converions back down to the max it could be in reguards to the tries.
Assign a handler to the ValueChanged event of both NumericUpDowns. In the handler you'll have to check if conversions > tries.
Private Sub NumericUpDowncon_ValueChanged(sender as Object, e as EventArgs) _
Handles NumericUpDowncon.ValueChanged, NumericUpDowntry.ValueChanged
If NumericUpDowncon.Value > NumericUpDowntry.Value then NumericUpDowncon.Value = NumericUpDowntry.Value
End Sub
On a normal textbox, I usually use the AfterUpdate event to perform some action. That means the user has to press Enter or Tab after typing, or click in another control, and I have always been happy with that behaviour.
Now I am setting up a Date filter in the header of a continuous form in Access 2010, and I realize that changing the date through the little calendar that comes automatically, does NOT fire the AfterUpdate event, forcing to press Enter after selecting the correct date, which is a bit heavy.
Using OnChange would trigger at every character entered, which is not nice either.
Any suggestion ?
It is a bit late reply but I hope it will help the others.
When using textbox as a DatePicker you should use Change Event with your filter.
However when you are checking your textbox like Form_name.TextboxName it will show last picked date. To avoid that and use currently selected one you need to provide current date like Form_name.TextboxName.Text. Careful here because .Text property is sensitive to focus.
...in short:
Form_name.TextboxName - will show last picked date
Form_name.TextboxName.Text - will show currently picked date
well, after you select a date from the date-picker, the Change event occurs for the TextBox control. Then, call a sub or function or set focus to another control... to avoid event fires for each pressed key, put something like:
if Len(me.activecontrol) < 10 then exit sub
I hope this helps
I use LostFocus event in the textbox. It allows to use the calendar tool and alter the content. The User has to leave the textbox sooner or later, isn't it?
I use it in this way
Private Sub txt_FirstDate_Change()
txt_FirstDate = txt_FirstDate.Text
myfilter
End Sub
I have some controls bound to a BindingSource control.
I want to do a calculation when the value changes in one control and set the result on another control.
Do I update the textbox the property is bound to or do I update the underlying entity which would update the control anyway (I hope)?
When I change combobox A (OnPropertyChange) textbox B is updated with the new calculated result. This works fine, but I have noticed that when I leave combobox A it reverts back to its original value. What is going on here!
Private Sub ComboBoxEditCostCode_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxEditCostCode.EditValueChanged
Select Case ComboBoxEditCostCode.EditValue
Case "7"
CType(TransactionEntityBindingSource.Current, TblTransactionsEntity).Qbdescription = "7-is here"
Case "2"
CType(TransactionEntityBindingSource.Current, TblTransactionsEntity).Qbdescription = "2-is here"
Case Else
CType(TransactionEntityBindingSource.Current, TblTransactionsEntity).Qbdescription = "7-is here"
End Select
End Sub
if we bind a control to a source, then if the source changes, we can make the its value automatically reflected in the control. About the problem you are facing, it would be better if you show the code snippet.
Tell more about your changing, how second text box is bound?
You have to change your initial data instead of changing textbox b value.
Also when textbox A loses it focus raises EndEdit event and I think binding mechanism refreshes value in textbox B.
You can control on which action editing is done when you setting your binding to textboxes.
as a rule of thumb, if you are using a binding source you always CRUD the data through it. Don't forget to call BindingSource.EndEdit when you are done, hope this helps