标题:可编辑的ListBox,是C#写的,谁帮我看看,怎么改一下,谢谢!
只看楼主
single_yu
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-3-15
结帖率:0
已结贴  问题点数:5 回复次数:2 
可编辑的ListBox,是C#写的,谁帮我看看,怎么改一下,谢谢!
看到一个帖子,我正好用到,但是不会C#,不知道怎么搞,谁帮我看一下,用怎么操作,我写了一个自定义控件,继承ListBox,但是没有实现最后一步,如果字体变大的话,就会被遮住了,选项宽度不会变!

下面把C#代码贴上来
正常情况下,.Net自带的ListBox是无法进行编辑的,欲让它可编辑,我们可添加一个隐藏的文本编辑框,当双击项的时候显示编辑框,更新项内容后隐藏编辑框即可。
欲调整ListBox每个项的高度,ListBox有一个自带属性ItemHeight,于是我们很自然的就设这个属性值,结果却发现改不动,改后又返回默认值12,后面才发现要使该属性值有效更改,必须将DrawMode属性设为OwnerDrawFixed,并且为ListBox添加DrawItem事件:
程序代码:
   private void listBox1_DrawItem( object sender , DrawItemEventArgs e )
        {
            e.DrawBackground( );
            e.DrawFocusRectangle( );
            e.Graphics.DrawString( listBox1.Items[e.Index].ToString( ) , e.Font , new SolidBrush( e.ForeColor ) , e.Bounds );
        }

经过这样的控制后,设计界面中的ListBox项看不到了,但没关系,运行的时候是可见的,而且每个项的高度确实作了有效更改。
接下来我们为ListBox再添几个事件来控制编辑框的显示,其实就两个:DoubleClick和MouseClick。好了,在添加之前我们在全局区定义一个编辑框txtEdit,并在构造函数为其添加KeyDown事件:
程序代码:
  TextBox txtEdit = new TextBox( );
        /// <summary>
        /// 构造函数
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            txtEdit.KeyDown += new KeyEventHandler( txtEdit_KeyDown );
        }
        /// <summary>
        /// KeyDown事件定义
        /// </summary>
        private void txtEdit_KeyDown( object sender , KeyEventArgs e )
        {
            //Enter键 更新项并隐藏编辑框
            if ( e.KeyCode == Keys.Enter )  
            {
                listBox1.Items[listBox1.SelectedIndex] = txtEdit.Text;
                txtEdit.Visible = false;
            }
            //Esc键 直接隐藏编辑框
            if ( e.KeyCode == Keys.Escape )   
                txtEdit.Visible = false;
        }
        /// <summary>
        /// 双击项时显示编辑框
        /// </summary>
        private void listBox1_DoubleClick( object sender , EventArgs e )
        {
            int itemSelected = listBox1.SelectedIndex;
            string itemText = listBox1.Items[itemSelected].ToString( );

            Rectangle rect = listBox1.GetItemRectangle( itemSelected );
            txtEdit.Parent = listBox1;
            txtEdit.Bounds = rect;
            txtEdit.Multiline = true;
            txtEdit.Visible = true;
            txtEdit.Text = itemText;
            txtEdit.Focus( );
            txtEdit.SelectAll( );
        }
        /// <summary>
        /// 点击其它项 隐藏编辑框
        /// </summary>
        private void listBox1_MouseClick( object sender , MouseEventArgs e )
        {
            txtEdit.Visible = false;
        }



[ 本帖最后由 single_yu 于 2015-2-5 01:00 编辑 ]
2015-02-05 00:41
jianjunfeng
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:42
专家分:166
注 册:2009-3-13
得分:3 
回复 楼主 single_yu
Public Class Form1
    Public WithEvents txtEdit As New TextBox
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.Items.Add("aa")
        Me.ListBox1.Items.Add("bb")
        Me.ListBox1.Items.Add("cc")
        Me.ListBox1.Items.Add("dd")
        Me.ListBox1.DrawMode = DrawMode.OwnerDrawFixed
        Me.ListBox1.ItemHeight = 25
        AddHandler txtEdit.KeyDown, AddressOf txtEdit_KeyDown
    End Sub
    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        Dim itemSelected As Integer = ListBox1.SelectedIndex
        Dim itemText As String = ListBox1.Items(Me.ListBox1.SelectedIndex).ToString
        Dim rect As Rectangle = ListBox1.GetItemRectangle(itemSelected)

        txtEdit.Parent = ListBox1
        txtEdit.Bounds = rect
        txtEdit.Multiline = True
        txtEdit.Visible = True
        txtEdit.Text = itemText
        txtEdit.Focus()
        txtEdit.SelectAll()
    End Sub
    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        e.DrawBackground()
        e.DrawFocusRectangle()
        e.Graphics.DrawString(Me.ListBox1.Items(e.Index).ToString, e.Font, New SolidBrush(e.ForeColor), e.Bounds)
    End Sub
    Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick
        txtEdit.Visible = False
    End Sub
    Private Sub txtEdit_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtEdit.KeyDown

        ' //Enter键 更新项并隐藏编辑框
        If e.KeyCode = Keys.Enter Then

            ListBox1.Items(ListBox1.SelectedIndex) = txtEdit.Text
            txtEdit.Visible = False
        End If

        ' //Esc键 直接隐藏编辑框
        If e.KeyCode = Keys.Escape Then
            txtEdit.Visible = False
        End If
    End Sub
   
End Class

[ 本帖最后由 jianjunfeng 于 2015-2-10 19:25 编辑 ]
2015-02-10 14:12
厨师王德榜
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:183
帖 子:942
专家分:4724
注 册:2013-2-16
得分:3 
楼上的改的,应该可以用.
但是,做项目的,即使你不用C#,但其它语言也应该知道一点,不必精通,但是基本的语法,多少应该掌握一点吧.
2015-02-13 16:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-441778-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.102214 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved