.NET Tip



How to send an E-mail through a Web application?

Ans: .NET has made this difficult task very easy by providing the System. Web.Mail namespace. The namespace exposes classes like Mail Message to prepare the mail, Smtp Mail to send the mail and Mail Attachment to attach a file (s) to mail. 

Top


Device Drivers Tip



Components Of The I/O System...

The PnP (Plug And Play) manager works closely with the I/O manager and a type of device driver called a bus driver to guide the allocation of hardware resources as well as to detect and respond to the arrival and removal of hardware devices. The PnP manager and bus drivers are responsible for loading a device's driver when the device is detected. When a device is added to a system that doesn't have an appropriate device driver, the executive Plug and Play component calls on the device installation services of a user-mode PnP manager.

Top


VC++ Tip



How do I write code to select entire row and make selection appear even when control doesn't have focus?

Ans:
To be able to select entire row use CListCtrl :: SetExtendedStyle to set the style LVS_EX_FULLROWSELECT. Similarly, use style LVS_SHOWSELALWAYS when creating your control.

Top


C++ Tip



Can we get the typeid of void pointers?

Ans: No! At runtime, type identification doesn’t work with void pointers, because void * truly means no type information at all.

Top


C Tip



What would be the output of the following program, if the program (myprog) is run from the command line as :

myprog one two three 

main( int argc, char*argv[] )

{

printf ( "%c", ++**++argv ) ;

}

Ans: The output of this program would be p. Here, argv is an array of char pointers. ++argv, results into argv pointing to the position that holds address of second string of command line arguments (i.e. 'one'). *argv, gives the base address of string 'one'. Further *argv, gives value at the base address, which is nothing but the ASCII value of 'o'.  Incrementing this ASCII value results into ASCII value of 'p'. Hence the output p.

Top


Article – C# - Handling Keyboard & Mouse Messages



WinForms process keyboard messages by overriding virtual methods inherited from the Form class. The following table lists the virtual methods corresponding to the keyboard events.

 Method  Called when  Argument Type
 OnKeyDown  A key is pressed  KeyEventArgs
 OnKeyPress  A character is typed  KeyPressEventArgs
 OnKeyUp  A Key is released  KeyEventArgs

The OnKeyDown( ) and OnKeyUp( ) methods correspond to the WM_KEYDOWN and WM_KEYUP message handlers in Windows. The KeyEventArgs class has a property KeyCode that identifies the key pressed. The OnKeyPress( ) method is equivalent to the WM_CHAR message handler and is invoked when a character key is pressed. The KeyPressEventArgs class has a property KeyChar that gives the outcome after taking into account the state of other keys. For example, OnKeyDown( ) will tell us whether key ‘A’ is pressed or not, whereas, OnKeyPress( ) will tell whether ‘A’ is in uppercase or lowercase.

The KeyEventArgs class has a property called Modifiers that contains the status of the modifier keys such as ‘Ctrl’, ‘Alt’ and ‘Shift’. So, to check whether Ctrl + A is pressed, we can write the if condition as follows.

if ( ( e.KeyCode == Keys.A ) && ( e.Modifiers == Keys.Control ) )

        MessageBox.Show ( "Ctrl + A pressed" ) ;

Keys is an enum that specifies the key codes and modifiers.

The Handled property can restrict the controls placed on the form from handling the keyboard events and allow them to be handled only at the form level. For this, form’s Handled property should be set to true. Setting the Handled property to true in the OnKeyPress( ) method indicates that the KeyPress event is already handled. The Handled property is also useful in a situation when we want that only certain characters should get entered in the textbox, others should not. For example, we want that a textbox should take only alphabets and should ignore digits and special symbols then the Handled property should be set to false if alphabets are entered and to true otherwise. This is to be done in the KeyPress event handler added for the textbox as shown below.

private void textBox1_KeyPress ( object sender, KeyPressEventArgs e )

{

        if ( ( e.KeyChar >= 97 && e.KeyChar <= 122 ) ||

                        ( e.KeyChar >= 65 && e.KeyChar <= 91 ) )

                e.Handled = false ;

        else

                e.Handled = true ;

}

Unlike the Handled property, if form’s KeyPreview property is set to true it receives all the three keyboard events plus the control having focus also receives the events. For example, if the textbox has the key input focus and form’s KeyPreview property is on, firstly the form will receive the events and then textbox will receive the key that was pressed. We can use the KeyPreview property to filter the keys before a control receives it. For example, we want to design a registration form having many text boxes. If user enters a value and presses the Enter key, next textbox should get the focus. For this, instead of adding keyboard events to each textbox and check for the Enter key press, we can add the KeyDown event handler to the form and check whether the Enter key is pressed. The code to shift the focus is given below.

private void Form1_KeyDown ( object sender, KeyEventArgs e )

{

        if ( e.KeyCode == Keys.Return )

        {

                Control c = GetNextControl ( ActiveControl, true ) ;

                c.Focus( ) ;

        }

}

The ActiveControl property returns the reference of the control that is currently selected. The GetNextControl( ) method returns the control next to the specified control in tab order. The focus is set on the next control using the Focus( ) method.

Mouse events raised on mouse click resemble the Windows mouse messages. The MouseDown, MouseMove and MouseUp events occur when the mouse is clicked, moved and released respectively. The type of parameters the event handlers receive is MouseEventArgs.

We would now demonstrate using mouse messages in a ‘TimeZone’ application. In this WinForm based application we would display a World map on the form. As the user moves cursor on the map, names of places would appear in a small pop-up window.

As a first step to create this application, we would display two lines intersecting each other at the cursor position as shown in the following figure. The point of intersection changes as the cursor position changes.

 

 

Add x and y as private data members of the Form class. Add the MouseMove event handler and add the code in it as shown below.

private void Form1_MouseMove ( object sender, MouseEventArgs e )

{

        Graphics g = CreateGraphics( ) ;

        int w = ClientSize.Width ;

        int h = ClientSize.Height ;

 

        // Erase Line

 

        Pen p = new Pen ( BackColor ) ;

        g.DrawLine ( p, x, 0, x, h ) ;

        g.DrawLine ( p, 0, y, w, y ) ;

 

        x = e.X ;

        y = e.Y ;

 

        // Draw Line

        p = new Pen ( Brushes.Red ) ;

        g.DrawLine ( p, x, 0, x, h ) ;

        g.DrawLine ( p, 0, y, w, y ) ;

}

Here, we have drawn the lines and erased the previous lines. We would update this handler as we proceed in the next article.

Top


Joke



A local florist just went out of business, but it was his own fault. He kept getting his orders mixed up. One woman received flowers sent by her husband, who was at a business meeting in Florida. She was perplexed by the message on her card: "Our deepest sympathy."

But she was not nearly as surprised as the woman whose husband had just passed away. Her card read, "Hotter here than I expected. Too bad you didn't come too."

Top


Different Strokes



Top