Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Building Windows Application (Address Book) - Part VII (New)
|
Building Windows Application (Address Book) - Part VII
Hai everybody,
So back into action once again. After a long break, I am continuing my address book developement process. Let me try and finish this developement process and present you the address book.
Ok, now with out wasting time let me continue my article numbered VII. As usual, let me give a tip and continue the development process.
Tip of the article.
I am going to give you a tip which will not help you in your development process,but will definetly help you.
Now, I am going to tell about Region in VB.Net. See the following pic.
click here for image
You could see and also . These two represent the Compressed and expanded region.
Actually, this regions are used to make imaginary division inside our code. Though this regions are of no use in programming or doesn't mean anything to the CLR.
For example, if you see the above picture which displays the region . This is the region which is generated by the IDE, containing the code generated by the IDE which contains the code required for loading the form.
Similary, we can also build our own region. Syntax for defining the region.
#Region "Label for the region"
................... ................... ...................
#End Region
Using this we can make logical division in our code. Lets not get deep into this. My intention in telling you this tip is to introduce you to new concepts or things and make you think "How this can be implemented ?".
Ok now, Lets get back to the business.
Back to Address Book Development (Click here to download the sample)
Address Book
Please, read my previous articles of this series and continue with this article.
In my last article "Part VI", I was talking about loading the events at runtime. Now, before getting into the development of address book I need to clear one thing, that is Creating Custom controls.
First lets see what is this custom controls. To explain, this let me use the common term I hope every one buy clothes. There are two types of clothes.
- Ready-made Clothes
- Tailor-made Clothes
What is this Ready-made and Tailor-made. The first one is already designed one which is similar to our Predefined controls (like textbox and other), which has its own size, color and stuffs like that, preety cool one which makes our job easier,
(prevents us from struggling with the tailor. But, this has its own drawback, I mean it may not fit every one, though it is easy for use.
The latter one is the tailor made clothes, which we design to our needs, we select the material, we select the color, we tell the measurement, we tell the design, its purely the dress of our liking. Which is best (no need to adjust). But it is quite time consuming, but a good one.
Similarly predifined controls are the one which has its features which does some specific task and we can them if they fit into our requirement overriding some properties.
Whereas, custom control is the one which we are going to build for our need and which will be used when ever required. For example, lets take the control, Textbox. Textboxes are meant for the getting the user inputs. Where they take any type of inputs. Which can also be made to accept inputs in a specific format. That is say the textbox which takes only integers. Though it can be done, we need to do the coding for all the textbox which are required. What if we want to reuse the code. In OOPs programming, our main objective is reusability. What is this reusability ?. Does it not sound funny.
Reusability, is used to avoid the repeation of code block (Create once, Use it when required). So reusbility is for the "lazy man" who does the job once and uses that job. Is that so ? My answer is......
A big No.
It is the way you work smarter and efficiently, So it is... yes, it is Smart Men Job. I prefer to be a smart developer, gather than the developer who works hard. I hope you will be a smart developer.
Wasting time.. Cool... Lets get back to bussiness.
Now, in this part of article lets see the following things.
- Something more about Custom Controls
- How to build Custom Controls
- Building the custom controls
- How to use them
- Judgement Call, when to use custom control and when to use predefined controls
- How are we going to use the custom controls in our Address book
I believe I don't need to define these custom controls. Please write me your definitions for custom controls to me at sadhasivam1981@gmail.com or sadhasivam1981@yahoo.com.
Something more about custom Controls
I could classify this controls into two major category,
- Web User Control
- Windows User Control
In this section of article we are going to look more on Windows User Control. Basically, these user controls are built inorder to simplify our coding. For example, consider that we are developing a Finance module. Definetly, which will involve text boxes which takes only numerals. The following is the code involved for this operation.
Private Sub TextBox1_KeyPress(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles _ TextBox1.KeyPress If Asc(e.KeyChar) = 8 Then
ElseIf Not IsNumeric(e.KeyChar) Then e.Handled = True End If End Sub
The above logic has to be implemented wherever we need such a functionality. Here, instead of writing the same code everywhere, why don't we build a control of our own and use it every where. Just think about it. Proceed further if you are not clear mail me your doubt and get it cleared and proceed further.
How to build Custom Controls
Now coming back to our main stream. True, creating the user controls or custom controls is worthy. But what are we going to create is a big question. Which you need to decide.
I can classify these custom controls or the controls which I am going to create in two ways,
- Only one control with some additional functionality
- Custom control with some combination of controls which does some set of actions
Now, first lets discuss on how to build custom control of type one.
Only one control with some additional functionality
In the above example we have discussed about the textbox and the validations to be done on it. Where it involves only one contol, with the input validations. Lets build these as a control now.
Steps involved in building the custom control.
- Create a class and name it.
- Inherit the control which you want to overload some properties. (in our example the control is textbox)
- Write the properties, events and method which are needed.
- Build the project
- Your custom control is ready
Building our example
- Create a new project called NumericTextbox.
- Add a new class called NumericControl
- Give reference to System.Windows.Froms
- Inherit the TextBox to the class NumericControl
- Write the following code in the class
Protected Overrides Sub OnKeyPress(ByVal e As _ System.Windows.Forms.KeyPressEventArgs) If Asc(e.KeyChar) = 8 Then ElseIf Not IsNumeric(e.KeyChar) Then e.Handled = True End If RaiseEvent KeyPress(Me, e) End Sub
Public Shadows Event KeyPress(ByVal sender As Object, _ ByVal e As System.windows.forms.KeyPressEventArgs)
Code Explaination
Our objective is to create a control like textbox which will take only numeric inputs. So in the first step I inherited the NumericControl class to Textbox.
Note: On inheriting the class to the textbox you will be gettin the designer window of the class like this.
Click here for image
Then handles the numeric inputs, we need to Override the KeyPress event of our Base class (Textbox). After that I am doing the necessary validation to be done on the textbox through the following code.
If Asc(e.KeyChar) = 8 Then ElseIf Not IsNumeric(e.KeyChar) Then e.Handled = True End If
This is enough for the control but we need to foresee the things which will happen. It man be required to handle the KeyPress event for this control. So I am defining the Key Press event for the control in the following line of code.
Public Shadows Event KeyPress(ByVal sender As Object, _ ByVal e As System.windows.forms.KeyPressEventArgs)
Then, it is our duty to tell when the event should be raised. This event should be raised when the key press takes place in the control. So in the OnKeyPress of the overrides method we need to raise this event using the following statement.
RaiseEvent KeyPress(Me,e)
Please read Handling event in VB.NET for more information on event handling.
Click here to download the sample.
With this I am closing this article, Let see the remaining in my next series. Do mail me your doubts. You can reach me at sadhasivam1981@yahoo.com and sadhasivam1981@hotmail.com. Also in my mobile: 9884461823
Visit my blog: http://answerindotnet.blog.com
|
Responses
|
| Author: Dessi Bravo 05 Jun 2005 | Member Level: Bronze Points : 0 | In this page you have a bad link that I cannot access the sample download, nor the picture of the address book using the sample keypress.
|
|