Problem with imagelist

G

Guest

Hi all,
I have the following problem:

I'm using C# programming language, MSVS 2003 .NET.
In my application I create a form with listview & imagelist controls.
I add some images with size 32x32 pixels in the imagelist. Set property of
listview "LargeImageList" to be equal to this imagelist.
I add a second form to project which is started from the first. Start the
application - everything is OK.
Now I add an imagelist control in the second form. If the imagelist property
"ImageSize" is set to 16x16 pixels( by default ) when start the application -
everything is OK.
When I change the imagelist property "ImageSize" to 32x32 pixels and run the
application.
Then go from first to second form, close second form, and when the first
form shows up images of the listview control disappear.
Can anybody help me, please ?

This is my code if it help to understand my problem:
Code:
///Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace TestApplication
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
InitializeComponent();

this.listView.Activation = ItemActivation.OneClick;
}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.listView = new System.Windows.Forms.ListView();
this.imageList1 = new System.Windows.Forms.ImageList();
//
// listView
//
this.listView.LargeImageList = this.imageList1;
this.listView.Location = new System.Drawing.Point(8, 8);
this.listView.Size = new System.Drawing.Size(224, 256);
this.listView.ItemActivate += new
System.EventHandler(this.listView_ItemActivate);
//
// imageList1
//
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))))
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))))
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource2"))));
this.imageList1.ImageSize = new System.Drawing.Size(32, 32);
//
// Form1
//
this.Controls.Add(this.listView);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.listView.Items.Clear();
ListViewItem lvi;

lvi = new ListViewItem("Test1");
lvi.ImageIndex = 0;
this.listView.Items.Add(lvi);

lvi = new ListViewItem("Test2");
lvi.ImageIndex = 1;
this.listView.Items.Add(lvi);

lvi = new ListViewItem("Test3");
lvi.ImageIndex = 2;
this.listView.Items.Add(lvi);
}

private void listView_ItemActivate(object sender, System.EventArgs e)
{
if ( this.listView.SelectedIndices.Count > 0 )
switch ( this.listView.SelectedIndices[0] )
{
case 0:
this.listView.Items[0].Selected = false;
Form2 form = new Form2();
form.ShowDialog();
break;
}
}
}
}

///Form2.cs

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

namespace TestApplication
{
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.ImageList imageList1;

public Form2()
{
InitializeComponent();

}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.imageList1 = new System.Windows.Forms.ImageList();
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(32, 32);
//
// Form2
//
this.Text = "Form2";

}
#endregion
}
}

Thanks in advance.
 
D

Darren Shaffer

Ichka,

1. How are you closing Form2 ? this.Close() or this.DialogResult =
DialogResult.OK ? Since
you are calling ShowDialog() to show Form2, you should use the DialogResult
syntax.

2. I don't see you setting the ListView view property in either form. In
other words, I don't
see any code like this: "this.listView.View =
System.Windows.Forms.View.LargeIcon;" or
"this.listView.View = System.Windows.Forms.View.SmallIcon;"

- Darren Shaffer

Ichka said:
Hi all,
I have the following problem:

I'm using C# programming language, MSVS 2003 .NET.
In my application I create a form with listview & imagelist controls.
I add some images with size 32x32 pixels in the imagelist. Set property of
listview "LargeImageList" to be equal to this imagelist.
I add a second form to project which is started from the first. Start the
application - everything is OK.
Now I add an imagelist control in the second form. If the imagelist
property
"ImageSize" is set to 16x16 pixels( by default ) when start the
application -
everything is OK.
When I change the imagelist property "ImageSize" to 32x32 pixels and run
the
application.
Then go from first to second form, close second form, and when the first
form shows up images of the listview control disappear.
Can anybody help me, please ?

This is my code if it help to understand my problem:
Code:
///Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace TestApplication
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
InitializeComponent();

this.listView.Activation = ItemActivation.OneClick;
}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.listView = new System.Windows.Forms.ListView();
this.imageList1 = new System.Windows.Forms.ImageList();
//
// listView
//
this.listView.LargeImageList = this.imageList1;
this.listView.Location = new System.Drawing.Point(8, 8);
this.listView.Size = new System.Drawing.Size(224, 256);
this.listView.ItemActivate += new
System.EventHandler(this.listView_ItemActivate);
//
// imageList1
//
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource2"))));
this.imageList1.ImageSize = new System.Drawing.Size(32, 32);
//
// Form1
//
this.Controls.Add(this.listView);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.listView.Items.Clear();
ListViewItem lvi;

lvi = new ListViewItem("Test1");
lvi.ImageIndex = 0;
this.listView.Items.Add(lvi);

lvi = new ListViewItem("Test2");
lvi.ImageIndex = 1;
this.listView.Items.Add(lvi);

lvi = new ListViewItem("Test3");
lvi.ImageIndex = 2;
this.listView.Items.Add(lvi);
}

private void listView_ItemActivate(object sender, System.EventArgs e)
{
if ( this.listView.SelectedIndices.Count > 0 )
switch ( this.listView.SelectedIndices[0] )
{
case 0:
this.listView.Items[0].Selected = false;
Form2 form = new Form2();
form.ShowDialog();
break;
}
}
}
}

///Form2.cs

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

namespace TestApplication
{
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.ImageList imageList1;

public Form2()
{
InitializeComponent();

}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.imageList1 = new System.Windows.Forms.ImageList();
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(32, 32);
//
// Form2
//
this.Text = "Form2";

}
#endregion
}
}

Thanks in advance.
 
G

Guest

Hi Darren,
1. Yes I close Form2 with this.DialogResult = DialogResult.OK.
2. You don't see this.listView.View = System.Windows.Forms.View.LargeIcon in
my code because this is a default property of MS Visual Designer and this
part of code is not included in method InitializeComponent().
I add it manually but still no result.
Thanks for you reply.
Any other suggestions?

Darren Shaffer said:
Ichka,

1. How are you closing Form2 ? this.Close() or this.DialogResult =
DialogResult.OK ? Since
you are calling ShowDialog() to show Form2, you should use the DialogResult
syntax.

2. I don't see you setting the ListView view property in either form. In
other words, I don't
see any code like this: "this.listView.View =
System.Windows.Forms.View.LargeIcon;" or
"this.listView.View = System.Windows.Forms.View.SmallIcon;"

- Darren Shaffer

Ichka said:
Hi all,
I have the following problem:

I'm using C# programming language, MSVS 2003 .NET.
In my application I create a form with listview & imagelist controls.
I add some images with size 32x32 pixels in the imagelist. Set property of
listview "LargeImageList" to be equal to this imagelist.
I add a second form to project which is started from the first. Start the
application - everything is OK.
Now I add an imagelist control in the second form. If the imagelist
property
"ImageSize" is set to 16x16 pixels( by default ) when start the
application -
everything is OK.
When I change the imagelist property "ImageSize" to 32x32 pixels and run
the
application.
Then go from first to second form, close second form, and when the first
form shows up images of the listview control disappear.
Can anybody help me, please ?

This is my code if it help to understand my problem:
Code:
///Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace TestApplication
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
InitializeComponent();

this.listView.Activation = ItemActivation.OneClick;
}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.listView = new System.Windows.Forms.ListView();
this.imageList1 = new System.Windows.Forms.ImageList();
//
// listView
//
this.listView.LargeImageList = this.imageList1;
this.listView.Location = new System.Drawing.Point(8, 8);
this.listView.Size = new System.Drawing.Size(224, 256);
this.listView.ItemActivate += new
System.EventHandler(this.listView_ItemActivate);
//
// imageList1
//
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("resource2"))));
this.imageList1.ImageSize = new System.Drawing.Size(32, 32);
//
// Form1
//
this.Controls.Add(this.listView);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.listView.Items.Clear();
ListViewItem lvi;

lvi = new ListViewItem("Test1");
lvi.ImageIndex = 0;
this.listView.Items.Add(lvi);

lvi = new ListViewItem("Test2");
lvi.ImageIndex = 1;
this.listView.Items.Add(lvi);

lvi = new ListViewItem("Test3");
lvi.ImageIndex = 2;
this.listView.Items.Add(lvi);
}

private void listView_ItemActivate(object sender, System.EventArgs e)
{
if ( this.listView.SelectedIndices.Count > 0 )
switch ( this.listView.SelectedIndices[0] )
{
case 0:
this.listView.Items[0].Selected = false;
Form2 form = new Form2();
form.ShowDialog();
break;
}
}
}
}

///Form2.cs

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

namespace TestApplication
{
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.ImageList imageList1;

public Form2()
{
InitializeComponent();

}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.imageList1 = new System.Windows.Forms.ImageList();
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(32, 32);
//
// Form2
//
this.Text = "Form2";

}
#endregion
}
}

Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top