Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi, I'm making a film and entertainment system in a windows form which allows users to select films. I made a custom listbox control which displays a list of films and when the user selects a film, the item in the listbox changes colour. I put this control in the windows form put the code doesnt work, it says that Item and Selected Item haven't been defined?

Heres my code for the custom control:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ColorListBox
{    public partial class ListboxControl : UserControl    
{        public ListboxControl()        
{            InitializeComponent();        }        
private void lbxColour_SelectedIndexChanged(object sender, EventArgs e)        {            Color namedColor;            
String colorName;            
colorName = lbxColour.SelectedItem.ToString();           
 if (colorName != "Black")            
{                namedColor = Color.FromName(colorName);                            }             
  }       
 public string NamedColor 
        {            get { return lbxColour.SelectedItem.ToString(); }        
}        
private void AddNamedColours()      
  {            lbxColour.Items.Add("Unknown");            lbxColour.Items.Add("Black");       
     lbxColour.Items.Add("Blue");           
 lbxColour.Items.Add("Brown");           
 lbxColour.Items.Add("Chocolate");         
   lbxColour.Items.Add("DarkGreen");           
 lbxColour.SelectedItem = "Blue";        }       
 protected override void OnPaint(PaintEventArgs e)        
{            e.Graphics.DrawRectangle(new Pen(Color.Silver, 2), 0, 0, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1);        }    
}
}


And heres the code for the windows form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Win32;

namespace FilmAndEntertainmentSystem
{    public partial class FrmFES : Form   
 {       static string ta = null; //global variable       
 public FrmFES()       
 {            InitializeComponent();        }   
  private void cbxGenre_SelectedIndexChanged(object sender, EventArgs e)         {            ta = cbxGenre.SelectedItem.ToString();             
if (ta == "Action/Thriller")            
 {                lbxColour.Items.Clear();                
 lbxColour.Items.Add("CasinoRoyale");                                      
lbxColour.Items.Add("Die Hard");               
 lbxColour.Items.Add("Raiders of the Lost Ark");       
     }           
 else if (ta == "Sci Fi")            
{                lbxColour.Items.Clear();               
 lbxColour.Items.Add("Blade Runner");                
lbxColour.Items.Add("Star Wars Trilogy");                
lbxColour.Items.Add("Star Trek");            }
}.
Posted
Updated 31-Mar-11 4:07am
v2

If you inherit from UserControl you need to do everything yourself, including managing the list of items that are in the list box. For example, here is my control to solve a similar problem.
 
Share this answer
 
Comments
programmer1234 31-Mar-11 10:21am    
So I need to put the list items for the listbox in the actual control for the listbox?
Sergey Alexandrovich Kryukov 31-Mar-11 16:21pm    
There is not point of ever inheriting from UserControl.
You need to inherit from Control and develop it all from scratch or inherit from either System.Windows.Forms.ListBox of System.Windows.Forms.ListControl and use some of the functionality of the base class.
--SA
BobJanova 31-Mar-11 18:11pm    
Inheriting from UserControl is better than Control. That's what UserControl is there for, in fact. Inheriting from the controls that are a cover over one of the Windows native ones can be tricky as some things don't work as you'd expect (because they're not really being done by .Net in the first place). In particular, relevant to this question, I'm not aware of a way to override ListBox to paint the lines different colours.

Inheriting from UserControl requires quite a bit of work to make your control operate as a user would expect, though.
Henry Minute 31-Mar-11 19:07pm    
With an Owner-Draw ListBox you can paint the lines any colour you want.
Sergey Alexandrovich Kryukov 1-Apr-11 0:36am    
Exactly, Henry. I just commented on that.
Where it's really easy and effective to implement is WPF. (See my Answer.)
--SA
See my comments to the Answer by Bob.

Here is a good alternative: use WPF.

You could simply use System.Windows.Controls.ListBox. Much more flexible content model of WPF (see) allows you to put almost anything as an item if the list box. Also, WPF is free from Windows legacy and inherited problems such as flicker and poor performance of over-populated controls.

—SA
 
Share this answer
 
Comments
programmer1234 31-Mar-11 17:05pm    
I tried using that by I still get more errors in my code, its says "Expected class" for the voids and strings?
Sergey Alexandrovich Kryukov 31-Mar-11 17:35pm    
Really, you tried WPF? Sure?
--SA
programmer1234 31-Mar-11 17:36pm    
Yeah I used the WPF User Control Library.
Sergey Alexandrovich Kryukov 1-Apr-11 0:27am    
I don't know how you used it and what's "not working". Well, show your code.
--SA
BobJanova 31-Mar-11 18:09pm    
Is it possible to mix WPF and Windows Forms? I thought it was, at least, a pain to do so.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900