Solved

VB.Net Create a form programatically and close when clicked

Posted on 2011-09-10
6
395 Views
Last Modified: 2012-05-12
Hi Experts.  Please can someone help.  I need to create a form programatically  ((just by clicking a button at this stage)) that contains two labels containing data but have it so that the form closes when clicked.   There maybe multiple instances of the form at one time containing with different data in the labels, so I think they need to be on different threads.  The user may choose to leave some instances of the form open and close others.   Im struggling with multithreading, creating the labels and closing the form when clicked.  Many Thanks in advance
0
Comment
Question by:PNRT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36516479
Why is multi-threading involved here?

To create multiple instances of a Form from a Button click:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f2 As New Form2
        f2.Show()
    End Sub

Forms close when you click the 'X' in the upper right.  Are you wanting to create a BORDERLESS form?  Where would it be hosted then?...does it need to be moved around?

Please describe the "big picture" here.
0
 
LVL 2

Author Comment

by:PNRT
ID: 36516533
Hi - thansk for the reply

From your code example I see one of my mistakes already.  I was creating a new instance of "Form" not a new instance of a "form2" already created.  So it was closing and reopening itself each time.

But the form is borderless, so can you suggest how I would close it by clicking on it?

Thanks again for you help
0
 
LVL 40
ID: 36516546
In the Form2 code, program the Click event of the form with Me.Close.

If this does not work, set the KeyPreview property of the form to True.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36516577
You'll need to handle the event for not only the form, but all of the labels as well.  Just list all of them after the "Handles" keyword like this:
Public Class Form2

    Private Sub All_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click, Label1.Click, Label2.Click
        Me.Close()
    End Sub

End Class

Open in new window

0
 
LVL 2

Author Comment

by:PNRT
ID: 36516593
Thats working perfectly.  Thank you.
I need to automatlically close these forms automatically after a period of time.
but I'll ask that as a seperate question.
Thanks again.
0
 
LVL 40
ID: 36516596
Idle_Mind is right.

I do not know where my head was, KeyPreview has not impact on the Mouse.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question