Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

item text in the ListView control overlaps other items in smallicon

Status
Not open for further replies.

progpradip

Programmer
Nov 26, 2008
23
In my application ListView is inside panel, item text in the ListView control overlaps other items in small icon view. The items are drawn as if aligned to a grid and this causes items to be drawn in overlapped manner which obviosuly looks horrrible. I have tried to change allignment with no success. There is no problem regarding List and Details mode.

Note that when I press 'F5' in smallicon view mode...there is huge problem. How can I get rid of this?

What might be the problem?

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Others.ListViewSmallIcon
{
/// <summary>
/// Summary description for ListViewCountry.
/// </summary>
public class ListViewCountry : System.Windows.Forms.Form
{
private ListViewEx listView1;
private System.Windows.Forms.ColumnHeader Country;
private System.Windows.Forms.ColumnHeader Capital;
private System.Windows.Forms.ColumnHeader City_1;
private System.Windows.Forms.ColumnHeader City_2;
private System.Windows.Forms.Button REPORT;
private System.Windows.Forms.Button LIST;
private System.Windows.Forms.Button SMALLICON;
private System.Windows.Forms.Button LARGEICON;
ImageList ig = new ImageList();
private System.Windows.Forms.Button SELECTED;
private System.Windows.Forms.Button CHECKED;
private System.Windows.Forms.Panel panel1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public ListViewCountry()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//Application.EnableVisualStyles();
//Application.

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new Others.ListViewSmallIcon.ListViewEx();
this.Country = new System.Windows.Forms.ColumnHeader();
this.Capital = new System.Windows.Forms.ColumnHeader();
this.City_1 = new System.Windows.Forms.ColumnHeader();
this.City_2 = new System.Windows.Forms.ColumnHeader();
this.REPORT = new System.Windows.Forms.Button();
this.LIST = new System.Windows.Forms.Button();
this.SMALLICON = new System.Windows.Forms.Button();
this.LARGEICON = new System.Windows.Forms.Button();
this.SELECTED = new System.Windows.Forms.Button();
this.CHECKED = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// listView1
//
this.listView1.AllowColumnReorder = true;
this.listView1.CheckBoxes = true;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.Country,
this.Capital,
this.City_1,
this.City_2});
this.listView1.Dock = System.Windows.Forms.DockStyle.Top;
this.listView1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.listView1.ForeColor = System.Drawing.SystemColors.HotTrack;
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.ShowControlOnlyWhenSelected = false;
this.listView1.Size = new System.Drawing.Size(464, 152);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.WordWarpMaxWidth = 15;
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);
this.listView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listView1_KeyDown);
//
// Country
//
this.Country.Text = "Country";
this.Country.Width = 100;
//
// Capital
//
this.Capital.Text = "Capital";
this.Capital.Width = 150;
//
// City_1
//
this.City_1.Text = "City_1";
this.City_1.Width = 100;
//
// City_2
//
this.City_2.Text = "City_2";
this.City_2.Width = 100;
//
// REPORT
//
this.REPORT.Dock = System.Windows.Forms.DockStyle.Left;
this.REPORT.Location = new System.Drawing.Point(75, 0);
this.REPORT.Name = "REPORT";
this.REPORT.Size = new System.Drawing.Size(75, 24);
this.REPORT.TabIndex = 1;
this.REPORT.Text = "Report";
this.REPORT.Click += new System.EventHandler(this.REPORT_Click);
//
// LIST
//
this.LIST.Dock = System.Windows.Forms.DockStyle.Left;
this.LIST.Location = new System.Drawing.Point(300, 0);
this.LIST.Name = "LIST";
this.LIST.Size = new System.Drawing.Size(75, 24);
this.LIST.TabIndex = 1;
this.LIST.Text = "List";
this.LIST.Click += new System.EventHandler(this.LIST_Click);
//
// SMALLICON
//
this.SMALLICON.Dock = System.Windows.Forms.DockStyle.Left;
this.SMALLICON.Location = new System.Drawing.Point(150, 0);
this.SMALLICON.Name = "SMALLICON";
this.SMALLICON.Size = new System.Drawing.Size(75, 24);
this.SMALLICON.TabIndex = 1;
this.SMALLICON.Text = "Small Icon";
this.SMALLICON.Click += new System.EventHandler(this.SMALLICON_Click);
//
// LARGEICON
//
this.LARGEICON.Dock = System.Windows.Forms.DockStyle.Left;
this.LARGEICON.Location = new System.Drawing.Point(375, 0);
this.LARGEICON.Name = "LARGEICON";
this.LARGEICON.Size = new System.Drawing.Size(75, 24);
this.LARGEICON.TabIndex = 1;
this.LARGEICON.Text = "Large Icon";
this.LARGEICON.Click += new System.EventHandler(this.LARGEICON_Click);
//
// SELECTED
//
this.SELECTED.Dock = System.Windows.Forms.DockStyle.Left;
this.SELECTED.Location = new System.Drawing.Point(0, 0);
this.SELECTED.Name = "SELECTED";
this.SELECTED.Size = new System.Drawing.Size(75, 24);
this.SELECTED.TabIndex = 2;
this.SELECTED.Text = "Selected";
this.SELECTED.Click += new System.EventHandler(this.SELECTED_Click);
//
// CHECKED
//
this.CHECKED.Dock = System.Windows.Forms.DockStyle.Left;
this.CHECKED.Location = new System.Drawing.Point(225, 0);
this.CHECKED.Name = "CHECKED";
this.CHECKED.Size = new System.Drawing.Size(75, 24);
this.CHECKED.TabIndex = 3;
this.CHECKED.Text = "Checked";
this.CHECKED.Click += new System.EventHandler(this.CHECKED_Click);
//
// panel1
//
this.panel1.Controls.Add(this.LARGEICON);
this.panel1.Controls.Add(this.LIST);
this.panel1.Controls.Add(this.CHECKED);
this.panel1.Controls.Add(this.SMALLICON);
this.panel1.Controls.Add(this.REPORT);
this.panel1.Controls.Add(this.SELECTED);
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 157);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(464, 24);
this.panel1.TabIndex = 4;
//
// ListViewCountry
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 181);
this.Controls.Add(this.panel1);
this.Controls.Add(this.listView1);
this.Name = "ListViewCountry";
this.Text = "ListView";
this.Load += new System.EventHandler(this.ListViewCountry_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

private void ListViewCountry_Load(object sender, System.EventArgs e)
{
// Create an image list of icons that you want to display for each item
ig.Images.Add(new Bitmap("C:\\FS\\Long File name of file is this (22).jpg"));
ig.Images.Add(new Bitmap("C:\\FS\\Long File name of file is this (22).jpg"));
ig.Images.Add(new Bitmap("C:\\FS\\Long File name of file is this (22).jpg"));
ig.Images.Add(new Bitmap("C:\\FS\\Long File name of file is this (22).jpg"));
ig.Images.Add(new Bitmap("C:\\FS\\Long File name of file is this (22).jpg"));

// Set the Imagelist for SmallIcons
listView1.SmallImageList = ig;
// Set the ImageList for LargeIcons
listView1.LargeImageList = ig;

// Align the columns 1,2,3 and the column data as Center
listView1.Columns[1].TextAlign = HorizontalAlignment.Center;
listView1.Columns[2].TextAlign = HorizontalAlignment.Center;
listView1.Columns[3].TextAlign = HorizontalAlignment.Center;

// Create a ListViewItem object for evey item that you wish to add the ListView.
string[] lv = new String[4];
lv[0] = "USA";
lv[1] = "Washington DC";
lv[2] = "New York";
lv[3] = "Los Angeles";
listView1.Items.Add(new ListViewItem(lv, 0));

lv[0] = "Italy";
lv[1] = "Rome";
lv[2] = "Venice";
lv[3] = "Milan";
listView1.Items.Add(new ListViewItem(lv, 1));

lv[0] = "Canada";
lv[1] = "Ottawa";
lv[2] = "Montreal";
lv[3] = "Quebec";
listView1.Items.Add(new ListViewItem(lv, 2));

lv[0] = "Switzerland";
lv[1] = "Geneva";
lv[2] = "Zurich";
lv[3] = "Lucerne";
listView1.Items.Add(new ListViewItem(lv, 3));

lv[0] = "UnitedKingdom";
lv[1] = "London";
lv[2] = "ChesterField";
lv[3] = "Wembly";
listView1.Items.Add(new ListViewItem(lv, 4));

lv[0] = "Republic of Nepal";
lv[1] = "Kathmandu";
lv[2] = "Pokhara";
lv[3] = "Khadichaur";
listView1.Items.Add(new ListViewItem(lv, 1));
lv = new String[4];
lv[0] = "USA";
lv[1] = "Washington DC";
lv[2] = "New York";
lv[3] = "Los Angeles";
listView1.Items.Add(new ListViewItem(lv, 0));

lv[0] = "Italy";
lv[1] = "Rome";
lv[2] = "Venice";
lv[3] = "Milan";
listView1.Items.Add(new ListViewItem(lv, 1));

lv[0] = "Canada";
lv[1] = "Ottawa";
lv[2] = "Montreal";
lv[3] = "Quebec";
listView1.Items.Add(new ListViewItem(lv, 2));

lv[0] = "Switzerland";
lv[1] = "Geneva";
lv[2] = "Zurich";
lv[3] = "Lucerne";
listView1.Items.Add(new ListViewItem(lv, 3));

lv[0] = "UnitedKingdom";
lv[1] = "London";
lv[2] = "ChesterField";
lv[3] = "Wembly";
listView1.Items.Add(new ListViewItem(lv, 4));

lv[0] = "Republic of Nepal";
lv[1] = "Kathmandu";
lv[2] = "Pokhara";
lv[3] = "Khadichaur";
listView1.Items.Add(new ListViewItem(lv, 1));
lv = new String[4];
lv[0] = "USA";
lv[1] = "Washington DC";
lv[2] = "New York";
lv[3] = "Los Angeles";
listView1.Items.Add(new ListViewItem(lv, 0));

lv[0] = "Italy";
lv[1] = "Rome";
lv[2] = "Venice";
lv[3] = "Milan";
listView1.Items.Add(new ListViewItem(lv, 1));

lv[0] = "Canada";
lv[1] = "Ottawa";
lv[2] = "Montreal";
lv[3] = "Quebec";
listView1.Items.Add(new ListViewItem(lv, 2));

lv[0] = "Switzerland";
lv[1] = "Geneva";
lv[2] = "Zurich";
lv[3] = "Lucerne";
listView1.Items.Add(new ListViewItem(lv, 3));

lv[0] = "UnitedKingdom";
lv[1] = "London";
lv[2] = "ChesterField";
lv[3] = "Wembly";
listView1.Items.Add(new ListViewItem(lv, 4));

lv[0] = "Republic of Nepal";
lv[1] = "Kathmandu";
lv[2] = "Pokhara";
lv[3] = "Khadichaur";
listView1.Items.Add(new ListViewItem(lv, 1));
lv = new String[4];
lv[0] = "USA";
lv[1] = "Washington DC";
lv[2] = "New York";
lv[3] = "Los Angeles";
listView1.Items.Add(new ListViewItem(lv, 0));

lv[0] = "Italy";
lv[1] = "Rome";
lv[2] = "Venice";
lv[3] = "Milan";
listView1.Items.Add(new ListViewItem(lv, 1));

lv[0] = "Canada";
lv[1] = "Ottawa";
lv[2] = "Montreal";
lv[3] = "Quebec";
listView1.Items.Add(new ListViewItem(lv, 2));

lv[0] = "Switzerland";
lv[1] = "Geneva";
lv[2] = "Zurich";
lv[3] = "Lucerne";
listView1.Items.Add(new ListViewItem(lv, 3));

lv[0] = "UnitedKingdom";
lv[1] = "London";
lv[2] = "ChesterField";
lv[3] = "Wembly";
listView1.Items.Add(new ListViewItem(lv, 4));

lv[0] = "Republic of Nepal";
lv[1] = "Kathmandu";
lv[2] = "Pokhara";
lv[3] = "Khadichaur";
listView1.Items.Add(new ListViewItem(lv, 1));
lv = new String[4];
lv[0] = "USA";
lv[1] = "Washington DC";
lv[2] = "New York";
lv[3] = "Los Angeles";
listView1.Items.Add(new ListViewItem(lv, 0));

lv[0] = "Italy";
lv[1] = "Rome";
lv[2] = "Venice";
lv[3] = "Milan";
listView1.Items.Add(new ListViewItem(lv, 1));

lv[0] = "Canada";
lv[1] = "Ottawa";
lv[2] = "Montreal";
lv[3] = "Quebec";
listView1.Items.Add(new ListViewItem(lv, 2));

lv[0] = "Switzerland";
lv[1] = "Geneva";
lv[2] = "Zurich";
lv[3] = "Lucerne";
listView1.Items.Add(new ListViewItem(lv, 3));

lv[0] = "UnitedKingdom";
lv[1] = "London";
lv[2] = "ChesterField";
lv[3] = "Wembly";
listView1.Items.Add(new ListViewItem(lv, 4));

lv[0] = "Republic of Nepal";
lv[1] = "Kathmandu";
lv[2] = "Pokhara";
lv[3] = "Khadichaur";
listView1.Items.Add(new ListViewItem(lv, 1));
lv = new String[4];
lv[0] = "USA";
lv[1] = "Washington DC";
lv[2] = "New York";
lv[3] = "Los Angeles";
listView1.Items.Add(new ListViewItem(lv, 0));

lv[0] = "Italy";
lv[1] = "Rome";
lv[2] = "Venice";
lv[3] = "Milan";
listView1.Items.Add(new ListViewItem(lv, 1));

lv[0] = "Canada";
lv[1] = "Ottawa";
lv[2] = "Montreal";
lv[3] = "Quebec";
listView1.Items.Add(new ListViewItem(lv, 2));

lv[0] = "Switzerland";
lv[1] = "Geneva";
lv[2] = "Zurich";
lv[3] = "Lucerne";
listView1.Items.Add(new ListViewItem(lv, 3));

lv[0] = "UnitedKingdom";
lv[1] = "London";
lv[2] = "ChesterField";
lv[3] = "Wembly";
listView1.Items.Add(new ListViewItem(lv, 4));

lv[0] = "Republic of Nepal";
lv[1] = "Kathmandu";
lv[2] = "Pokhara";
lv[3] = "Khadichaur";
listView1.Items.Add(new ListViewItem(lv, 1));
lv = new String[4];
lv[0] = "USA";
lv[1] = "Washington DC";
lv[2] = "New York";
lv[3] = "Los Angeles";
listView1.Items.Add(new ListViewItem(lv, 0));

lv[0] = "Italy";
lv[1] = "Rome";
lv[2] = "Venice";
lv[3] = "Milan";
listView1.Items.Add(new ListViewItem(lv, 1));

lv[0] = "Canada";
lv[1] = "Ottawa";
lv[2] = "Montreal";
lv[3] = "Quebec";
listView1.Items.Add(new ListViewItem(lv, 2));

lv[0] = "Switzerland";
lv[1] = "Geneva";
lv[2] = "Zurich";
lv[3] = "Lucerne";
listView1.Items.Add(new ListViewItem(lv, 3));

lv[0] = "UnitedKingdom";
lv[1] = "London";
lv[2] = "ChesterField";
lv[3] = "Wembly";
listView1.Items.Add(new ListViewItem(lv, 4));

lv[0] = "Republic of Nepal";
lv[1] = "Kathmandu";
lv[2] = "Pokhara";
lv[3] = "Khadichaur";
listView1.Items.Add(new ListViewItem(lv, 1));
lv = new String[4];
lv[0] = "USA";
lv[1] = "Washington DC";
lv[2] = "New York";
lv[3] = "Los Angeles";
listView1.Items.Add(new ListViewItem(lv, 0));

lv[0] = "Italy";
lv[1] = "Rome";
lv[2] = "Venice";
lv[3] = "Milan";
listView1.Items.Add(new ListViewItem(lv, 1));

lv[0] = "Canada";
lv[1] = "Ottawa";
lv[2] = "Montreal";
lv[3] = "Quebec";
listView1.Items.Add(new ListViewItem(lv, 2));

lv[0] = "Switzerland";
lv[1] = "Geneva";
lv[2] = "Zurich";
lv[3] = "Lucerne";
listView1.Items.Add(new ListViewItem(lv, 3));

lv[0] = "UnitedKingdom";
lv[1] = "London";
lv[2] = "ChesterField";
lv[3] = "Wembly";
listView1.Items.Add(new ListViewItem(lv, 4));

lv[0] = "Republic of Nepal";
lv[1] = "Kathmandu";
lv[2] = "Pokhara";
lv[3] = "Khadichaur";
listView1.Items.Add(new ListViewItem(lv, 1));

for (int j = 0; j < listView1.Items.Count; j++)
{
ListViewItem lvi = listView1.Items[j];
for (int i = 0; i < lvi.SubItems.Count; i++)
{
Console.Write(lvi.SubItems.Text + "\t");
}
Console.WriteLine("\n");
}
}

private void REPORT_Click(object sender, System.EventArgs e)
{
// Switch to the Report View
this.Text = "Report View";
listView1.View = View.Details;
}

private void LARGEICON_Click(object sender, System.EventArgs e)
{
// Switch to the LargeIcon view
this.Text = "LargeIcon View";
listView1.View = View.LargeIcon;
}

private void SMALLICON_Click(object sender, System.EventArgs e)
{
// Switch to the SmallIcon view
this.Text = "SmallIcon View";
listView1.View = View.SmallIcon;
}

private void LIST_Click(object sender, System.EventArgs e)
{
// Switch to the List view
this.Text = "List View";
listView1.View = View.List;
}

private void SELECTED_Click(object sender, System.EventArgs e)
{
// Get the list of indices for all the selected items

string StrSelected = "ITEM SELECTED FROM THE LISTVIEW ARE ...\n\n";
for (int k = 0; k < listView1.SelectedIndices.Count; k++)
{
StrSelected = StrSelected + "[" + (k + 1) + "] ";
// Get the item ( as ListViewItem ) at the selected index.
ListViewItem lvi = listView1.Items[listView1.SelectedIndices[k]];
for (int i = 0; i < lvi.SubItems.Count; i++)
{
// Get the subItem for the the selected item
StrSelected = StrSelected + lvi.SubItems.Text + "\t";
}
StrSelected = StrSelected + "\n";
}
MessageBox.Show(StrSelected, "Selected Items");
}

private void CHECKED_Click(object sender, System.EventArgs e)
{
// Get the list of indices for all checked items
string StrSelected = "ITEM CHECKED FROM THE LISTVIEW ARE ...\n\n";
for (int k = 0; k < listView1.CheckedIndices.Count; k++)
{
StrSelected = StrSelected + "[" + (k + 1) + "] ";
// Get the item ( as ListViewItem ) at the selected index.
ListViewItem lvi = listView1.Items[listView1.CheckedIndices[k]];
for (int i = 0; i < lvi.SubItems.Count; i++)
{
// Get the subItem for the the selected item
StrSelected = StrSelected + lvi.SubItems.Text + "\t";
}
StrSelected = StrSelected + "\n";
}
MessageBox.Show(StrSelected, "Checked Items");
}

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

}

private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
if (listView1.Sorting == SortOrder.Descending)
listView1.Sorting = SortOrder.Ascending;
else
listView1.Sorting = SortOrder.Descending;
listView1.Sort();
}

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{
listView1.Items.Clear();
ListViewCountry_Load(null, null);
listView1.AutoArrange = true;
}
}

}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top