Mam ListView In My Winform, który ma 4 kolumny, Nazwa, Pieniądze, ID i Poziom.Zapobieganie zmianie rozmiaru kolumn w ListView?
Problem polega na tym, że po uruchomieniu aplikacji nadal mam możliwość wprowadzania kolumn o szerokości i zmieniania ich.
Szukałem i stwierdził, że należy zrobić coś takiego:
private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
e.Cancel = true;
e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
}
Ale problemem jest to, że kiedy debugowany i Ccanged szerokości kolumn, wydarzenie to nawet nie pali!
Dlaczego nie wystrzelono?
Jak ustawić szerokość kolumn?
Zrobiłem nową aplikację WinForm na wszelki wypadek gdyby coś było nie tak w moim starym, on zwolniony, ale tylko po raz pierwszy uruchomiony aplikację .. Oto kod:
namespace CsharpWinformTestingStuff
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listView1.ColumnWidthChanging += new ColumnWidthChangingEventHandler(listView1_ColumnWidthChanging);
}
void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
e.Cancel = true;
e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
}
}
}
tutaj jest składnikiem initialize na wszelki wypadek może chcesz wiedzieć:
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.SuspendLayout();
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(12, 12);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(284, 275);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "Name";
this.columnHeader1.Width = 97;
//
// columnHeader2
//
this.columnHeader2.Text = "Age";
this.columnHeader2.Width = 52;
//
// columnHeader3
//
this.columnHeader3.Text = "Email";
this.columnHeader3.Width = 157;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(308, 299);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
możliwy duplikat [jak naprawić szerokość kolumny widoku listy w oknie C#?] (Http://stackoverflow.com/questions/2460308/how-to-fix-the-column-width- of-a-listview-in-c-sharp-windows-form) –
strzela. Spróbuj upewnić się, że zdarzenie jest prawidłowo rejestrowane. – nawfal