c#.net中创建带图标的ListBox

下面的代码实现了带图标的ListBoox的功能,可以直接拷贝即可运行。运行结果如下:

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

namespace ListBoxWithIcon
{
  /// <summary>
  /// Form1 的摘要说明。
  /// </summary>
  public class Form1 : System.Windows.Forms.Form
  {
    private GListBox lb;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
      //
      // Windows 窗体设计器支持所必需的
      //
      InitializeComponent();

      //
      // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
      //
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
      ImageList imageList = new ImageList();
      imageList.Images.Add(Image.FromFile(@"D:\我的文件\myrice\favicon.ico"));
      imageList.Images.Add(Image.FromFile(@"D:\我的文件\myrice\favicon4.ico"));
      imageList.Images.Add(Image.FromFile(@"D:\我的文件\myrice\favicon5.ico"));

      this.lb = new GListBox();;
      this.SuspendLayout();
      //
      // lb
      //
      this.lb.ItemHeight = 16;
      this.lb.Location = new System.Drawing.Point(10, 10);
      this.lb.Name = "listBox1";
      this.lb.Size = new System.Drawing.Size(200, 60);
      this.lb.TabIndex = 0;

      lb.ImageList = imageList;
      lb.Items.Add( new GListBoxItem("http://xml.sz.luohuedu.net/",0));
      lb.Items.Add( new GListBoxItem("http://lucky.myrice.com/",1));
      lb.Items.Add( new GListBoxItem("【孟宪会之精彩世界】",2));

      //
      // Form1
      //
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
      this.ClientSize = new System.Drawing.Size(292, 160);
      this.Controls.Add(this.lb);
      this.Text = "带图标的ListBox测试";
      this.Load += new System.EventHandler(this.Form1_Load);
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
    }

    // GListBoxItem 类
    public class GListBoxItem
    {
      private string _myText;
      private int _myImageIndex;
      // 属性
      public string Text
      {
        get {return _myText;}
        set {_myText = value;}
      }
      public int ImageIndex
      {
        get {return _myImageIndex;}
        set {_myImageIndex = value;}
      }
      //构造函数
      public GListBoxItem(string text, int index)
      {
        _myText = text;
        _myImageIndex = index;
      }
      public GListBoxItem(string text): this(text,-1){}
      public GListBoxItem(): this(""){}
      public override string ToString()
      {
        return _myText;
      }
    }

    // GListBox 类
    public class GListBox : ListBox
    {
      private ImageList _myImageList;
      public ImageList ImageList
      {
        get {return _myImageList;}
        set {_myImageList = value;}
      }
      public GListBox()
      {
        this.DrawMode = DrawMode.OwnerDrawFixed;
      }
      protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
      {
        e.DrawBackground();
        e.DrawFocusRectangle();
        GListBoxItem item;
        Rectangle bounds = e.Bounds;
        Size imageSize = _myImageList.ImageSize;
        try
        {
          item = (GListBoxItem) Items[e.Index];
          if (item.ImageIndex != -1)
          {
            ImageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex);
            e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
              bounds.Left+imageSize.Width, bounds.Top);
          }
          else
          {
            e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor),
              bounds.Left, bounds.Top);
          }
        }
        catch
        {
          if (e.Index != -1)
          {
            e.Graphics.DrawString(Items[e.Index].ToString(),e.Font,
              new SolidBrush(e.ForeColor) ,bounds.Left, bounds.Top);
          }
          else
          {
            e.Graphics.DrawString(Text,e.Font,new SolidBrush(e.ForeColor),
              bounds.Left, bounds.Top);
          }
        }
        base.OnDrawItem(e);
      }
    }
  }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现: 1. 在窗体添加一个 RichTextBox 和一个 ListBox 控件。 2. 在 RichTextBox 的 TextChanged 事件,获取当前光标所在行的文本,并根据该文本获取匹配的代码提示列表。 3. 将匹配的提示项添加到 ListBox ,并显示在 RichTextBox 下方。 4. 监听 ListBox 的 SelectedIndexChanged 事件,当选一个提示项时,将其插入到 RichTextBox 。 以下是一个简单的示例代码: ``` private void richTextBox1_TextChanged(object sender, EventArgs e) { // 获取当前光标所在行的文本 int index = richTextBox1.SelectionStart; int line = richTextBox1.GetLineFromCharIndex(index); string text = richTextBox1.Lines[line]; // 根据文本获取匹配的代码提示列表 List<string> suggestions = GetSuggestions(text); // 清空 ListBox,并添加提示项 listBox1.Items.Clear(); listBox1.Items.AddRange(suggestions.ToArray()); // 显示 ListBox if (suggestions.Count > 0) { listBox1.Visible = true; listBox1.Width = richTextBox1.Width; listBox1.Top = richTextBox1.Top + (line + 1) * richTextBox1.Font.Height; listBox1.Left = richTextBox1.Left; } else { listBox1.Visible = false; } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { // 将选的提示项插入到 RichTextBox int index = richTextBox1.SelectionStart; int line = richTextBox1.GetLineFromCharIndex(index); int start = richTextBox1.GetFirstCharIndexFromLine(line); int length = index - start; string suggestion = listBox1.SelectedItem.ToString(); richTextBox1.SelectionStart = start; richTextBox1.SelectionLength = length; richTextBox1.SelectedText = suggestion; listBox1.Visible = false; } private List<string> GetSuggestions(string text) { // 根据文本获取匹配的代码提示列表 List<string> suggestions = new List<string>(); if (text.StartsWith("using ")) { suggestions.Add("System"); suggestions.Add("System.Collections"); suggestions.Add("System.IO"); suggestions.Add("System.Linq"); suggestions.Add("System.Text"); } else if (text.StartsWith("Console.")) { suggestions.Add("Write"); suggestions.Add("WriteLine"); suggestions.Add("Read"); suggestions.Add("ReadLine"); } return suggestions; } ``` 在 GetSuggestions 方法,可以根据实际需求生成代码提示列表,例如从一个 XML 文件读取提示项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值