Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.Net Create a form programatically and close when clicked

Posted on 2011-09-10
6
Medium Priority
?
400 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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