Solved

VB.Net Create a form programatically and close when clicked

Posted on 2011-09-10
6
381 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 85

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 1

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 85

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 1

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now