.NET Tip



The Handled Property...

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 ;

}

Top


Device Drivers Tip



The Device Tree...

The device tree begins from the root device conventionally placed at the bottom. The root device finds the ACPI (Advanced Configuration and Power Interface) device. The ACPI system further finds the ACPI fan, the PCI bus and the ACPI battery system. 

The PCI bus is usually the primary bus in the system . A primary bus is the main bus of the system every other bus like ISA or USB connects to the main bus (PCI) bus in order to get connected with the system. Hence it is the primary bus (PCI) that finds the ISA bridge device, the USB controller and the SCSI adapter. The USB controller further finds the USB hub ( a device where USB devices get connected) . The hub further detects 2 devices the USB joystick and the USB camera.

The ISA bus discovers the ISA sound card the serial port, keyboard and the mouse. The serial port driver in turn finds the external pnp modem attached to it. The SCSI adapter discover the disk (hard disk) attached to it.

Each node of the tree thus formed is known as a device node and can contain driver loaded for it to work.

Top


VC++ Tip



Is there any way by which I can increase/decrease the limit of amount of text an edit control can hold?

Ans: The CEdit and CEditView classes have limits on the amount of text they can contain. But, these limits are operating-system dependent. To adjust the limit we can use CEdit::SetLimitText( ) function. We can also use CEdit::LimitText( ) function to limit the length of the text that the user may enter into an edit control. 

Windows 95 has a limit of 64 KB on edit controls and it cannot be increased. Windows NT has a machine-dependent limit on edit controls. If you need to use a control that is expected to contain near or over 64 KB of data, use CRichEditCtrl or CRichEditView. These classes wrap the rich edit Windows control.

Top


C++ Tip



Can we allocate memory dynamically for references?

Ans: No! The references are initialized at the time of creation. Trying to allocate memory dynamically for a reference creates a problem in initializing it. Thus, the compiler does not allow us to dynamically allocate the memory for references.

Top


C Tip



In the following code p is a pointer to an array of MAXCOL elements. Also, malloc( ) allocates memory for MAXROW such arrays. Would these arrays be stored in adjacent locations? How would you access the elements of these arrays using p?

#define MAXROW 3
#define MAXCOL 4

main( )
{

int i, j ;
int ( *p ) [MAXCOL] ;
p = ( int ( * ) [MAXCOL] ) malloc ( MAXROW * sizeof ( *p ) ) ;

}

Ans: The arrays are stored in adjacent locations. We can confirm this by printing their addresses using the following loop.

int i ;
for ( i = 0 ; i < MAXROW ; i++ )

printf ( "%d", p[i] ) ;

To access the array elements we can use the following set of loops.

for ( i = 0 ; i < MAXROW ; i++ )
{

for ( j = 0 ; j < MAXCOL ; j++ )

printf ( "%d", p[i][j] ) ;

}

Top


Article – C# - Using The Splitter Control



In earlier version of Visual Studio, we used to create splitter windows by calling methods of an MFC class. The main application window actually used to get split in panes. Each pane represented a separate view window. .NET’s concept of splitting windows is different. .NET provides a Splitter control, which is used to resize docked controls at run time. It does not actually splits a window. The control to be resized must be just before the splitter control in z-order.

This article shows how to use the Splitter control in a WinForm application. We intend to display a form window as shown in the following snap shot.

The blue horizontal bar is a label control. We have changed its BackColor, ForeColor and Font properties. The control placed at the left hand side is the ListBox control. The control placed on the right hand side is the ListView control. The program is to list the currently running processes in the list box. If a process is selected its information is displayed in the list view control. Here, our aim is to show how to use the Splitter control. Our stress is not on browsing and retrieving information of processes.
 
When we place the Splitter control on a container, it divides the entire container in two parts—left and right (or top and bottom if placed horizontally). But, we don’t want to show the vertical bar of the Splitter control on the label. We want that the effect of splitter should be felt only on the remaining part of the form. For this, we have placed a Panel on the form so as to cover the remaining part of the form. This is shown in the following snap shot.

Now, place the ListBox and change its Dock property to Left. Name the control as list. Drag the Splitter control on the form. It will get docked to the left side indicating that it has to resize the control placed on the left. Run the program and place the cursor on the splitter bar. When the cursor changes, drag it to left or right. You would see the list box resizing. We want to place a ListView control as well as button on the right side of the splitter. If user moves the splitter, both the controls should be relocated properly. For this, we have again placed a Panel covering the right side of the form and placed the ListView and Button controls on it. Name the Panel control as panel, ListView control as listview and Button control as kill. Clicking this button would kill the process selected from the list box. Again note that our aim is to show you how to relocate the multiple controls if Splitter control is moved, not to kill the process. Lastly, add the Process component from the ‘Components’ tab on ToolBox and name it process. 
The designing part is over, lets move on to the coding part. Firstly, add the Load event handler. The handler is shown below.

private void Form1_Load ( object sender, System.EventArgs e )
{

Process[ ] parr = Process.GetProcesses( ) ;
foreach ( Process p in parr )

list.Items.Add ( p.ProcessName ) ;

}

Here, we have browsed the processes and listed them in the list box

Now add the Click event handler for the button. In this handler we have checked whether any process is selected. If it is, then we have closed the process using the kill( ) method.

private void kill_Click ( object sender, System.EventArgs e )
{

if ( list.SelectedItem == null )

return ;


Process[ ] p = Process.GetProcessesByName ( 
list.SelectedItem.ToString( ) ) ;
p [ 0 ].Kill( ) ;
list.Items.Remove ( list.SelectedItem ) ;
list.SelectedIndex = -1 ;
listview.Items.Clear( ) ;

}

After the process is closed, we have deleted the process name from the list box, set the selected index to –1 and cleared the list view control. When the user selects a process, its information is displayed in the ListView control. The code to fill the ListView control is written in the SelectedIndexChanged event handler. 

private void list_SelectedIndexChanged ( object sender, EventArgs e )
{

if ( list.SelectedIndex != -1 )
{

listview.Items.Clear( ) ;
Process[ ] p = Process.GetProcessesByName 
( list.SelectedItem.ToString( ) ) ;
listview.Items.Add ( p [ 0 ].BasePriority.ToString( ) ) ;

listview.Items [ 0 ].SubItems.Add ( p [ 0 ].Id.ToString( ) ) ;
listview.Items [ 0 ].SubItems.Add ( p [ 0 ].StartTime.ToShortTimeString( ) ) ;
listview.Items [ 0 ].SubItems.Add ( ( p [ 0 ].WorkingSet / 1024 ).ToString( ) ) ;

}

}

Here, firstly we have checked whether the handler has got called because of the change in selected index in the kill_Click( ) event handler. Then we have used various properties of the Process class to obtain information of the process. 

Moving the splitter would resize the list control, but it would not change the width of the Panel control hosting the ListView and button. We have to do it ourselves. 

Add a handler for the SplitterMoved event for Splitter control and adjust the size and left coordinate of the Panel control as given below.

private void splitter_SplitterMoved ( object sender, SplitterEventArgs e )
{

panel.Width = Width - e.X ;
panel.Left = e.X ;

}

Run the program. The form after moving the splitter to the right would look as given below.

Top


Joke



After shopping for most of the day, a couple returns to find their car has been stolen. They go to the police station to make a full report. Then, a detective drives them back to the parking lot to see if any evidence can be found at the scene of the crime. To their amazement, the car has been returned. 

There is an envelope on the windshield with a note of apology and two tickets to a music concert. The note reads, 'I apologize for taking your car, but my wife was having a baby and I had to hot-wire your ignition to rush her to the hospital. Please forgive the inconvenience. Here are two tickets for tonight's concert of Garth Brooks, the country-and-western music star.' 

Their faith in humanity restored, the couple attend the concert and return home late. They find their house has been robbed. Valuable goods have been taken from throughout the house, from basement to attic. And, there is a note on the door reading, 'Well, you still have your car. I have to put my newly born kid through college somehow, don't I?'

Top


Different Strokes



Top