Solved

VB.Net Create a form programatically and close when clicked

Posted on 2011-09-10
6
392 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 99
Create a Dropdown list with sub group 10 43
VB.net Filesystem watcher not working 5 58
SSIS - Using VB.NET to parse XML file 11 43
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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