Page 1 of 2 12 LastLast
Results 1 to 40 of 41

Thread: Are there something wrong with this?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    Help :/

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    Use the flag? I have never heard of it...

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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.

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    The code:
    Code:
     Button4.SetFocus()
    is not a member of Button :v

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Are there something wrong with this?

    Quote Originally Posted by Quang-vip View Post
    The code:
    Code:
     Button4.SetFocus()
    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.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Are there something wrong with this?

    Quote Originally Posted by Quang-vip View Post
    Is this right?
    Did it work?

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    Nope

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Are there something wrong with this?

    Quote Originally Posted by Quang-vip View Post
    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?

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    Later I check it then I post this :v

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    So, In conclusion, it didn't work

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Are there something wrong with this?

    So, you're refusing to explain what it actually did do?

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    What?

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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?

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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...

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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

  24. #24
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    I want to set focus on Form1 after typing in the textbox

  26. #26
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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?

  28. #28
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    I have streeses these days,...... :/

  30. #30

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    ActiveControl huh?......

  31. #31

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    ........ I am sooooo tired of this........

  32. #32
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  33. #33

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    So the
    Code:
    ActiveControl = Nothing
    must in the textbox?

  34. #34

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    I mean, in the textbox's code

  35. #35
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Are there something wrong with this?

    Quote Originally Posted by Quang-vip View Post
    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.
    Quote Originally Posted by Quang-vip View Post
    So the
    Code:
    ActiveControl = Nothing
    must in the textbox?
    It must go wherever it is that you want to remove focus from any control.

  36. #36

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    Re: Are there something wrong with this?

    I mean, the
    Code:
    ActiveControl = Nothing
    in this case, must in the textbox?

  37. #37
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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.

  38. #38

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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

  39. #39

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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

  40. #40

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Location
    Vietnam
    Posts
    89

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width