Rahul Kaushik

Rahul Kaushik

  • 1k
  • 608
  • 63.5k

How do i add image in listbox

Mar 22 2016 8:47 PM
i Was building a program and i want to add image in the list box when the button is clicked
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Aspoke
{
public partial class Form5 : Form
{
List<Bitmap> _items = new List<Bitmap>();
public Form5()
{
InitializeComponent();
//_items.Add(Bitmap.FromFile("FileOpen.bmp"));
//img_fileopen = new Bitmap("FileOpen.bmp");
}
Bitmap img_fileopen;
static string ss;
int flag = 0;
string str;
private void button1_Click(object sender, EventArgs e)
{
flag = 0;
ss = textBox1.Text;
if (ss.Length > 30)
{
while (ss.Length > 30)
{
try
{
str = ss.Substring(0, 30);
str = str.Substring(0, str.LastIndexOf(" "));
listBox1.Items.Add(str);
ss = ss.Replace(str + " ", "");
}
catch (Exception)
{
str = ss.Substring(0, 30);
listBox1.Items.Add(str);
ss = ss.Replace(str, "");
}
if (ss.Length < 30)
{ flag = 2; }
}
if (flag == 2)
{
listBox1.Items.Add(ss);
}
}
else
{
listBox1.Items.Add(ss);
}
}
private void Form5_Load(object sender, EventArgs e)
{
}
private void Form5_Shown(object sender, EventArgs e)
{
textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
// The Add button was clicked.
// _items.Add(img_fileopen);
String path = "FileOpen.bmp";
var ms = new MemoryStream(File.ReadAllBytes(path));
listBox1.Items.Add(Image.FromStream(ms));
// Change the DataSource.
}
}
}
 

Answers (2)