用TextBox 实现 ListView 的编辑功能

dofly 2004-09-30 03:51:34
想实现这么一个功能:
当单击 ListView 的时候,TextBox 放到 ListView 上;
当TextBox失去焦点的时候,TextBox的值放到 ListView 上。

请大家指导一下。
...全文
405 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
dofly 2004-10-03
  • 打赏
  • 举报
回复
不知道你加行首加图标没有,听说行高也受图标的影响
dofly 2004-10-03
  • 打赏
  • 举报
回复
F14 : *17 这段理解,如果有滚动条的时候,你考虑了吗?

我那个 TextBox 的 Top 值总是不正确,真奇怪,你有 MSN吗?交流一下: Mis_hid@hotmail.com
dofly 2004-10-03
  • 打赏
  • 举报
回复
谢谢你。辛苦了。

关于滚动条的问题我正在考虑。。。
F14 2004-10-03
  • 打赏
  • 举报
回复
To: dofly(巴山夜雨)
不好意思,程序没有优化,我是在英文环境下测试的,主要调整TextBox_Comment.Location = New Point(ColumnsWidth + 2, ListView_Info.Items.IndexOf(ListView_Info.SelectedItems(0)) * 17 + 20)中的17和20的值,17使listview每行的高度,20是listview的标题栏的高度。
F14 2004-10-03
  • 打赏
  • 举报
回复
但出现个新问题,:(
出现textbox时,滑动listview的滚动条textbox不失去焦点。
F14 2004-10-03
  • 打赏
  • 举报
回复
不好意思,太匆忙了,忘了最小值的情况,这次应该对了。
Public LVHeaderHeight As Integer
Public LVItemHeight As Integer

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox_Comment.Font = ListView_Info.Font
LVHeaderHeight = TextBox_Comment.Height + 7
LVItemHeight = TextBox_Comment.Height + 2
If LVItemHeight < 17 Then LVItemHeight = 17
TextBox_Comment.AutoSize = False
TextBox_Comment.Height = LVItemHeight - 1
End Sub

Private Sub ListView_Info_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView_Info.DoubleClick
Dim i As Integer = 0
Dim ColumnsWidth As Long = 0

While i < ListView_Info.Columns.Count - 1 And ListView_Info.Columns(i).Text <> "Comment" 'Seek the "Comment" column
ColumnsWidth += ListView_Info.Columns(i).Width
i += 1
End While
If ListView_Info.SelectedItems.Count < 1 Then Exit Sub 'Exit if no items in listview
TextBox_Comment.Location = New Point(ColumnsWidth + 2, (ListView_Info.Items.IndexOf(ListView_Info.SelectedItems(0)) - ListView_Info.TopItem.Index) * LVItemHeight + LVHeaderHeight)
TextBox_Comment.Text = ListView_Info.SelectedItems(0).SubItems(i).Text
TextBox_Comment.Width = ListView_Info.Columns(i).Width - 1
TextBox_Comment.Visible = True
TextBox_Comment.Focus()
TextBox_Comment.SelectAll()
End Sub
F14 2004-10-03
  • 打赏
  • 举报
回复
高度的问题解决了,先要改一下listview的click事件
如下:
Private Sub ListView_Info_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView_Info.DoubleClick
Dim i As Integer = 0
Dim ColumnsWidth As Long = 0

While i < ListView_Info.Columns.Count - 1 And ListView_Info.Columns(i).Text <> "Comment" 'Seek the "Comment" column
ColumnsWidth += ListView_Info.Columns(i).Width
i += 1
End While
If ListView_Info.SelectedItems.Count < 1 Then Exit Sub 'Exit if no items in listview
TextBox_Comment.Location = New Point(ColumnsWidth + 2, (ListView_Info.Items.IndexOf(ListView_Info.SelectedItems(0)) - ListView_Info.TopItem.Index) * (TextBox_Comment.Height + 2) + TextBox_Comment.Height + 8)
TextBox_Comment.Text = ListView_Info.SelectedItems(0).SubItems(i).Text
TextBox_Comment.Width = ListView_Info.Columns(i).Width - 1
TextBox_Comment.Visible = True
TextBox_Comment.Focus()
TextBox_Comment.SelectAll()
End Sub

在再form的load事件中增加
TextBox_Comment.Font = ListView_Info.Font
Dim h As Integer = TextBox_Comment.Height '这个h变量一定要,详细原因我们在msn上可以谈谈
If h < 15 Then h = 15
TextBox_Comment.AutoSize = False
TextBox_Comment.Height = h

我的textbox控件BoderStyle=None,初始化AutoSize=True
cancersyf 2004-10-03
  • 打赏
  • 举报
回复
http://dev.csdn.net/develop/article/21/21801.shtm
http://dev.csdn.net/develop/article/39/39153.shtm
ysf0816 2004-10-03
  • 打赏
  • 举报
回复
我这儿有源代码,用VB.NET写的,要的写明邮箱
F14 2004-10-03
  • 打赏
  • 举报
回复
关于滚屏(有滚动条的时候)这样可以解决
TextBox_Comment.Location = New Point(ColumnsWidth + 2, (ListView_Info.Items.IndexOf(ListView_Info.SelectedItems(0)) - ListView_Info.TopItem.Index) * 17 + 20)

ListView_Info.TopItem.Index是显示的这一屏的第一行的index值
F14 2004-10-03
  • 打赏
  • 举报
回复
dofly(巴山夜雨):
我加了你的MSN,你不在线,这几天放假,作息时间有点乱:)
很想和你继续讨论一下这个问题,因为我的程序也要用这个功能,你说的两个情况我也想到了,正在解决中,有点苦恼,共同努力吧
我加了行首图标的16*16,所以行高最小17
F14 2004-10-02
  • 打赏
  • 举报
回复
你们看看这段可不可以用,我的程序里面用的,我是用来修改“Comment”这个列。你把双击事件改为单击就行了。


Private Sub ListView_Info_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView_Info.DoubleClick
Dim i As Integer = 0
Dim ColumnsWidth As Long = 0

While i < ListView_Info.Columns.Count - 1 And ListView_Info.Columns(i).Text <> "Comment" 'Seek the "Comment" column
ColumnsWidth += ListView_Info.Columns(i).Width
i += 1
End While
If ListView_Info.SelectedItems.Count < 1 Then Exit Sub 'Exit if no items in listview
TextBox_Comment.Location = New Point(ColumnsWidth + 2, ListView_Info.Items.IndexOf(ListView_Info.SelectedItems(0)) * 17 + 20)
TextBox_Comment.Text = ListView_Info.SelectedItems(0).SubItems(i).Text
TextBox_Comment.Width = ListView_Info.Columns(i).Width - 1
TextBox_Comment.Visible = True
TextBox_Comment.Focus()
TextBox_Comment.SelectAll()
End Sub

Private Sub TextBox_Comment_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_Comment.LostFocus
Dim i As Integer = 0
Dim ColumnsWidth As Long = 0

While i < ListView_Info.Columns.Count - 1 And ListView_Info.Columns(i).Text <> "Comment" 'Seek the "Comment" column
ColumnsWidth += ListView_Info.Columns(i).Width
i += 1
End While
TextBox_Comment.Visible = False
ListView_Info.SelectedItems(0).SubItems(i).Text = TextBox_Comment.Text
End Sub

Private Sub TextBox_Comment_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox_Comment.KeyDown
If e.KeyCode = Keys.Return Then ListView_Info.Focus()
End Sub
dofly 2004-10-02
  • 打赏
  • 举报
回复
谢谢大家的支持!
今天下午有测试大家提供的方法。
Johnczy 的转换不成功。

F14 : TextBox 的 Top 值,为什么总是不标准呢,一般都是在低于要求的高度这是怎么回事,
是不是受 ListView 的行高和字体影响了,你那都设的多少
johnczy 2004-10-02
  • 打赏
  • 举报
回复
C# --> VB
http://www.kamalpatel.net/ConvertCSharp2VB.aspx
lxcc 2004-10-01
  • 打赏
  • 举报
回复
mark一下,有空那两个大侠写的C#改成VB.NET :P

其实原理有了,改起来应该不费劲!
dofly 2004-10-01
  • 打赏
  • 举报
回复
lxcc(虫子|需要点勇气和信心)提供的不是VB.NET的,还有没有办法呀。

加到一百分了,有答案马上结贴。
leezjs 2004-09-30
  • 打赏
  • 举报
回复
up 吧
没有想法
lxcc 2004-09-30
  • 打赏
  • 举报
回复
http://dev.csdn.net/develop/article/21/21801.shtm
http://dev.csdn.net/develop/article/39/39153.shtm

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧