vb中向combobox控件中永久添加item
DimComboListIndexAsString'声明ComboListIndex变量,用于将Combo1.ListIndex从注册表中读取出来,'以ComboList...
Dim ComboListIndex As String '声明ComboListIndex变量,用于将Combo1.ListIndex从注册表中读取出来, '以ComboListIndex变量为中间值,将值传递给combo1Dim s As String '当用户选择combo1中的“其他”时,允许用户输入一个字符串变量添加到combo1中Dim h As Integer '循环变量Private Sub Combo1_Click() Select Case Combo1.ListIndex Case 4 s = InputBox("", "请输入测试项目") If s <> "" Then Combo1.AddItem s Combo1.ListIndex = Combo1.ListCount - 1 End If End SelectEnd SubPrivate Sub Command1_Click() '保存按钮 SaveSetting "MyApp", "setup", "Form3.Text1", Text1.Text SaveSetting "MyApp", "setup", "Form3.Text2", Text2.Text SaveSetting "MyApp", "setup", "Form3.Text3", Text3.Text For h = 5 To Combo1.ListCount - 1 SaveSetting "MyApp", "setup", "Combo1.List(" & h & ")", Combo1.List(h) Next h SaveSetting "MyApp", "setup", "Form3.Combo1", Combo1.ListIndex Unload Form3End SubPrivate Sub Command2_Click() '取消按钮 Unload Form3End SubPrivate Sub Form_Load() Form3.Text1.Text = GetSetting("MyApp", "setup", "Form3.Text1", Text1.Text) Form3.Text2.Text = GetSetting("MyApp", "setup", "Form3.Text2", Text2.Text) Form3.Text3.Text = GetSetting("MyApp", "setup", "Form3.Text3", Text3.Text) ComboListIndex = GetSetting("MyApp", "setup", "Form3.Combo1", Combo1.ListIndex) For h = 5 To Combo1.ListCount - 1 Form3.Combo1.AddItem GetSetting("MyApp", "setup", "Combo1.List(" & h & ")", Combo1.List(h)) Next h Form3.Combo1.ListIndex = Val(ComboListIndex)End Sub各位大神高手,目前我的combo1这个控件中有5个item,最后一个item为“其他”。现在想实现功能就是当用户选“其他”的时候,会弹出inputbox,将用户输入的item名称添加到combo1里。现在我的问题是每次添加item是可以的,但我下一次运行程序时就没有用户自己添加的item,只有我写程序时添加的5个item。ps:如果我想实现用户添加item始终是在“其他”这个item前面,这个如何实现?
展开
2个回答
展开全部
Dim s As String '当用户选择combo1中的“其他”时,允许用户输入一个字符串变量添加到combo1中
Dim h As Integer '循环变量
Private Sub Combo1_Click()
If Combo1.ListIndex = Combo1.ListCount - 1 Then
s = InputBox("请输入测试项目")
If s <> "" Then
Combo1.AddItem s, Combo1.ListCount - 1
Combo1.ListIndex = Combo1.ListCount - 2
End If
End If
End Sub
Private Sub Command1_Click() '保存按钮
SaveSetting "MyApp", "setup", "Form3.Text1", Text1.Text
SaveSetting "MyApp", "setup", "Form3.Text2", Text2.Text
SaveSetting "MyApp", "setup", "Form3.Text3", Text3.Text
s = ""
For h = 4 To Combo1.ListCount - 2
If s <> "" Then s = s & "|"
s = s & Combo1.List(h)
Next h
If s <> "" Then SaveSetting "MyApp", "setup", "Combo1.List", s
SaveSetting "MyApp", "setup", "Form3.Combo1", Combo1.ListIndex
Unload Me
End Sub
Private Sub Command2_Click() '取消按钮
Unload Me
End Sub
Private Sub Form_Load()
Dim ss() As String
Text1.Text = GetSetting("MyApp", "setup", "Form3.Text1", Text1.Text)
Text2.Text = GetSetting("MyApp", "setup", "Form3.Text2", Text2.Text)
Text3.Text = GetSetting("MyApp", "setup", "Form3.Text3", Text3.Text)
s = GetSetting("MyApp", "setup", "Combo1.List", "")
If s <> "" Then
ss = Split(s, "|")
For h = 0 To UBound(ss)
Combo1.AddItem ss(h), Combo1.ListCount - 1
Next h
End If
Combo1.ListIndex = GetSetting("MyApp", "setup", "Form3.Combo1", Combo1.ListIndex)
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询