请教各位大神。。。。如何在VB中实时改写text中的内容

lixrnet01 2017-01-06 11:20:07
小弟最近在编写一个软件,用于单位的业务统计,卡在一个地方很久了,也百度了许久始终没找到合适的解决方案……
具体为:
(假如)有一个TEXT控件,有一个listbox控件,目的是将点选的listbox中的list内容(比如“list1.list(0)的内容是A,list1.list(1)的内容是B,list1.list(2)的内容是C”),显示到TEXT中,
而当不点选时,就将TEXT中的相关内容删除。list内容有几十项,如何实现内容在TEXT上的实时改写?


我写了一段代码,发现不能实现实时改写,一团糟,就是前次点选的内容都还在,造成了点第1个TEXT中显示A,然后点第2个、TEXT显示AAB,、再点第3个TEXT就显示AABABC……,而目标是,list中点选了几项就在TEXT中显示几项

If List1.Selected(0) = True Then
Text1.Text = Text1.Text & List1.List(0)
End If
If List1.Selected(1) = True Then
Text1.Text = Text1.Text & List1.List(1)
End If
If List1.Selected(2) = True Then
Text1.Text = Text1.Text & List1.List(2)
End If

不知道各位大神有什么好的建议?
...全文
534 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2017-01-16
  • 打赏
  • 举报
回复
Private Sub List1_Click()
Dim i As Integer

    Text1 = ""
    For i = 0 To List1.ListCount - 1
        If List1.Selected(i) Then Text1 = Text1 & List1.List(i)
    Next i
End Sub
  • 打赏
  • 举报
回复
这个简单啊啊哼哼唧唧
vansoft 2017-01-06
  • 打赏
  • 举报
回复
Text1.Text = List1.List(0) & List1.List(1) & List1.List(2)
lixrnet01 2017-01-06
  • 打赏
  • 举报
回复
引用 6 楼 Chen8013 的回复:
把“已经添加Text1”的列表项作上标记:
Private Sub List1_Click()
   Dim i As Long

   i = List1.ListIndex
   If (List1.ItemData(i) = 0) Then
      Text1.Text = Text1.Text & List1.List(i)
      List1.ItemData(i) = -1
   End If
End Sub
只是要注意,每次List1.clear 时,最好也把Text1清空。
嗯,后来选了个最原始的办法,弄好了: If List1.Selected(0) = True Then Text1.Text = Text1.Text & "," & List1.List(0) If Not List1.Selected(0) = True Then Text1.Text = Replace(Text1.Text, " List1.List(0)", "") End If End If 谢谢大神们!
舉杯邀明月 2017-01-06
  • 打赏
  • 举报
回复
把“已经添加Text1”的列表项作上标记:
Private Sub List1_Click()
   Dim i As Long

   i = List1.ListIndex
   If (List1.ItemData(i) = 0) Then
      Text1.Text = Text1.Text & List1.List(i)
      List1.ItemData(i) = -1
   End If
End Sub
只是要注意,每次List1.clear 时,最好也把Text1清空。
笨狗先飞 2017-01-06
  • 打赏
  • 举报
回复

Private Sub Form_Load()
    List1.AddItem "AAA"
    List1.AddItem "BBB"
    List1.AddItem "CCC"
    List1.AddItem "DDD"
    List1.AddItem "EEE"
    List1.AddItem "FFF"
    List1.AddItem "GGG"
    List1.AddItem "HHH"
    List1.ListIndex = 0
End Sub

Private Sub List1_Click()
    Dim I As Integer
    Text1.Text = ""
    For I = 0 To List1.ListCount - 1
         If List1.Selected(I) Then Text1.Text = Text1.Text & IIf(Len(Text1.Text) = 0, "", ",") & List1.List(I)
    Next
End Sub

lixrnet01 2017-01-06
  • 打赏
  • 举报
回复
还有种尝试就是改写生成TXT后再读入:
Open "C:\1.txt" For Output As #1
T = Text1.Text
Print #1, T
Close #1
Text1.Text = ""
Open "C:\2.txt" For Input As #2
Input #1, str1
Close #1
Text1.Text = str1
lixrnet01 2017-01-06
  • 打赏
  • 举报
回复
也尝试使用Replace函数。
If List1.Selected(0) = True Then
Text1.Text = Text1.Text & List1.List(0)
else
Text1.Text =Replace(Text1.Text," List1.List(0)","")
End If
If List1.Selected(1) = True Then
Text1.Text = Text1.Text & List1.List(1)
else
Text1.Text =Replace(Text1.Text," List1.List(1)","")
End If
If List1.Selected(2) = True Then
Text1.Text = Text1.Text & List1.List(2)
else
Text1.Text =Replace(Text1.Text," List1.List(2)","")
End If


但是也失败了!!
lixrnet01 2017-01-06
  • 打赏
  • 举报
回复
引用 1 楼 vansoft 的回复:
Text1.Text = List1.List(0) & List1.List(1) & List1.List(2)
大神好!这个运算式的结果是:不管在list列表中选没选中均会在TEXT1中显示List1.List(1), List1.List(2),List1.List(3) 目标是:当选中list中的某一项时,则在TEXT1中显示,而不选中时,则“去除”(即误选后可取消显示)。。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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