Solved

VB.Net Create a form programatically and close when clicked

Posted on 2011-09-10
6
384 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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridview Currency Formating? 22 53
Showdialog 8 29
Protecting vb6 & .Net code Obfuscation 18 97
DataGridView does not show data rows correctly in vb.net 2013 3 31
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…
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 …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

16 Experts available now in Live!

Get 1:1 Help Now