Cancel Handler on a KeyUP Event? VB.NET - regex

There a way to use e.Cancel () on a keyup event?
I'm trying to validate a textbox with a Regex and I need to cancel the event if it no meets the Regex expression, or delete the key pressed to meet so that the expression
For Example:
Dim rex As Regex = New Regex("^[0-9]{0,9}(\.[0-9]{0,2})?$")
Private Sub prices_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Textbox1.KeyUp,
Dim TxtB As TextBox = CType(sender, TextBox)
If (rex.IsMatch(TxtB.Text) = False ) Then
e.cancel = true
End If
End Sub
ERROR: 'cancel' is not a member of 'System.Windows.Forms.KeyEventArgs'.

Try
If (rex.IsMatch(TxtB.Text) = False) Then
e.SuppressKeyPress = True
End If
From MSDN:
You can assign true to this property in an event handler such as KeyDown in order to prevent user input.
Setting SuppressKeyPress to true also sets Handled to true.
It's unclear what you are trying to cancel though, since you are reading the TextBox.Text value. KeyDown is usually the preferred event to intercept the keystroke.
If you are trying to validate the entire string, the Validating event might be more appropriate. With TextBox controls, you will always have the danger of someone pasting clipboard text into the control, which could bypass key events.

Related

How can I clear the focus from a desensitized GtkWidget - GtkSpinButton with property sensitive = false

Background: I have a few tabs set up in my GtkNotebook, and when certain conditions are met on the main tab, then input fields in the 'test signals' tab are accordingly disabled.
When the input inside a GtkSpinButton is highlighted on the 'test signals' tab and the GtkSpinButton's property sensitive is set to false, the text within the GtkSpinButton remains highlighted. Because sometimes there are no other editable fields on this tab, the text remains highlighted until another widget has its property sensitive set to true.
This just looks sloppy to me, so I would like to stop this behavior and have the widgets that are set to sensitive = false all cleared of focus or highlighting. Any idea how to un-highlight the text within the GtkSpinButton, maybe before setting sensitive = false?
To get rid of a selection you can use the gtk_editable_select_region() function. This is a method on GtkEditable, an interface that both GtkEntry and GtkSpinButton satisfy. You can convert either of these to GtkEditable with GTK_EDITABLE(). For example:
gtk_editable_select_region(GTK_EDITABLE(spinbutton), 0, 0);

Validating an Edit Control's text while the user types MFC

Question/Problem: I have an edit control (text box) that the user enters a username into. I am attempting to compare the username entered to the ones listed in my List Control. If the username given matches, my button's text should change from Create User to Update User.
My problem is in finding the correct event/time to compare the strings, without creating an infinite loop.
What I have tried: I have tried using the edit control events EN_CHANGE and EN_UPDATE. Both of these events cause stack-overflow exception or an infinite loop. I thought that one of these events would be called every time something is typed or the backspace was used within my edit control.
In my EN_CHANGE / EN_UPDATE event, I compare the username strings and set the button's text. With either event, it is called infinite times:
void Users::OnEnUpdateLoginName() //EN_UPDATE Event
{
bool match = false;
//Compare the edit control text with each List Control text.
for(int i = 0; i<m_UserList.GetItemCount(); i++)
{
if(strcmp(m_UserList.GetItemText(i,0),m_loginName)==0)
match = true;
}
//If the usernames match, change the button's text to "Update User"
if(match)
{
CWnd *currentSelection = GetDlgItem(TXTC_LOGIN_NAME);
currentSelection->SetWindowTextA("Update User");
}
else
{
CWnd *currentSelection = GetDlgItem(TXTC_LOGIN_NAME);
currentSelection->SetWindowTextA("Create User");
}
}
.
If the text in red matches, change the text of the button highlighted in blue.
Should I be using a different event to validate the string in real-time as the user types?
My code had two issues. I needed to use UpdateData, so that data for all my dialog controls would be current. I also was updating the wrong variables. Thanks #rrirower

Ember handle save action and extract pressed key

I have a textarea from which I want to catch a shift-enter event, which is pressed inside it.
enter as well as shift-enter both call the save method of the controller. But inside the save method, I don't seem to have an param giving me the pressed key(number).
When I implement the keyUp: (e) -> method inside my view(controller), it either not called when the save method is implemented inside the controller, or, when the save method is not implemented, I get an error saying nothing handled the action save.
Actually I only want to know if the pressed key(s), which called the save action was either enter or shift-enter.
What's the best way to do this? Thanks!
Update
My code looks like this now:
ContactIndex = Ember.View.extend
listenKey: ((event) ->
if(event.which == 13)
#get('controller').send('save', event.shiftKey)
).on('keyDown') # When I use `keyUp` instead, the isShift boolean value is not being transmitted.
Contact = Ember.ObjectController.extend
save: (isShift) ->
console.log('Shift Key Pressed: ' + isShift)
false
Every time I press enter or shift-enter, the save action gets called twice.
its better to pass a boolean value whether its a shift key event or not from the view to controller.
App.IndexController=Ember.Controller.extend({
actions: {
save: function(isShift){
console.log('Shift Key Pressed: ' + isShift);
return false;
}
}
});
App.IndexView=Ember.View.extend({
listenKey: function(event){
if(event.which===13){
this.get('controller').send('save',event.shiftKey);
}
}.on('keyUp')
});
Check this jsbin http://emberjs.jsbin.com/fazibu/1/edit

Continually check process and show Button/TextBox

I am attempting to create a form using VB.Net that checks if the IExplorer process is running and then display a RichTextBox (which advises the user to close IE) is the process = 1 and a Button to proceed to the next form if the process = 0.
That's the easy part, the hard part is that if the process was = 0 when the form was loaded then the user opens IE, I want to remove the button and show the RichTextBox (which advises the user to close IE) and once again if they close IE the Button reappears.
I have the button and RichTextBox in the form_load with an If statement that shows depending on IE being open or not, but i cannot get them to swap over if IE is closed or opened, any help will be greatly appreciated.
Here is the code I have in the Form_load for the RTB and Button
aProc = Process.GetProcessesByName("iexplore")
If aProc.Length = 0 Then
Dim b1 As New Button
b1.Location = New System.Drawing.Point(274, 244)
b1.Name = "btnOK"
b1.Size = New System.Drawing.Size(75, 29)
b1.TabIndex = 5
b1.Text = "OK"
b1.UseVisualStyleBackColor = False
Me.Controls.Add(b1)
AddHandler b1.Click, AddressOf btn_OK
Else
Dim t1 As New RichTextBox
t1.Location = New System.Drawing.Point(170, 233)
t1.Name = "rtbMessage2"
t1.ReadOnly = True
t1.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
t1.Size = New System.Drawing.Size(293, 40)
t1.TabIndex = 5
t1.Text = ("Internet Explorer is Running - Please Close Internet Explorer to Continue")
Me.Controls.Add(t1)
AddHandler t1.Click, AddressOf btn_OK
End If
Two changes I would make:
Add a timer that continually calls the logic (show/hide the Button/RichTextBox)
Make the Button and RichTextBox always there, but initially invisible.
To do this, I would (on load) create the Button and RichTextBox with .Visible = false. Then create a timer that runs every 500 milliseconds (+/-). That timer would call a function that contains the logic you have above. However, instead of creating the controls (with that logic) just reference them and set their visibility.
In essence, create the controls once, run the logic multiple times.

Save User Settings With IsolatedStorage in Silverlight 5 (vb)

I want to save a value from a TextBox, an item of text from the SelectedIndex of a ComboBox and a CheckBox checked true or false. I then want to recall the saved settings with the OnClick from a button. I've had a go with the TextBox below but I get the following error statement: KeyNotFoundException was unhandled by user code. The given Key was not in the present dictionary?
Can anybody help me?
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
Dim Store As IsolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings
IsolatedStorageSettings.ApplicationSettings(TextBox1.Text) = TextBox1
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button3.Click
TextBox1 = (IsolatedStorageSettings.ApplicationSettings(TextBox1.Text))
End Sub
The ApplicationSettings works similar to a Hashtable. It requires a key to identify any setting you wish to store.
You can store your setting like this (air code):
IsolatedStorageSettings.ApplicationSettings("MyTextBoxKey") = TextBox1.Text
And you can retrieve your setting like this:
TextBox1.Text = CStr(IsolatedStorageSettings.ApplicationSettings("MyTextBoxKey"))
You can read more about the ApplicationSettings on MSDN