设置c#中listbox的每项itemheight的高度

本文介绍如何在C#中自定义ListBox控件的每一项item的高度。通过覆盖DrawItem和MeasureItem事件,可以实现对ListBox项高度的个性化设置。示例代码展示了如何在Form加载时和绘制项时调整高度。
摘要由CSDN通过智能技术生成
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;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


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


        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();


            让文字位于Item的中间
            //float difH = (e.Bounds.Height - e.Font.Height) / 2;
            //RectangleF rf = new Rectangl
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C#,可以使用多种方法设置ListBox的行数。以下是三种常用的方法: 方法1:添加记录后,选择最后一条记录,让滚动条滚动到底部,再自动取消选择: ```csharp listBox1.Items.Add("a good day"); listBox1.SelectedIndex = listBox1.Items.Count - 1; listBox1.SelectedIndex = -1; //取消选择行 ``` 方法2:通过计算显示行数,设置TopIndex属性,实现滚动目的: ```csharp listBox1.Items.Add("a good day"); listBox1.TopIndex = listBox1.Items.Count - (listBox1.Height / listBox1.ItemHeight); ``` 方法3:先计算滚动条是否在底部,然后添加记录,根据需要确定是否自动滚动: ```csharp bool scroll = false; if (listBox1.TopIndex == listBox1.Items.Count - (listBox1.Height / listBox1.ItemHeight)) scroll = true; listBox1.Items.Add("a good day"); if (scroll) listBox1.TopIndex = listBox1.Items.Count - (listBox1.Height / listBox1.ItemHeight); ``` 以上是三种常用的方法,你可以根据需要选择其一种来设置ListBox的行数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C# ListBox自动滚动方法](https://blog.csdn.net/qq_27474555/article/details/132141212)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值