ListView columns show up overlapped
-
I am new to .NET in general and Windows Forms programming in particular. As a part of a bigger project, I am trying to write a test application. I have a ListView control whose number of rows and columns are determined by the user at runtime. To read the number of rows and columns, I have provided two textboxes. I have a refresh button, which when clicked, reads the number of rows and columns from the text boxes and tries to populate the ListView control. When I run the program and click on the Refresh button, it appears as if nothing has happened in response to that click. The fact, however, is that the ListView control gets populated successfully, but no data is visible because the columns somehow overlap each other. I verified this by clicking inside the ListView control and pressing "CTRL +". On doing that, the columns auto-arrange themselves and become visible. My gut feeling is that this is something really simple and stupid. Please help me out! ========================= CODE BEGINS ===========================================================
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ListViewTest { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox TB_Rows; private System.Windows.Forms.TextBox TB_Columns; private System.Windows.Forms.Label LABEL_Rows; private System.Windows.Forms.Button BTN_Refresh; private System.Windows.Forms.ListView LV_Data; private System.Windows.Forms.Label LABEL_Columns; private System.Windows.Forms.TextBox TB_BaseString; private System.Windows.Forms.Label LABEL_BaseString; private int m_iColumnCount; private int m_iRowCount; private string m_szBaseString; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify
-
I am new to .NET in general and Windows Forms programming in particular. As a part of a bigger project, I am trying to write a test application. I have a ListView control whose number of rows and columns are determined by the user at runtime. To read the number of rows and columns, I have provided two textboxes. I have a refresh button, which when clicked, reads the number of rows and columns from the text boxes and tries to populate the ListView control. When I run the program and click on the Refresh button, it appears as if nothing has happened in response to that click. The fact, however, is that the ListView control gets populated successfully, but no data is visible because the columns somehow overlap each other. I verified this by clicking inside the ListView control and pressing "CTRL +". On doing that, the columns auto-arrange themselves and become visible. My gut feeling is that this is something really simple and stupid. Please help me out! ========================= CODE BEGINS ===========================================================
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ListViewTest { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox TB_Rows; private System.Windows.Forms.TextBox TB_Columns; private System.Windows.Forms.Label LABEL_Rows; private System.Windows.Forms.Button BTN_Refresh; private System.Windows.Forms.ListView LV_Data; private System.Windows.Forms.Label LABEL_Columns; private System.Windows.Forms.TextBox TB_BaseString; private System.Windows.Forms.Label LABEL_BaseString; private int m_iColumnCount; private int m_iRowCount; private string m_szBaseString; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify
private void CreateColumns () { // Create the "Item" column first //this.LV_Data.Columns.Add ("Item Column", -2, HorizontalAlignment.Left); // Now create the data columns for (int i=0; i < m_iColumnCount; i++) this.LV_Data.Columns.Add ("Column " + i.ToString(), -2 * (i+1), HorizontalAlignment.Left); } ur setting the width of your columns to -2*(i+1) which will always be <0. I dont understand what ur trying to do here, but if u just change the width to a fixed one (70), everything works fine.
-
private void CreateColumns () { // Create the "Item" column first //this.LV_Data.Columns.Add ("Item Column", -2, HorizontalAlignment.Left); // Now create the data columns for (int i=0; i < m_iColumnCount; i++) this.LV_Data.Columns.Add ("Column " + i.ToString(), -2 * (i+1), HorizontalAlignment.Left); } ur setting the width of your columns to -2*(i+1) which will always be <0. I dont understand what ur trying to do here, but if u just change the width to a fixed one (70), everything works fine.
Thanks a lot. :) :) I got the -2 from a Microsoft sample and then while playing around with the code, I added that (i+1) part. Here's the sample I am talking about: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistviewlistviewitemcollectionclassaddrangetopic.asp[^] Thanks a lot again.