C Tip



Can I change the size of a file?

Ans: Yes! We can. Sometimes, while working with files, we may require to allocate a large amount of disk space for a file or we may even require to reduce the file size. We can use chsize( ) function, This function takes two parameters-handle to the file and new size of the file. The function returns 0 if successful otherwise it returns –1. If this function is used to increase the size of the file, then it fills the new file space with NULL characters, whereas if we reduce the file size then all data beyond new end-of file indicator is lost.

Top


C++ Tip



What is the purpose of seekp( ) and seekg( ) function?

Ans: The seekp( ) function positions the put pointer at the specific location in the file. The seekp( ) function is overloaded to take either one or two parameters. If we use one-argument seekp( ), we can mention the position in bytes relative to the beginning of the file. In the two-argument version we can specify the position in bytes relative to the end of file, beginning of the file and current position. For this, we can use end, beg and cur flags respectively.

The seekg( ) function works similar to seekp( ) function but the major differences is that the first works on get pointer while the second works on put pointer.

Top


VC++ Tip



When we create a modeless dialog we allocate memory dynamically for the object of the desired dialog class. How do I write code to display only one instance of the modeless dialog and to de-allocate memory?

Ans: The memory allocated dynamically for the dialog class would not get de-allocated unless we call delete operator. Suppose a modeless dialog of class mydialog has to be displayed by calling a function of CChildView class, then follow the steps given below:

1. Add a pointer to an object of the mydialog class as a private data member to CChildView class.

2. Initialize this data member to NULL in the constructor of CChildView class.

3. Add code shown below to the function that displays modeless dialog.

void CChildView :: OnModelessdlg( )
{

if ( d== NULL )
{

d = new mydialog ( this ) ; // where mydialog is a class 
d -> Create ( IDD_DIALOG1 ) ; // where IDD_DIALOG1 is an ID of the dialog created in resource editor
d -> ShowWindow ( 1 ) ;

}
else

d -> SetFocus( ) ; // to activate the dialog which is already created

}

Here, first we have ascertained whether the pointer d is NULL. If it is, then we have allocated memory dynamically, passed the address of parent window (i.e. CChildView class) to the constructor of mydialog class. Then we have created dialog and displayed it. If the pointer d is not NULL then we have activated the dialog by calling SetFocus( ) so that the same dialog would get activated.

4. Add a pointer to CWnd class as a data member to dialog class and collect the parent address received by constructor in it.

5. Add handlers for 'OK' and 'Cancel' button to the mydialog class. A modeless dialog never gets dismissed by calling EndDialog( ) hence, delete the base class handlers called in OnOK( ) or OnCancel( ), and call DestroyWindwo( ) function in each of them.

6. Override PostNcDestroy( ) (a virtual function) in the mydialog class and call a user defined function say delptr( ) of CChildView class as shown below:

void mydialog::PostNcDestroy( ) 
{

( CChildView * ) p ) -> delptr( ) ;
CDialog::PostNcDestroy( );

}

7. Add a function delptr( ) to CChildView class and enter following code to it.

voif CChildView :: delptr( )
{

delete d ;
d = NULL ;

}

Top


VB.NET Tip



How do I write code that pops up a context sensitive menu on right clicking onto a combo box?

Ans: Follow the steps given below. 

1. Create a Windows Application and place a combo box named cb on the form. Add a context menu on the form named 
    combomenu
and add an item cadd in the menu. 

2. Set the text property of the menu item cadd as "ADD".

3. Set the ContextMenu property of the combo box as combomenu

4. Add the Click event handler for the cadd menu item and enter code given below in it.

Private Sub cadd_Click ( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles cadd.Click

If cb.FindStringExact(cb.Text) = -1 Then

cb.Items.Add(cb.Text)

End If

End Sub

Here, we have not only added a context sensitive menu but also given a provision  that on clicking the menu item 'ADD", the text typed in the combo box should get added in the dropdown list. If the text is already present in the dropdown list it would not get added to the list.

 Top


Device Driver Tip



Hardware environment

Device Registers are locations within a hardware device. These locations can be read only, write only or read / write. Moreover a device can have more than one device register. In fact real world devices usually have many device registers. The concept of device registers is analogous to registers within the microprocessor (AX,BX CX.etc.). The device registers of hardware devices are used for many different purpose like transferring data, obtaining status information of the device and controlling the behavior of the device.

Data register – The registers are used for data transfer between the device and the CPU. Output data registers are usually written to by the device driver. Input device registers are read by the device driver

Status register – The status device registers are read only and are read by the device driver to obtain the status of the device. For example a device register in the printer would indicate the status of the printer (online / offline, paper empty, busy etc..)

Control register – The control device register are usually write only and are written to by the device driver software to control the functionality and behavior of the device.
For example the control register of a device like printer can be used to start, abort data transfer or configure the device in some way.

Top


Article: C#- Creating Web Application - I



ASP.NET is a technology used to create web applications and dynamic web sites. ASP.NET is compatible and is integrated with the .NET environment. It is very much different from ASP. First let’s see how it differs.

ASP (Active Server Pages) is a technology that helps us create web pages dynamically. An ASP page is an HTML file with server-side script written in VBScript or JScript. When a client requests an ASP page, the web server delivers the static HTML portions of the page as it is, and processes the server side script, which further generates HTML. The problems faced with ASP are:

(a) The scripting is interpreted, leading to slow rendering of pages.

(b) The entire HTML portion (content) and scripting portion (code) is jumbled up in one ‘.asp’ file.

(c) We have to write code in ASP for everything including simple jobs like validating form fields.

ASP.NET solves all these problems faced with ASP. First of all ASP.NET pages are compiled and not interpreted. Secondly ASP.NET pages are structured, meaning server-side scripts and plain HTML are not jumbled up; scripts and HTML can be separated. ASP.NET provides a feature called code-behind. This feature allows us to separate the server-side code of a page, and place it into another file. A ‘.aspx’ file contains the HTML portion of the application and a ‘.cs’ file contains the code. This allows one team of team members to concentrate on HTML design, as others work on the code.

Web Forms

.NET Web Applications or Web Forms like WinForms brings RAD to development of web applications. Web forms are created by dropping controls onto a form and then double-clicking those controls and writing event handlers for them in code behind pages. The controls to be used in Web Forms are present in the .NET Framework class library. Some controls are mere wrappers around simple HTML tags, but others represent complex UI objects that generate HTML. Let us first build a Web Form and then understand the working of an ASP.NET Web Form application.

In this article we would see how to create a registration form that would allow students to register for an online course. 
Let us now set to build the application. Create an ASP.NET application names ‘Registration’ by selecting ASP.NET Web Application from the template list. On doing so a virtual directory named Registration would get created in the IIS and its local location will be ‘C:\Inetpub\wwwroot\Registration’. As soon as we create a project the following files would get generated in the Registration directory:

 - A Global.asax file containing application-level program directives, handlers for application and session-level events, and
   declarations of objects that are globally accessible to all parts of the application.
 - A Web.config file containing XML that stores the application configuration information.
 - A WebForm1.aspx file containing HTML for rendering Web Forms.
 - A WebForm1.aspx.cs file containing the code behind page written in C#.NET.
 - An AssemblyInfo.cs file containing standard code for assembly description.
 - A WebForm1.disco file describing any web services in the project.

We will only be concentrating on the ‘WebForm1.aspx’ file (content) and the ‘WebForm1.aspx.cs’ (code) file for our application.

On creating the application an empty web form would get displayed in the design view of the ‘WebForm1.aspx’ file. We intend to add a table to this form. To add a table we need to select Table menu and then ‘Insert | Table’ option and we would be presented with the ‘Insert Table’ Dialog box. Select the rows (11) and columns (3) that we wish to add to the table. A grid would get displayed on the form. Insert the controls—labels, text boxes, validation controls and button to the form as shown in the following figure.

The details of the controls are given in the following table.

 Control   Name
 TextBox  uid
 TextBox  pass
 TextBox  cpass
 TestBox  sname
 TextBox  address
 TextBox  state
 TextBox  zip
 TextBox  country
 TextBox  mail
 RadioButtonList  course
 Button  submit

The controls appearing in red color are called validation controls. Whenever we enter something on the form and submit it to the server it is called a postback. Validation controls provide a method of validating user input without writing any code. Whenever postback is initiated (i.e. we click the ‘Submit Form’ button) each control checks the control it is validating and changes its IsValid property accordingly. If the property is set to false it means that the user input is wrong. In such a case an error message is flashed. If the property is set to true the postback occurs. Some of the validation controls that we have used in this program are given in the following table.

 Control  Name
 RequiredFieldValidator  reqid 
 CompareValidator  compass
 RegularExpressionValidator  checkmail

The pass textbox would accept a password. Generally a password is something that should not be visible hence we need to change the TextMode property of this textbox to Password. Now whatever the student types in this textbox is substituted by *s. 

We must now change the properties of validation controls. How to do this would be discussed next week.

To be continued…      

Top


Joke



In a huff, a woman rushed up to a zoo attendant. “You should see what’s going on in the monkey cage!” she exclaimed. “Four chimps are sitting at a table playing cards!” “So what?” answered the attendant, shrugging his shoulders. “They’re only playing for peanuts.”

Top


Different Strokes





An Illusion!

Top