自定义列表框控件不起作用? [英] Custom listbox control not working?

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

问题描述

我正在制作一个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 //Read only 
        {
            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 which i''ve put my control in:

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

            }

}

推荐答案

如果您告诉我们lbxColour是什么类型,您会认为很明显吗?
请告诉我们lbxColour的声明方式.

问候
If you tell us what type lbxColour is it would be pretty obvious don''t you think?
Please show us how lbxColour is declared.

Regards,


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

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