-
Jun 21st, 2017, 06:39 AM
#1
Thread Starter
Lively Member
Are there something wrong with this?
I'm making a web browser based on CEFsharp. But, when I make the program to wait for user's key press then do the code, it DOESN'T work, like it doesn't do the code. Here's the code:
Code:
Imports CefSharp.WinForms
Imports CefSharp
Public Class Form1
Private WithEvents browser As ChromiumWebBrowser
Public Sub New()
InitializeComponent()
Dim settings As New CefSettings()
CefSharp.Cef.Initialize(settings)
browser = New ChromiumWebBrowser("https://google.com") With {
.Dock = DockStyle.Fill
}
Panel1.Controls.Add(browser)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
browser.Back
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
browser.Forward
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
browser.Reload
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
browser.Load(TextBox1.Text)
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
browser.Load(TextBox1.Text)
End If
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown 'here's the problem
If e.KeyCode = Keys.Back Then
browser.Back
End If
If e.KeyCode = Keys.Pause Then
Me.Close()
End If
If e.KeyCode = Keys.A Then
browser.ShowDevTools
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
browser.Load("https://google.com")
End Sub
End Class
Help me :/
Last edited by Shaggy Hiker; Jun 21st, 2017 at 10:04 AM.
Reason: Added CODE tags.
-
Jun 21st, 2017, 09:19 AM
#2
Re: Are there something wrong with this?
The Form's Key events won't fire if there is a control on the form that accepts Keystrokes.
In the Form's properties list there is an Item named "KeyPreview". If you set that to True then all keystroke events will be sent to the form as well as the control.
Of course if someone is typing in a textbox and hits the backspace key to delete a character you wouldn't want the browser to switch back to a previous page so I'm not sure how you plan to prevent that.
-
Jun 21st, 2017, 10:05 AM
#3
Re: Are there something wrong with this?
Welcome to the forum. I edited your post to add [CODE][/CODE] tags to improve the appearance. You can do this by pressing the # button and pasting the code between the resulting tags.
My usual boring signature: Nothing
-
Jun 21st, 2017, 11:42 AM
#4
Thread Starter
Lively Member
Re: Are there something wrong with this?
Of course I don't make the user when press backspace while typing in the textbox to return to the previous page :v But I set the KeyPreview to True and I don't see anything :V
-
Jun 21st, 2017, 09:00 PM
#5
Thread Starter
Lively Member
Re: Are there something wrong with this?
Ah yeah, I saw the problem, when I typing in the textbox, I pressed backspace and it returns to the previous page, but why it is relate to the textbox?
-
Jun 21st, 2017, 09:03 PM
#6
Thread Starter
Lively Member
Re: Are there something wrong with this?
-
Jun 22nd, 2017, 11:35 AM
#7
Re: Are there something wrong with this?
If you don't have KeyPreview on, the keystrokes will go to whatever control that is currently active and the Form will not receive any Key events.
If you have controls that can accept Input focus (e.g. buttons and textboxes), then one of those will always be active and be receiving Key events and the form will never receive Key events.
You can turn on KeyPreview, then the form will receive all key events for all controls, and of course the active control will also receive the event. I haven't tested it in .Net, but in VB6 and earlier, the form received the event first and could cancel sending on the event to the currently active control. I didn't try or read the documentation to see if that is still a capability in .Net.
There are usually two methods used to handle the situation you have.
One is not to use KeyPreview and instead have a control that the user doesn't see (located outside the bounds of the form) which you set the focus to whenever you want to be able to process "global" key contexts, so instead of using a Form Key event handler, you would put the code in that control's Key event handler.
Whenever the user pressed Enter in the textbox, or clicked on one of the buttons, you would set the focus back to your "global" key processor control, so it could process "Browser Level" actions.
A second option is to use the KeyPreview option, and set a flag to not process keys in the form Key event if the Key has meaning to the control that has the focus. I.e. Set the flag in the Textbox's TextChanged event and skip testing for Keys in the Form Key event if the flag is set. Clear the flag so that keys are processed in the Form's Key event whenever you press the Enter Key in the textbox, or press one of the buttons. That would disable the backspace and "A" key processing at the form level if you're typing in the textbox, but re-enable processing them when you're done typing in the textbox (assuming you click a button or pressed Enter to navigate away from the current page).
Last edited by passel; Jun 22nd, 2017 at 11:40 AM.
-
Jun 22nd, 2017, 12:51 PM
#8
Thread Starter
Lively Member
Re: Are there something wrong with this?
Use the flag? I have never heard of it...
-
Jun 22nd, 2017, 02:34 PM
#9
Re: Are there something wrong with this?
A flag is a variable (usually a Boolean) to indicate the state of something.
In this case you might create it like this:
Code:
Private ignoreThisKey as Boolean = False
...and then set it to True or False as appropriate based on passel's instructions.
-
Jun 22nd, 2017, 03:00 PM
#10
Re: Are there something wrong with this?
{edit} I was offline a bit too long so si_the_geek slipped in while I was writing this, which is good since it could have been much longer before I came back, you never know.{/edit}
Flag is just a term referring to a variable that is used to "flag", i.e. "signal" some state to your code.
In your case, you want to signal to your Form Key event code that it should not process key events (i.e. it should ignore them).
Depending on what you want the variable (i.e. flag) to mean when it is True should be represented by its name, so it makes sense when you use it in your code.
As an example, I added the boolean variable "ProcessKeys" below to serve as a flag in the code.
If we start typing in the textbox, the flag is set to false so the form key event handler doesn't respond to any keys.
If we hit Enter in the textbox , then focus is set to the button that would do the same thing as the Enter key so we don't erase characters, or type "a" in the textbox when we want the form to process those keys.
Code:
Imports CefSharp.WinForms
Imports CefSharp
Public Class Form1
Private WithEvents browser As ChromiumWebBrowser
Private ProcessKeys As Boolean = True 'Set True when we want form to process key events
Public Sub New()
InitializeComponent()
Dim settings As New CefSettings()
CefSharp.Cef.Initialize(settings)
browser = New ChromiumWebBrowser("https://google.com") With {
.Dock = DockStyle.Fill
}
Panel1.Controls.Add(browser)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
browser.Back
ProcessKeys = True 'Process keys in the Form Key events
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
browser.Forward
ProcessKeys = True 'Process keys in the Form Key events
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
browser.Reload
ProcessKeys = True 'Process keys in the Form Key events
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
browser.Load(TextBox1.Text)
ProcessKeys = True 'Process keys in the Form Key events
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True 'prevent the beep from hitting Enter key in a non-multiline textbox
browser.Load(TextBox1.Text)
Button4.SetFocus() 'remove focus from textbox so that keys don't process here
ProcessKeys = True 'Process keys in the Form Key events
Else
ProcessKeys = False 'Don't process keys in the Form Key events (process them here)
End If
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown 'here's the problem
If ProcessKeys Then 'If we're not typing in the textbox then, process keys here
If e.KeyCode = Keys.Back Then
browser.Back
End If
If e.KeyCode = Keys.Pause Then
Me.Close()
End If
If e.KeyCode = Keys.A Then
browser.ShowDevTools
End If
End If 'ProcessKeys
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
browser.Load("https://google.com")
ProcessKeys = True 'Process keys in the Form Key events
End Sub
End Class
Personally, I would probably combine the button click events into one handler and use a case statement to process the unique portion.
Code:
Private Sub Button_Click(sender As Object, e As EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click,
Button4.Click, Button5.Click
Select Case True
Case Is Button1 : browser.Back
Case Is Button2 : browser.Forward
Case Is Button3 : browser.Reload
Case Is Button4 : browser.Load(TextBox1.Text)
Case Is Button5 : browser.Load("https://google.com")
End Select
ProcessKeys = True 'Process keys in the Form Key events
End Sub
If the case only has a single line of code, I usually use the statement separator ":" to inline the case on one line of the file for compactness as shown above.
If there were two or more lines in the case, I would use the normal block format, with the case code following the case statement, like follows
Code:
Select Case True
Case Is Button1
browser.Back
Case Is Button2
browser.Forward
Case Is Button3
browser.Reload
Case Is Button4
browser.Load(TextBox1.Text)
Case Is Button5
browser.Load("https://google.com")
End Select
Your preference, of course.
-
Jun 22nd, 2017, 09:29 PM
#11
Thread Starter
Lively Member
Re: Are there something wrong with this?
The code:
is not a member of Button :v
-
Jun 22nd, 2017, 09:39 PM
#12
Re: Are there something wrong with this?
Originally Posted by Quang-vip
The code:
is not a member of Button :v
There is a Focus method but, to set focus to a WinForms control, an application developer should call its Select method. If you want no control to have focus, set the form's ActiveControl property to Nothing.
-
Jun 22nd, 2017, 10:31 PM
#13
Thread Starter
Lively Member
Re: Are there something wrong with this?
Is this right?
Code:
Imports CefSharp.WinForms
Imports CefSharp
Public Class Form1
Private WithEvents browser As ChromiumWebBrowser
Private ProcessKeys As Boolean = True
Public Sub New()
InitializeComponent()
Dim settings As New CefSettings()
CefSharp.Cef.Initialize(settings)
browser = New ChromiumWebBrowser("https://google.com") With {
.Dock = DockStyle.Fill
}
Panel1.Controls.Add(browser)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
browser.Back
ProcessKeys = True
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
browser.Forward
ProcessKeys = True
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
browser.Reload
ProcessKeys = True
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
browser.Load(TextBox1.Text)
ProcessKeys = True
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
browser.Load(TextBox1.Text)
Me.Select()
ProcessKeys = True
End If
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If ProcessKeys Then
If e.KeyCode = Keys.Back Then
browser.Back
End If
If e.KeyCode = Keys.Pause Then
Me.Close()
End If
If e.KeyCode = Keys.F12 Then
browser.ShowDevTools
End If
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
browser.Load("https://google.com")
ProcessKeys = True
End Sub
End Class
-
Jun 22nd, 2017, 11:24 PM
#14
Re: Are there something wrong with this?
Originally Posted by Quang-vip
Is this right?
Did it work?
-
Jun 22nd, 2017, 11:39 PM
#15
Thread Starter
Lively Member
Re: Are there something wrong with this?
-
Jun 23rd, 2017, 12:17 AM
#16
Re: Are there something wrong with this?
Originally Posted by Quang-vip
Nope
Then obviously it's not right, so why even ask that question? The obvious thing is to do is tell us that it didn't work and explain exactly what did happen. Why be cryptic when that just makes it harder for us to help you?
-
Jun 23rd, 2017, 12:42 AM
#17
Thread Starter
Lively Member
Re: Are there something wrong with this?
Later I check it then I post this :v
-
Jun 23rd, 2017, 12:42 AM
#18
Thread Starter
Lively Member
Re: Are there something wrong with this?
So, In conclusion, it didn't work
-
Jun 23rd, 2017, 12:46 AM
#19
Re: Are there something wrong with this?
So, you're refusing to explain what it actually did do?
-
Jun 23rd, 2017, 02:22 AM
#20
Thread Starter
Lively Member
Re: Are there something wrong with this?
-
Jun 23rd, 2017, 02:41 AM
#21
Re: Are there something wrong with this?
When you execute that code, it does something. You've told us that it doesn't do what you expect. OK then, so what does it do? Would you go to the doctor and expect to be diagnosed without describing your symptoms? What actually happens is important because it gives us an idea what we're looking for. How is this not obvious?
-
Jun 23rd, 2017, 02:45 AM
#22
Thread Starter
Lively Member
Re: Are there something wrong with this?
Ok ok, when I compile/build it, it doesn't have any error, but when I open the program, not as I expected...
-
Jun 23rd, 2017, 02:47 AM
#23
Thread Starter
Lively Member
Re: Are there something wrong with this?
Like, I have to click the mouse in the textbox so the codes in Form1 can be executed
-
Jun 23rd, 2017, 03:21 AM
#24
Re: Are there something wrong with this?
Are you saying, without actually saying, that you want your code to set focus to a TextBox and it isn't doing that? If so then exactly what part of that code are you expecting to do that? I told you explicitly that you need to call Select on a control if you want that control to get focus. Are you calling Select on that TextBox? I can't see anywhere that you're doing that.
-
Jun 23rd, 2017, 03:26 AM
#25
Thread Starter
Lively Member
Re: Are there something wrong with this?
I want to set focus on Form1 after typing in the textbox
-
Jun 23rd, 2017, 03:33 AM
#26
Re: Are there something wrong with this?
Is the TextBox on Form1? If so then Form1 already has focus. In that case, maybe read this again:
If you want no control to have focus, set the form's ActiveControl property to Nothing.
If not then you call Activate on a form to make it the active form.
-
Jun 23rd, 2017, 03:39 AM
#27
Thread Starter
Lively Member
Re: Are there something wrong with this?
Code:
If you want no control to have focus, set the form's ActiveControl property to Nothing.
How?
-
Jun 23rd, 2017, 04:05 AM
#28
Re: Are there something wrong with this?
If you don't know how to set a property then I suggest that you follow the tutorial link in my signature below to learn the basics. That said, you're already setting properties in the code you posted so it would appear that you do know how but are just not thinking. Either way, I'm not here to spoon-feed anyone. You can wait for someone else who is happy to do that or engage your brain.
-
Jun 23rd, 2017, 04:09 AM
#29
Thread Starter
Lively Member
Re: Are there something wrong with this?
I have streeses these days,...... :/
-
Jun 23rd, 2017, 04:22 AM
#30
Thread Starter
Lively Member
Re: Are there something wrong with this?
-
Jun 23rd, 2017, 04:47 AM
#31
Thread Starter
Lively Member
Re: Are there something wrong with this?
........ I am sooooo tired of this........
-
Jun 23rd, 2017, 04:54 AM
#32
Re: Are there something wrong with this?
Excellent. That is why I push people to think for themselves. I generally assume that they can but just aren't for whatever reason but will if given a reason to do so. Some refuse to regardless. Some make the effort, as in this case.
-
Jun 23rd, 2017, 04:57 AM
#33
Thread Starter
Lively Member
Re: Are there something wrong with this?
So the
Code:
ActiveControl = Nothing
must in the textbox?
-
Jun 23rd, 2017, 04:57 AM
#34
Thread Starter
Lively Member
Re: Are there something wrong with this?
I mean, in the textbox's code
-
Jun 23rd, 2017, 05:33 AM
#35
Re: Are there something wrong with this?
Originally Posted by Quang-vip
I mean, in the textbox's code
The TextBox doesn't have any code, or at least none that you have access to. Your code is part of the form. If you have a method in that form that handles an event of a TextBox, it's still part of the form, not of the TextBox.
Originally Posted by Quang-vip
So the
Code:
ActiveControl = Nothing
must in the textbox?
It must go wherever it is that you want to remove focus from any control.
-
Jun 23rd, 2017, 06:01 AM
#36
Thread Starter
Lively Member
Re: Are there something wrong with this?
I mean, the
Code:
ActiveControl = Nothing
in this case, must in the textbox?
-
Jun 23rd, 2017, 09:06 AM
#37
Re: Are there something wrong with this?
Where you put a piece of code depends on when you want it to run.
For example, if you want a piece of code to run when Button1 is clicked, you can put it in the Button1_Click event (not in one of the other events for Button1, or events for anything else). If you want something to happen when Button5 gets the focus, you put that code in the event that occurs when Button5 gets the focus (not in the click event for that button).
What you told us you want is this:
I want to set focus on Form1 after typing in the textbox
...you have hinted in a few posts that you know which object the event is for, so you just need to work out which event of that object to use (and in this case there are several valid options).
At that point you should be trying at least one of them, to see if you can make it work. If you can't get it to work, show us what you have tried and explain what is wrong... not just "it didn't work" or "not as I expected" because we can't tell what you mean, give us some details (eg: if something shows an error message, tell us what it is and what line of code it happened on; if something unexpected happens, tell us what you expected and what happened instead).
It shouldn't take 5 posts to persuade you to do that (you should try to always do it), but it has been over 20 posts so far.
We understand that it seems easier for you if we do everything for you, but it means that you learn much slower so you get many more problems in future. You also have to bear in mind that we are random strangers who are giving up our own time to help you, and we are less likely to do that if you don't make an effort yourself.
-
Jun 23rd, 2017, 10:00 AM
#38
Thread Starter
Lively Member
Re: Are there something wrong with this?
Code:
At that point you should be trying at least one of them, to see if you can make it work. If you can't get it to work, show us what you have tried and explain what is wrong... not just "it didn't work" or "not as I expected" because we can't tell what you mean, give us some details (eg: if something shows an error message, tell us what it is and what line of code it happened on; if something unexpected happens, tell us what you expected and what happened instead).
It shouldn't take 5 posts to persuade you to do that (you should try to always do it), but it has been over 20 posts so far.
We understand that it seems easier for you if we do everything for you, but it means that you learn much slower so you get many more problems in future. You also have to bear in mind that we are random strangers who are giving up our own time to help you, and we are less likely to do that if you don't make an effort yourself.
jmcilhinney said the same :v
-
Jun 23rd, 2017, 10:10 AM
#39
Thread Starter
Lively Member
Re: Are there something wrong with this?
So, anyways, I put the
Code:
ActiveControl = Nothing
in the textbox1 so that when I done typing in it (and press the Enter key), it won't process keys in the textbox1 anymore, instead of that it will process the key in the Form1 Event. My problem is, when I have done typing in the textbox1 and press Enter, It still process the keys in the textbox1 :v
-
Jun 23rd, 2017, 10:12 AM
#40
Thread Starter
Lively Member
Re: Are there something wrong with this?
Ah, I forgot, After the
Code:
ActiveControl = Nothing
is
Code:
ProcessKeys = True 'this code processes keys in the form event
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|