ARTICLE

Using static variable and Activated event for building "Home" button of Windows application

Posted by Michael Livshitz Articles | Visual Basic 2010 December 08, 2006
In this tutorial I will share experience how , just with the help of a few clicks and a few lines of code , to build a windows application with "Home" button, that allows to return to "Home" form (in fact to close all opened forms by one click) . The examples are written using VB.Net.
 
Reader Level:

In this tutorial I will share experience how, just with the help of a few clicks and a few lines of code, to build a windows application with "Home" button, that allows to return to "Home" form (in fact to close all opened forms by one click). The examples are written using VB.Net.  

Before we jump into VB.Net code we have to choose the best way to build solution of our Windows application.

(Thanks, thanks DOT.NET ! Now we have inheritance, user controls, and so on. And we must use that!).

I strongly recommend to include in our solution at least two projects with different "Output Type":

  • Output Type of the first project is "Windows Aplication" ; 
  • Output Type of the second project is "Class Library".

Before using any control in the first project, you have to create a control in the second project (for example, to create Button_C control that inherits from standard Button control ) and to use only controls from the second project on your forms.

This process takes no more than 20 seconds but advantage is obvious: at any time just for some seconds you can add to your control all that you want ( properties, events, color, and so on ). And just in seconds all addings and changes work on every your form (ten forms or hundred - it doesn't matter!).

Let's create our test project.

Step 1. Make a new solution named " WinProjects_Test".

Step 2. Add a "Windows Application" project named  "CloseForms".

Step 3. Add a "Windows Control Library" project named "CloseForms_Controls".

Step 4. Delete "UserControl1" from "CloseForms_Controls" project (we just don't need it).

Step 5. Add Windows Form named  "FormBasic" to "CloseForms_Controls" project. Change property BackColor to "Cornsilk" (just to see that it is our form with our BackColor).

Step 6. Build "CloseForms_Controls" project (right-click and select Build). After every changes it is recommended to rebuild the project !

Step 7. Build "CloseForms" project.

Step 8. To References of  "CloseForms" project add a reference to CloseForms_Controls.dll

(Right-click References of  "CloseForms" project, select Add Reference, from a tabbed dialog box select .NET Framework and with the help of "Browse ...". Find out the physical CloseForms_Controls.dll on the disk. Double-click CloseForms_Controls.dll to add it to the selected components list. Click OK to add this reference to the project).

Step 9. Rebuild solution.

Step 10. All our forms have to be inherited from FormBasic form. So far there is only one form in  our "CloseForms"  project : this is Form1. Open code of Form1, find out the line           

Public Class FormBasic

    Inherits System.Windows.Forms.Form

and change it to          

Public Class Form1

    Inherits CloseForms_Controls

Step 11. OK. Now our ship is ready for sailing off. We just have to  add a few forms and user controls to make our travel more interesting. Add Form2, Form3, Form4 to  "CloseForms" project and make them inherited from FormBasic. (To add inherited form to "CloseForms" project you can just right-click and select Add|Add Inherited Form ,then from Add New Item dialog box select Inherited Form and then from Inheritance Picker dialog box select our FormBasic).

Step 12. Add User Control , named Button_C, to CloseForms_Controls project. Open code of this control, find out line          

Public Class Button_C

    Inherits System.Windows.Forms.UserControl

and change it to          

Public Class Button_C

    Inherits Button

 

Now we have our own Button_C control with all properties, events of Button control. "To prove" that this is ours control let's change default property BackColor : double-click on Button_C.vb, right-click on the page (Button_C.vb[Design]) and select Properties, set BackColor to, for example, "AliceBlue". (Do you remember ?! After every changes it is recommended to rebuild the solution).

Step 13. Add one more User Control, named Buttons, to CloseForms_Controls project. Double-click on Button_C.vb, inside Toolbox click on "My User Controls" tab and drag and drop Button_C. Name it "button_CHome" with the text "Home". Drag and drop two more Button_C : button_CBack with the text "Back" and button_CNext with the text "Next". Pay attention ! We have created Buttons control with "Home" button. But, in fact, we could add "Home" button at any moment ("on demand" of the boss) without any serious problems (Don't forget to rebuild solution !).

Step 14. You should force to work buttons according to your desire on any form. With this purpose add to class Buttons three event Handlers:

#region "forClass"

Event ClickNext As EventHandler

Public Event ClickBack As EventHandler

Public Event ClickHome As EventHandler

#endregion

Open design of the Buttons.vb and double-click on the button_CNext button. Add the next code (to "private void button_CNext_Click"):

Private Sub button_CNext_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    If Not (ClickNext Is Nothing) Then

        ClickNext(Me, e)

    End If

End Sub 'button_CNext_Click

Similar "process" have to be done for the button_CBack and button_CHome controls:

Private Sub button_CBack_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    If Not (ClickBack Is Nothing) Then

        ClickBack(Me, e)

    End If

End Sub 'button_CBack_Click

 

Private Sub button_CHome_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    If Not (ClickHome Is Nothing) Then

        ClickHome(Me, e)

    End If

End Sub 'button_CHome_Click

 

Our control is now ready to act.

Step 15. Now we can add some "actions" to our project. Inside CloseForms project from "My User Controls" tab drag and drop on every form Buttons control. Double-click on Form1.vb , right-click on Buttons1 control and select Properties, click "Events" button, double-click on "ClickBack", then - on "ClickHome", and then - on "ClickNext". Now Form1.vb code page has three new functions: buttons1_ClickBack, buttons1_ClickHome, buttons1_ClickNext.

Similar "clicks" have to be done for the Form2, Form3, Form4.

Step 16. Add a few lines of code to Form1.vb  code page (to above mentioned functions):

Private Sub buttons1_ClickBack(ByVal sender As Object, ByVal e As System.EventArgs)

    'Just to see that our ClickBack event can do something

    MessageBox.Show("This is ClickBack. There is no any 'Back' way ! " + "We are on the Home Form ! ", "CloseForms Project")

End Sub 'buttons1_ClickBack

 

Private Sub buttons1_ClickHome(ByVal sender As Object, ByVal e As System.EventArgs)

    'Just to see that our ClickHone event can do something

    MessageBox.Show("This is ClickHome. There is no any 'Home' way ! " + "We are on the Home Form ! ", "CloseForms Project")

End Sub 'buttons1_ClickHome

 

Private Sub buttons1_ClickNext(ByVal sender As Object, ByVal e As System.EventArgs)

    'To open the next form ( in this case : Form2 )

    Dim fOpen As New Form2()

    fOpen.ShowDialog()

End Sub 'buttons1_ClickNext

fOpen.ShowDialog ();

 

To Form2 (only for "buttons1_ClickNext"):

 

Private Sub buttons1_ClickNext(ByVal sender As Object, ByVal e As System.EventArgs)

    'To open the next form ( in this case : Form3 )

    Dim fOpen As New Form3()

    fOpen.ShowDialog()

End Sub 'buttons1_ClickNext

 

To Form3 (only for "buttons1_ClickNext"):

 

Private Sub buttons1_ClickNext(ByVal sender As Object, ByVal e As System.EventArgs)

    'To open the next form ( in this case : Form4 )

    Dim fOpen As New Form4()

    fOpen.ShowDialog()

End Sub 'buttons1_ClickNext

 

To Form4 (only for "buttons1_ClickNext"):

 

Private Sub buttons1_ClickNext(ByVal sender As Object, ByVal e As System.EventArgs)

    'To open the next form ( in this case : Form2 )

    Dim fOpen As New Form2()

    fOpen.ShowDialog()

End Sub 'buttons1_ClickNext

 

Now you have possibilities to travel from form to form without any restrictions :

Form1-Form2-Form3-Form4-Form2-Form3-...

But ... only  forward ( not "backward and forward").

Step 17. In order to return "step by step" to the previous forms  add to Buttons.cs code page function "closeForm()"

Private Sub closeForm()

    If ParentForm.Name <> "Form1" Then

        ParentForm.Close()

    End If

End Sub 'closeForm

 

and now add this function to "button_CBack_Click" :

 

Private Sub button_CBack_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    If Not (ClickBack Is Nothing) Then

        ClickBack(Me, e)

        closeForm()

    End If

End Sub 'button_CBack_Click

 

Step 18. We closely have approached to our goal: there are tens (or may be hundreds) of opened forms and you should close all forms except for the Form1 (just to return to the "Home" form) by means only one click. OK ! We are going to do that by adding a few lines of code (without using collection of opened forms, and so on).

First of all you have to add some "command" to close all form. Certainly this command should be clear and visible for all forms. Let it be some variable : public static bool closeForms. Of course while  loading any form we have to command : "Don't close now !" (closeForms = false;). Well. While closing any form (I don't mean Form1) the previous one is activated . OK !

We can catch this and , if there is command " To close forms "  (closeForms = true;), we can close this form too ... and so on. And at last we should define who will give a command "Home". Of course it will be our "Buttons" control  and "button_CHome". Well, now we shall pass to action.

Double-click on FormBasic.vb, right-click on FormBasic[Design] and select Properties, click "Events" button, double-click on "Activated". 

Add to FormBasic.vb code page the next code :

#region "forClass"

Public Shared closeForms As Boolean 'command to close all forms

#endregion

 

Private Sub FormBasic_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    closeForms = False 'command : "Don't close now !"

    'just to inform where we are and to hide ControlBox

    If Me.Name <> "Form1" Then

        Me.Text = " My name is " + Me.Name

        Me.ControlBox = False

    Else

        Me.Text = "Home Form. Where you would not sail you can " + "come back to me by means of one click !!!"

        Me.ControlBox = True

    End If

    'to make control width depended on length of the text

    Me.Width += Me.Text.Trim().Length * 4

End Sub 'FormBasic_Load

 

Private Sub FormBasic_Activated(ByVal sender As Object, ByVal e As System.EventArgs)

    'to catch command "To close forms"

    If Me.Name <> "Form1" And closeForms Then

        Me.Close()

    End If

End Sub 'FormBasic_Activated

 

Open Buttons.cs code page and add command "Close forms ! " (that is "Go home ! ") to button_CHome_Click :

 

Private Sub button_CHome_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    If Not (ClickHome Is Nothing) Then

        ClickHome(Me, e)

        FormBasic.closeForms = True 'command : "Close Forms ! "

        closeForm()

    End If

End Sub 'button_CHome_Click

 

That is all ! Now you may test the project.

Just run your project and open (click on the "Next" button) as many forms as you want. Click on "Home" button and ... All your forms will have been closed and you will have arrived "home".

CONCLUSION

In order to return to your "Home"  form in some Windows Forms project all that you need are only a few things . You have to

  • build solution in the way described above ( pay attention ! It can help in many cases), 
  • use inheritance for creating forms ( that is use a BaseForm),
  • use a user control for creating your "Home" button, 
  • initialize some public static bool variable to command "Go home" (or "Close forms" , it doesn"t matter)
  • and then using a few lines of code and Activated event to catch the command 
  • and close all forms except your "Home" form.

Good luck in programming !

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).

share this article :
post comment
 

Dear Amit!
For win forms you  don't need any code to do that.
You just have to set property the 'AutoCompleteMode' to 'SuggestAppend' and the property 'AutoCompleteSourse' to 'ListItems'.
For ASP.Net, to do the same, you need special control with JavaScript and, unfortunately, now I have no opportunity to help you with this control.
With the best regards.
Michael

Posted by Michael Livshitz Dec 14, 2006

tell me the entire coding of sorting the name

suppose there is a n number of students in the school when we click the s character the drop down list must start with student name starting with s prior student name should be upword of that name

Posted by amit gambhir Dec 11, 2006
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor