列表框自定义控件不起作用 [英] Listbox custom control not working

查看:99
本文介绍了列表框自定义控件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Windows形式的电影和娱乐系统,允许用户选择电影.我制作了一个自定义列表框控件,该控件显示电影列表,并且当用户选择电影时,列表框中的项目会更改颜色.我将此控件放在Windows窗体中,使代码无效,它说尚未定义Item和Selected Item?

这是我用于自定义控件的代码:

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);        }    
}
}



这是Windows窗体的代码:



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");            }
}.

推荐答案

如果您从UserControl继承,则需要自己做所有事情,包括管理列表框中的项目列表.例如,这是我的控件,用于解决类似的问题.
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.


请参阅我对答案"的评论鲍勃.

这是一个很好的选择:使用WPF.

您可以简单地使用System.Windows.Controls.ListBox. WPF的更灵活的内容模型(请参见)允许您将几乎所有内容(如果是列表框)都放置为项​​目.此外,WPF不受Windows遗留问题的影响,并且继承了诸如闪烁和过度填充的控件的较差性能等问题.
—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


这篇关于列表框自定义控件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆