listview in c# with images [duplicate] - c#

This question already has answers here:
Images in ListView subitem
(2 answers)
Closed 9 years ago.
I wish to make a list of items with pictures, the amount of items can vary from 1-60 and for each item I wish to also show data.
I believe the best way of going about this is using the ListView in c#.
is this true and if so how would I go about doing this?
i have also thought about using interactive images within a scrolling window

If you want to do this in the designer, you can take the following steps to add the
images to the ListView control:
Switch to the designer, click on the ImageList component on the Component Tray,
there will be a smart tag appear on the top-right corner of the ImageList.
Click the smart tag, and click "Choose Images" on the pane.
On the pop-up Image Collection Editor dialog, choose the images from the folder
your want.
Click OK to finish adding images to the ImageList.
Click the ListView on the form, there will be a smart tag appear on the top-right
corner.
Click the smart tag, you will find there're three ComboBoxes there, choose a
ImageList from the list as you want.
Click the "Add items" option on the smart tag, a ListViewItem Collection Editor
will appear, you can add items to the ListView, it's important here to set the
ImageIndex or ImageKey property, or the image won't appear.
Click OK to finish item editing, now you'll find the images are displayed on the
ListView.
If you want to add the images to the ListView by code, you can do something like this`
Code Snippet
private void Form10_Load(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(#"c:\pic");
foreach (FileInfo file in dir.GetFiles())
{
try
{
this.imageList1.Images.Add(Image.FromFile(file.FullName));
}
catch{
Console.WriteLine("This is not an image file");
}
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(32, 32);
this.listView1.LargeImageList = this.imageList1;
//or
//this.listView1.View = View.SmallIcon;
//this.listView1.SmallImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
}
Source

Related

Adding buttons programmatically to a TabControl (TabPage)

I read this topic (Adding buttons to a TabControl Tab in C#) but I don't figure out why my code below add one button only to the tabpage.
I've obviously debugged that the foreach works properly.
foreach (string line in File.ReadAllLines(#"C:\quicklauncher.ini"))
{
TabPage page = new TabPage(foldername);
DirectoryInfo d = new DirectoryInfo(line);
foreach (FileInfo file in d.GetFiles("*.*"))
{
Button button = new Button();
button.Text = file.Name;
button.Click += new EventHandler(button_Click);
page.Controls.Add(button);
}
tabControl.TabPages.Add(page); //add our tab page to the tab control
}
Thanks,
Steve
You thought it added only 1 button for you but in fact it did not, it added all the buttons for you but those buttons had the same Location (which is (0,0) by default). That's why you did think there was only 1 button (because you saw only 1 last button on top of others).
You added buttons automatically to your tabpage, so you should have some rule to locate them, I'm not sure what that rule is but I suppose you want to line them up vertically (just an example), I'm going to correct your code to achieve such a thing, at least you will see it work, and in fact all the buttons are added normally:
//you need some variable to save the next Top for each new button:
//let's call it nextTop:
int nextTop = 0;
foreach (FileInfo file in d.GetFiles("*.*"))
{
Button button = new Button { Top = nextTop,
Text = file.Name };
button.Click += new EventHandler(button_Click);
page.Controls.Add(button);
nextTop += button.Height + 5; //it's up to you on the
//Height and vertical spacing
}
//...
You can also try using some layout control like FlowLayoutPanel and TableLayoutPanel to contain all the buttons, they can help arrange your buttons in some way you may want, just try it.

C# List text files in a listbox and let user open them in a textbox

I'am pretty new to C# and I want a listbox with all text files in a folder and if the user dubbleclicks on the listed file the file will display in a textbox.
I don't want to use the openFileDialog function because the textfiles are on a webserver which I access using username:password#server.com/folder.
Kinda like a texteditor limited to edit only files in 1 folder :)
If this is possible using openFileDialog please tell me how.
I hope you can understand what I want to do.
Greetings,
From what I understand you are after, you want to iterate through the files in a specific directory and then allow them to be edited once opened with a double click in a listbox.
This can be done using the var Files = Directory.GetFiles("path", ".txt");
Files would be a string[] of the file names.
Then filling the Listbox with the files something like this:
ListBox lbx = new ListBox();
lbx.Size = new System.Drawing.Size(X,Y); //Set to desired Size.
lbx.Location = new System.Drawing.Point(X,Y); //Set to desired Location.
this.Controls.Add(listBox1); //Add to the window control list.
lbx.DoubleClick += OpenFileandBeginEditingDelegate;
lbx.BeginUpdate();
for(int i = 0; i < numfiles; i++)
lbx.Items.Add(Files[i]);
lbx.EndUpdate();
Now your event delegate should look something like this:
OpenFileandBeginEditingDelegate(object sender, EventArgs e)
{
string file = lbx.SelectedItem.ToString();
FileStream fs = new FileStream(Path + file, FileMode.Open);
//Now add this to the textbox
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
tbx.Text += temp.GetString(b);//tbx being the textbox you want to use as the editor.
}
}
Now to add an event handler through the VS window editor click on the control in question and go to the properties pane for that control. You will need to then switch to the events pane and scroll untill you find the DoubleClick event if you use that the designer should auto-insert a valid delegate signature and allow you to write the logic for the event.

C# winform change selected tabcontrol image

I have a winform application written in C#. I had an imageList in my winform and I have a tabcontrol and each of the tab I assign a image as icon for the tab by changing ImageIndex.
However they have only 1 image for each tab and I want them to change to another image for the selected tab (like another highlighted image for active one). I have an idea to add all images to the imageList (both active and inactive images) and change the imageIndex of the selected tab. But I am not sure how to do it in practical.
Here are my current codes that I can come up with:
Inside SelectedIndexChange event, I have a function:
foreach (TabPage tab in tabControl1)
{
if (tab.index == tabControl1.SelectedIndex) { <---how to get the index?
tab.imageIndex = tab.index + tabControl1.TabCount;
} else {
tab.imageIndex = tab.index;
}
}
I came up with a solution
for (int i=0; i<tabControl1.TabPages.Count; i++)
{
if (tabControl1.TabPages[i] == tabControl1.SelectedTab)
{
tabControl1.TabPages[i].ImageIndex = i + tabControl1.TabPages.Count;
}
else
{
tabControl1.TabPages[i].ImageIndex = i;
}
}

How to add system icons in an imagelist in a listview

I have added icons in a imageList via using this code in the listview. Now I want them to be displayed whenever the list view of certain directory is shown.
My question is:
What changes would I need to make in imagelist1 control? And how to call imagelist1 in the code?
imageList1.Images.Add(
BlackFox.Win32.Icons.IconFromExtensionShell(
".*",
BlackFox.Win32.Icons.SystemIconSize.Small));
//lv.ImageIndex = 1;
If I understand you correctly you want to display the icons in the ImageList together with the corresponding files in the ListView. To do this you only need to point the SmallImageList or LargeImageList attribute of your ListView object to the ImageList (depending on the icon display mode your ListView uses).
private void UpdateListView() {
ImageList IconList = new ImageList();
IconList.Images.Add(
BlackFox.Win32.Icons.IconFromExtensionShell(".*",
BlackFox.Win32.Icons.SystemIconSize.Small));
YourListview.SmallImageList = IconList;
//Add the items to your Listview
}
Don't forget to assign the icons in the ImageList to the items in the ListView:
MyListItem.ImageIndex = 0;
or
MyListItem.ImageKey = "MyImageName";
or add them right away when you add your ListItems:
ListViewItem MyListItem= new ListViewItem("ItemName", "MyImageName");
ListViewItem MyListItem2= new ListViewItem("ItemName2", int ImageIndex);

adding files to listview in C#

I have a Listview and an "ADD" button,when I click on ADD i should be able to browse the files in computer, select files and when click OK or the Open, the file list should be added in the listview...how to do that...is listview correct or any other alternative...?
ListView should be fine for file listing. Just be aware that longer file paths are difficult to see (have to horizontally scroll which is bad!) if you are gonna just add the full path to list. You can toy with the idea of other representation like:
File.Txt (C:\Users\Me\Documents)
C:\Users\..\File.Txt
etc
As far as doing it using code is concerned, you will need to use OpenFileDialog control to let user choose the files.
var ofd = new OpenFileDialog ();
//add extension filter etc
ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
if(ofd.ShowDialog() == DialogResult.OK)
{
foreach (var f in openFileDialog1.FileNames)
{
//Transform the list to a better presentation if needed
//Below code just adds the full path to list
listView1.Items.Add (f);
//Or use below code to just add file names
//listView1.Items.Add (Path.GetFileName (f));
}
}
If you want to do this in the designer, you can take the following steps to add the images to the ListView control:
Switch to the designer, click on the ImageList component on the Component Tray, there will be a smart tag appear on the top-right corner of the ImageList.
Click the smart tag, and click "Choose Images" on the pane.
On the pop-up Image Collection Editor dialog, choose the images from the folder your want.
Click OK to finish adding images to the ImageList.
Click the ListView on the form, there will be a smart tag appear on the top-right corner.
Click the smart tag, you will find there're three ComboBoxes there, choose a ImageList from the list as you want.
Click the "Add items" option on the smart tag, a ListViewItem Collection Editor will appear, you can add items to the ListView, it's important here to set the ImageIndex or ImageKey property, or the image won't appear.
Click OK to finish item editing, now you'll find the images are displayed on the ListView.
If you want to add the images to the ListView by code, you can do something like this
give the following code in addButton_click
var fdlg = new OpenFileDialog();
fdlg.Multiselect = true;
fdlg.Title = "Select a file to add... ";
fdlg.InitialDirectory = "C:\\";
fdlg.Filter = "All files|*.*";
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
foreach (var files in fdlg.FileNames)
{
try
{
this.imageList1.Images.Add(Image.FromFile(files));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(32, 32);
this.listView1.LargeImageList = this.imageList1;
//or
//this.listView1.View = View.SmallIcon;
//this.listView1.SmallImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
}

Categories

Resources