• 0

[VB.NET] ListBox index


Question

Hi guys,

I fairly advanced in my project but it seems to me that i'm stuck in this phase.

Actually i'm working on kinda catalogue for movies and i would like to implement next features within:

1. within ListBox appears the titles (names) of the movies //already done

2. within PictureBox appears DVD covers (.jpg format) respectively to movie

3. Label get the info from column short content for the movies etc.

So my issue is how when i choose an item from ListBox that give the info to PictureBox to retrieve the url for the pic i put in DB i.e. pic/1100.jpg ... means make linkable the items from ListBox component .... that would fill the PictureBox and Label with certain items from DB

Thanks in advance

Note: I'm working with VB.NET 2003 and MS Access 2003 DB that has these three column i mentioned above - title, url and Plot Outline

Link to comment
https://www.neowin.net/forum/topic/227143-vbnet-listbox-index/
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Just subscribe to the IndexChanged event on the listbox and instead of inserting just strings into your listbox, insert a class or a struct that exposes all the information you need. Then you just set the ValueMember and DisplayMember on the ListBox. If that doesn't make sense I'll describe it in more detail for you.

  • 0

Okay I'm back at work. Don't know why my computer decided it needed to hybernate.

Anywho...don't worry about it. I'm having fun doing this. Here's my winform example.

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

namespace WindowsApplication1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
  private System.Net.WebClient m_client;
  private System.Net.WebClient Client
  {
 	 get
 	 {
    if(m_client == null) m_client = new System.Net.WebClient();
    return m_client;
 	 }
  }
  private System.Windows.Forms.ListBox listBox1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.PictureBox pictureBox1;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
 	 //
 	 // Required for Windows Form Designer support
 	 //
 	 InitializeComponent();

 	 listBox1.DisplayMember = "Title";
 	 listBox1.ValueMember = "ImageURL";

 	 listBox1.Items.Add(new DVDInfo("Daft Punk & Leiji Matsumoto's Interstella 5555: The 5tory of the 5ecret 5tar 5ystem",
    @"http://images.amazon.com/images/P/B0000E6XH3.01.LZZZZZZZ.jpg"));

 	 listBox1.Items.Add(new DVDInfo("Spider-Man 2",
    @"http://images.amazon.com/images/P/B00005JMQW.01.LZZZZZZZ.jpg"));

 	 listBox1.Items.Add(new DVDInfo("Wonder Woman - The Complete First Season",
    @"http://images.amazon.com/images/P/B0001ZMWYG.01.LZZZZZZZ.jpg"));
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
 	 if( disposing )
 	 {
    if (components != null) 
    {
   	 components.Dispose();
    }
 	 }
 	 base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
 	 this.listBox1 = new System.Windows.Forms.ListBox();
 	 this.label1 = new System.Windows.Forms.Label();
 	 this.pictureBox1 = new System.Windows.Forms.PictureBox();
 	 this.SuspendLayout();
 	 // 
 	 // listBox1
 	 // 
 	 this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
 	 this.listBox1.Location = new System.Drawing.Point(16, 16);
 	 this.listBox1.Name = "listBox1";
 	 this.listBox1.Size = new System.Drawing.Size(256, 121);
 	 this.listBox1.TabIndex = 0;
 	 this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
 	 // 
 	 // label1
 	 // 
 	 this.label1.AutoSize = true;
 	 this.label1.Location = new System.Drawing.Point(24, 152);
 	 this.label1.Name = "label1";
 	 this.label1.Size = new System.Drawing.Size(0, 16);
 	 this.label1.TabIndex = 1;
 	 // 
 	 // pictureBox1
 	 // 
 	 this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
 	 this.pictureBox1.Location = new System.Drawing.Point(16, 176);
 	 this.pictureBox1.Name = "pictureBox1";
 	 this.pictureBox1.Size = new System.Drawing.Size(256, 144);
 	 this.pictureBox1.TabIndex = 2;
 	 this.pictureBox1.TabStop = false;
 	 // 
 	 // Form1
 	 // 
 	 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
 	 this.ClientSize = new System.Drawing.Size(288, 331);
 	 this.Controls.Add(this.pictureBox1);
 	 this.Controls.Add(this.label1);
 	 this.Controls.Add(this.listBox1);
 	 this.Name = "Form1";
 	 this.Text = "Form1";
 	 this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main() 
  {
 	 Application.Run(new Form1());
  }

  private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
 	 try
 	 {
    DVDInfo info = (DVDInfo)listBox1.SelectedItem;
    label1.Text = info.ImageURL;


    // Got this from http://blogs.msdn.com/danielfe/archive/2004/07/26/197811.aspx
    System.IO.Stream imageStream = Client.OpenRead(info.ImageURL);
    System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream);
    pictureBox1.Image = image;
 	 }
 	 catch(Exception ex){MessageBox.Show(ex.Message, "Error");}
  }
	}

	public class DVDInfo
	{
  string m_Title;
  public string Title
  {
 	 get{return m_Title;}
 	 set{m_Title = value;}
  }
  string m_ImageURL;
  public string ImageURL
  {
 	 get{return m_ImageURL;}
 	 set{m_ImageURL = value;}
  }

  public DVDInfo(string title, string imageURL)
  {
 	 this.Title = title;
 	 this.ImageURL = imageURL;
  }
	}
}

Hopefully that can shed some light on your problem. Have a good one.

  • 0

Hi MrRogers,

Thanks for the example ... it works flawless.... but obviously we have some misunderstanding ... my project is in VB.net manner ... so this from above seems to me as new world ... lol

I hope you don't mind because i was sincerely toward you ? lol

Means if you have an example within VB manner it would really help me ? anyhow I may pull out kind of conclusion from your example cause I have no problem to make a code but cannot get a logic ?. Lol

I.e.

Within flash I made next thing

First I?m connection to db, and then I delimit rows and columns in next _root.fielddelimiter = "#";

_root.rowdelimiter = ";";

dvdArray = dvdData.split(rowdelimiter);er);

Then I make function for ListBox that collects columns

function displayFiles() {
	box.text = "";
	myList.removeAll();
	ph.unloadMovie();
	for (i=0; i<dvdCount; i++) {
 ?myArray = dvdArray[i].split("#");
 ?myList.addItem(myArray[1], myArray[3]+"%"+myArray[2]+"%"+myArray[7]+"%"+myArray[9]);
	}
}

And because FSP works with fscommands I join this code to select from database:

// dvdSQL is build for specific task
fscommand("flashstudio.selectfromdb", "dvdSQL");
// count returned records to variable
fscommand("flashstudio.getrecordcount", "dvdCount");
// get records to variable
fscommand("flashstudio.getdelfromdb", "dvdData,fielddelimiter,rowdelimiter");

If this looks unclear to you I will send you my already done application .exe (as i told you i have this project done but performances is very low so i have to make same app within VB.NET manner) to install and get the logic what I?m trying to do now

Thanks in advance

Kind regards

Note: Nearly i forgot to point out how i add pic url to PictureBox (it has specific action in ActionScript)

path = myVar[1];
//i'm telling the path to movie where pic is located
img = fsp_appdir+"/"+path;
	loadMovie(img, "_root.ph");

  • 0

jugomkd

The main point I was trying to make with my demo code, which I had a little too much fun with, was that you can place a class or a structure into the listbox that can contain all the information you need. Like:

public class DVDInfo
{
 string m_Title;
 public string Title
 {
  get{return m_Title;}
  set{m_Title = value;}
 }
 string m_ImageURL;
 public string ImageURL
 {
  get{return m_ImageURL;}
  set{m_ImageURL = value;}
 }

 public DVDInfo(string title, string imageURL)
 {
  this.Title = title;
  this.ImageURL = imageURL;
 }
}

and inserting it into the listbox:

listBox1.DisplayMember = "Title";
  listBox1.ValueMember = "ImageURL";

  listBox1.Items.Add(new DVDInfo("Daft Punk & Leiji Matsumoto's Interstella 5555: The 5tory of the 5ecret 5tar 5ystem",
   @"http://images.amazon.com/images/P/B0000E6XH3.01.LZZZZZZZ.jpg"));

  listBox1.Items.Add(new DVDInfo("Spider-Man 2",
   @"http://images.amazon.com/images/P/B00005JMQW.01.LZZZZZZZ.jpg"));

  listBox1.Items.Add(new DVDInfo("Wonder Woman - The Complete First Season",
   @"http://images.amazon.com/images/P/B0001ZMWYG.01.LZZZZZZZ.jpg"));

When a user selects an item on the list, you then pick up the changed index event from the listbox. You can then get that object that you placed within the listbox for the index and you'll have all the information you need to either go to the db or if you already populated your object with the information needed to find the image then you can access it then. As in:

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
 {
  try
  {
   DVDInfo info = (DVDInfo)listBox1.SelectedItem;
   label1.Text = info.ImageURL;


   // Got this from http://blogs.msdn.com/danielfe/archive/2004/07/26/197811.aspx
   System.IO.Stream imageStream = Client.OpenRead(info.ImageURL);
   System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream);
   pictureBox1.Image = image;
  }
  catch(Exception ex){MessageBox.Show(ex.Message, "Error");}
 }

Make sense? That's all I was trying to show with my example. Just the pattern.

As far as my example not being in VB I'm sorry, but I don't know VB. I try to stay away from it! :wacko:

  • 0

Ok,

Let?s put things in this way:

I found that vb.net IDE could be very useful for newbie .... i.e. i draw couple win Forms (listBox, Label, ComboBox and Button) on stage and make OleDbconnection, OleDbdataAdapter, and Generated DataSet with needed data (column ---- SELECT col1, col2, col3, FROM mytable) from my database (MS access) ... then in properties window i'm setting the DataSource and DisplayMember for ListBox, after that i set up the text properties in databindings for Label (Intended column within db) and same for ComboBox ... then i just join code to the button to Fill dataSet

//for instance
DSetDVD1.Clear()
OleDbDataAdapter1.Fill(DSetDVD1)

And that's all .... and just run ... it works flawless .... when i select some item from List box Label and Combo present info respectively to the selected Item in List box (read Row in Database) .... lol

But i didn't find the way how to put url of pictures within databindings of PictureBox ... i.e i have column url and movie1 has url like: "pictures/movie1.jpg"

I hope somebody will help me ... Thanks in advance

Kind regards

  • 0

I advanced for a while in meantime so my present issue that producing me a lot of headaches is next:

I have code (event of ListBox.SelectedItem) that should load image into PictureBox1 but it backs to me eror message like this (it compiling without errors and if i choose an item from ListBox it appears then) System.InvalidCastException

My code for this event is next:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim arrayImage() As Byte = CType(Me.DSetDVD1.Tables(0).Rows(Me.ListBox1.SelectedIndex)("url"), Byte())
        Dim ms As New MemoryStream(arrayImage)
            With Me.PictureBox1
            .Image = Image.FromStream(ms)
            End With
    End Sub

what i'm doing wrong? give me advice or link for sucha example .. i tried all i knew but .... i'm dissapointed

  • 0

Hi weenur,

Firs off, thanks for reply, but i think it doesn't coorelate with my issue but rather i need something like:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        PictureBox1.Image = Image.FromFile _
        (System.Environment.GetFolderPath _
        (System.Environment.SpecialFolder.MyPictures) _
        & "\1001.jpg")
    End Sub

and it works only with one picture located in MyPictures on local Drive C while i need folder located on same level with my APP

So, i need solution instead 1001.jpg to get value from url column within my database (MS ACCESS)

Any Help will be apreciate .. please

Kind regards

  • 0

Hi

I'm very close to solution but obviously need some additionally help ... lol

I found that for non HTTP pat i can use System.Drawing.Bitmap.FromFile and i succeed to load an image but with absolute path i.e.

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
 ? ? ? ?PictureBox1.Image = System.Drawing.Bitmap.FromFile _
 ? ? ? ?("pic\1002.jpg")
End Sub

so, when i select an item (whatever i choose) from ListBox WinForm i get loaded Picture in PictureBox but only that one i put in code from above .. so i would like to someone help me getting path as string or else from databasurl>urlvaluealue so i would be able loading images regard to selected item in ListBox .... thanks in advance

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.