Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to be able to exit a new form I created, but since I manually made the form its does not have a handler in the withevents. So since I can not figure out for the life of me how to approach getting my form into the withevents, I came up with another possible solution.

I figured that I could make a keydown event inside the event that populates the new form. Right now it's not working, but I was hoping to ask someone if I am on the right track, and possibly if they know what I need to change?

Here is my code:

Dim dk As System.Windows.Forms.KeyEventArgs
     dk = sender
     If dk.KeyCode = Keys.Escape Then
             picform.Close()
         End If


What I have tried:

I have tried changing the dk value into an integer and also tried changing it to a key value, none have helped.
Posted
Updated 24-Jul-17 9:09am

Refactor (extract) the code into a new method (Sub/Function). Then you can call it from multiple places in your code. Example:

BEFORE:
VB
Public Class Form1

    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown

        ' code does something here!

    End Sub

End Class

AFTER:
VB
Public Class Form1

    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown

        NewMethod()

    End Sub

    Sub NewMethod()

        ' code does something here!

    End Sub

    Sub OtherMethod()

        NewMethod()

    End Sub

End Class

Updated with new requirement: For the new requirement, you need to use WithEvents to capture the KeyDown event.Here is a new example to reflect this.
VB
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenForm()
    End Sub

    Private WithEvents picform As New Form

    Sub OpenForm()

        picform.Height = 800
        picform.Width = 600
        ' picform.FormBorderStyle = FormBorderStyle.None
        picform.Show()

    End Sub

    Private Sub picform_KeyDown(sender As Object, e As KeyEventArgs) Handles picform.KeyDown

        MsgBox("Picformn keydown event caputred in parent form", MsgBoxStyle.Exclamation)

    End Sub

End Class
 
Share this answer
 
v2
Comments
Member 11856456 24-Jul-17 4:22am    
The only problem is where it says mybase.keydown, it suppose to be picform.keydown. I cant find this event in the withevents section, but I can see the form1. I need to call forth picform to perform this function. any suggestions?
Graeme_Grant 24-Jul-17 4:31am    
The Keydown event is for a form or control?

I read it for the form - ie: picform. The only difference is that my Form is called Form1. In Visual Studio's Properties toolbox, click on the lightning-bolt and all events for the form or control are listed.

If you mean the PictureBox control, use the PreviewKeyDown event instead.
Ralf Meier 24-Jul-17 4:44am    
Perhaps you should provide the COMPLETE code to the issue ...
For me it's impossible to give you an answer with this less Information ...
Member 11856456 24-Jul-17 5:05am    
so I have created this form,

Dim picform As New Form
picform.Controls.Add(control)
picform.Height = 800
picform.Width = 600
' picform.FormBorderStyle = FormBorderStyle.None
picform.Show()

but this is in another event, so I can not get a handle from it. So I am trying to figure out a way to close the newly created form from an escape key press.
Graeme_Grant 24-Jul-17 5:29am    
See update above.

When asking questions, you need to spell out exactly what you are trying to do, then you will get a better answer the first time.
So thanks to Graeme_Grants solution I was able to figure out the rest, I just needed a way to get the withevent handler.

 Private WithEvents picform As New Form

Dim picform As New Form
        picform.Controls.Add(expandpic)
        picform.Height = 800
            picform.Width = 600
        ' picform.FormBorderStyle = FormBorderStyle.None
        picform.Show()

        AddHandler picform.KeyDown, AddressOf picform_convert

    End Sub

    Private Sub picform_convert(ByVal sender As System.Object, ByVal e As KeyEventArgs)

        picform = CType(sender, Form)

        If e.KeyCode = Keys.Escape Then
            picform.Close()
            picform.Dispose()
        End If

    End Sub


Just figured out the rest, thanks for the help. now I know how to call forth an withevent handler for future reference.
 
Share this answer
 
Comments
Dave Kreskowiak 24-Jul-17 15:23pm    
WithEvents just means that event handlers specifying the Handles clause are wired up for you. If you can't use WithEvent, you have to wire up the events yourself with AddHandler, like you've done in your example.

Your example is a bit flawed. You're creating a new form instance variable, "picform", that is scoped inside a method and then showing in non-modal. Immediately after this method exists, the picform variable goes out of scope and the form you launched will eventually die when the garbage collector gets around to seeing that nothing holds a reference to it.

Your current form should also NOT be handling the events of another form. This is very bad practice, forever tying the two forms together. One cannot be reused without the other. The concept is called "separation of concerns". The picform should have been designed separately and the code to handle key presses put in that forms code, NOT in the code of another form.
Member 11856456 24-Jul-17 16:19pm    
Dave, I appreciate the response. Since I am new to coding looking up your "separation of concerns" was something I had to do. Since I do not know a lot about coding do you have any examples of creating what you are talking about or possible examples from other people. All my code is doing is creating a temporary form with a picturebox in it so people can enlarge their image. once they are done they can use the escape key and then it will close that temporary form. If there is a better, cleaner way to do this I would love to learn how.
Dave Kreskowiak 24-Jul-17 18:57pm    
Simple. A class, and a Form is a class, is responsible for one thing and one thing only.

A class can handle database access to a certain database. A class handles a business object, like a configuration. A class can represent a business process.

In the case of forms, it is responsible for displaying data and input against that data. It it responsible for only the controls on that form, NOTHING MORE.

If the case of your picturebox form, it is a small business process. It shows an image, which you have to pass in somewhere. That form allows some editing of that image, like changing its size and returns that altered image to the caller, your first form.

In all cases, in no way do you let one form handle the events of the controls on another form such as keystrokes and mouse moves and clicks.

You should not be building the picturebox form in the code of the first form. There's no need for that.

Your picturebox form should be shown with ShowDialog, not Show. This makes your first form stop and wait for the picturebox for, shown as a dialog box, to be dismissed before resuming.

I don't have time to build an example and there are tons and tons of articles on passing data between forms all over the web, even here on CP, codeproject pass data between forms[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900