:: Home

  login:         
  passwords:  

Winforms Interview Questions

Windows Forms Deployment
Windows Forms Controls
Windows Forms Data Binding
Windows Forms Datagrid
Windows Forms Docking
Windows Forms Keyboard Handling
Windows Forms Layout
Windows Forms Licensing
Windows Forms Menus
Windows Forms Mouse Handling
Windows Forms from MFC
Windows Forms from VB6
Windows Forms Patterns
Windows Forms Printing
Windows Forms Resources
Windows Form Scrolling
Windows Forms Tips
Windows Forms Common Dialogs
Windows Forms Listbox
Windows Forms ComboBox
Windows Forms Rich TextBox
Windows Forms ListView
Windows Forms TreeView
Windows Forms Button
Windows Forms TabControl
Windows Forms TextBox
Windows Forms MDI
Windows Forms Cursors
Windows Forms WebBrowser
Windows Forms PictureBox
Windows Forms Form
Windows Forms MDI
Windows Forms In IE
GDI+Bitmaps&Images
GDI+Font
GDI+Color
GDI+Brushes
GDI Drawing Tips
GDI+ from GDI
GDI Paths Regions
GDI+Pens
Interioerability WIn32
Tools Metadata Viewers
Design Time Serialization
Design Time Custom Designers
Design Time Tips
Design Time Type Editors
Vs.Net Tips
Vs.Net Debugging
Vs.Net Macros
Framework Tips General
Framework Tips Events
Framework Tips General IO
Framework Tips Strings
Framework Tips Threading
Tool Resource Editor
Design Time UI
Framework Tips CGI
Framework Tips XML

WPF Interview Qs

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

<

Windows Forms ListView

The General Winforms Interview Questions consists the most frequently asked questions in Winforms. This list of 100+ questions guage your familiarity with the Winforms platform. The q&a have been collected over a period of time from various blogs, forums and other similar Winforms sites

22. Windows Forms ListView

    22.1 How do I add SubItems to a ListView control?
    22.2 When I call ListViewItem.Selected = true;, why doesn't the item get selected?
    22.3 How do I unselect the selected items in a ListView programatically?
    22.4 How can I tell which column (subitem) has been clicked on in my listview?
    22.5 How do I programmatically select an item in my listview?
    22.6 I am trying to programmatically change the ForeColor and BackColor properties of subitems in a listview. Why doesn't this work?
    22.7 How do I implement custom column sorting in a listview?

22.1 How do I add SubItems to a ListView control?

Try code such as:

ListViewItem item = new ListViewItem("NewItem");
item.SubItems.AddRange(new string[]{"SubItem1", "SubItem2")};

listView1.Items.Add(item);
listView1.Items.Add(new ListViewItem(new string[]{"item1", "item2", "item3", "item4"});
listView1.View = View.Details;

22.2 When I call ListViewItem.Selected = true;, why doesn't the item get selected?

Make sure the ListView has focus when you set the Selected property. For example, if this code is in the click handler for a button, this button will have the focus, and the selection in the list will fail. Just set the focus back to the list before you set the Selected property.


22.3 How do I unselect the selected items in a ListView programatically?

Simply do the following:

[C#]
this.listView1.SelectedItems.Clear();

21.4 How can I tell which column (subitem) has been clicked on in my listview?

Here is a solution offered by Bharat Patel of Microsoft in the microsoft.public.dotnet.languages.csharp newgroup. The attached solution contains both C# and VB.NET projects. The solution use Interop with the LVM_GETSUBITEMRECT window's message to loop through each subitem's bounds rectangle until it finds the one that contains the specified mouse click point.

22.5 How do I programmatically select an item in my listview?

If you visit the selection a character at the time, you can get the current FontStyle and modify it directly to add or remove a style like Bold or Italic. Doing it a character at a time will avoid losing the other styles that are set. The problem with doing it a whole selection at a time is that the FontStyle of the entire selection is the common styles set among all characters in the selection. So, if one char is bold and one is not, then bold is not set when you retrieve the fontstyle for the entire selection. Doing things a character at the time, avoids this problem and allows things to work OK.

To select the i-th item, you can use code such as
//Make sure the listview has focus
listView1.Focus();
listView1.Items[i].Selected = true;

22.6 I am trying to programmatically change the ForeColor and BackColor properties of subitems in a listview. Why doesn't this work?

Make sure the item's UseItemStyleForSubItems property is set to false.

ListViewItem item1 = new ListViewItem("item1",0);
//this line makes things work
item1.UseItemStyleForSubItems = false;
//set fore & back color of next sub item
item1.SubItems.Add("1", Color.Black, Color.LightGreen, Font);

item1.SubItems.Add("44");
item1.SubItems.Add("3");
//As long as UseItemStyleForSubItems is false, you can later set the colors with code such as
item1.SubItems[2].BackColor = Color.Pink;

22.7 How do I implement custom column sorting in a listview?

You create a class the implements the IComparer interface. This interface has a single method that accepts two objects, and returns positive integers if the first object is larger than the second, negative integers if the first object is less than the second object, and returns zero if they are the same. Once you have this class, then you handle the listview's ColumnClick event to set the property ListViewItemSorter to point to an instance of this class.

public class SorterClass : IComparer
{
private int _colNum = 0;
public SorterClass(int colNum)
{
_colNum = colNum;
}
//this routine should return -1 if xy and 0 if x==y.
// for our sample we'll just use string comparison
public int Compare(object x, object y)
{
System.Windows.Forms.ListViewItem item1 = (System.Windows.Forms.ListViewItem) x;
System.Windows.Forms.ListViewItem item2 = (System.Windows.Forms.ListViewItem) y;
if(int.Parse(item1.SubItems[_colNum].Text) < int.Parse(item2.SubItems[_colNum].Text)
) return -1;
else if(int.Parse(item1.SubItems[_colNum].Text) > int.Parse(item2.SubItems[_colNum].Text))
return 1;
else
return 0;
}
}
//usage...
private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{

//don't sort col 0
if(e.Column > 0)
{
SorterClass sc = new SorterClass(e.Column);
listView1.ListViewItemSorter = sc;
}
}
Here is a VB snippet. Depending on your default namespace settings, you may not need explicit reference to the namespace in the GetManifestResourceStream argument.
Dim stream As Stream = Me.GetType().Assembly.GetManifestResourceStream("MyNameSpace.RTFText.rtf")
If Not (stream Is Nothing) Then
Dim sr As New StreamReader(stream)
richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText)
sr.Close()
End If

21.8 How can I print my rich text?

There is no direct support for printing rich text in the .NET Framework. To print it, you can use interop with the SendMessage API and these three messages to do your printing.
EM_SETTARGETDEVICE : specify the target device
EM_FORMATRANGE : format part of a rich edit control's contents for a specific device
EM_DISPLAYBAND : send the output to the device


21.9 How can I add a hyperlink to a RichTextBox control?

To add a hyperlink to the RichTextBox, so that it opens up the link you click on, ensure that DetectUrls property is set to True and call:

[C#]
private void richTextBox1_LinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.LinkText);
}
[VB.NET]
Private Sub richTextBox1_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs)
System.Diagnostics.Process.Start(e.LinkText)
End Sub

21.10 How can I change the FontStyle of a selection without losing the styles that are present?

If you visit the selection a character at the time, you can get the current FontStyle and modify it directly to add or remove a style like Bold or Italic. Doing it a character at a time will avoid losing the other styles that are set. The problem with doing it a whole selection at a time is that the FontStyle of the entire selection is the common styles set among all characters in the selection. So, if one char is bold and one is not, then bold is not set when you retrieve the fontstyle for the entire selection. Doing things a character at the time, avoids this problem and allows things to work OK.

Copyright 2007, Megasolutions Ltd
     
Copyright 2007, Megasolutions Ltd