在VB.NET中,您可以使组合框仅由某些项目组成,例如不能在其中输入吗?

huangapple go评论67阅读模式
英文:

in vb.net can you make the combo box only composed of some items like you can't type in it?

问题

我正在开发一个机票预订系统的项目。我有一个提交按钮,它还充当某些输入数据(如选择的票数和电子邮件)的数据验证。我希望用户只能从组合框中选择一个项目,或者将一个项目设置为默认值,因为当我打开表单时,输入数据时,您仍然可以在组合框中写任何内容。

如果 airportlist.Text = "" Then
problems &= "未选择机场" & vbCrLf
Else
var_of_aeroport = airportlist.Text
End If

Private Sub airportlist_SelectedIndexChanged(sender As Object, e As EventArgs) Handles airportlist.SelectedIndexChanged
airportlist.DropDownStyle = ComboBoxStyle.DropDownList
End Sub

我尝试过这样做,但问题是当您尚未选择项目时。

英文:

I am working on a project of a ticket reservation system. I have a submission button that also works as data validation for some inputs like number of tickets chosen and email ,...
I want to make the user is only able to choose one of the items from a combo box, or make one item default because when I open the form while inputing data you can still write anything in the combo box

If airportlist.Text = "" Then
    problems &= "**Airport not selected**" & vbCrLf
Else
    var_of_aeroport = airportlist.Text
End If

Private Sub airportlist_SelectedIndexChanged(sender As Object, e As EventArgs) Handles airportlist.SelectedIndexChanged
    airportlist.DropDownStyle = ComboBoxStyle.DropDownList
End Sub

I tried this but the problem is when you haven't selected an item yet.

答案1

得分: 1

你可以在设计模式下设置ComboBox控件的DropDownStyle属性(当在窗体/用户控件上布置控件时)。

此外,在设计模式下,您可以通过编辑ComboBox的Items属性来填充列表中的项目。

最后,您可以使用ComboBox的SelectedIndex属性来预设要默认显示的项目。在我的窗体的Load事件中,我将ComboBox1的SelectedIndex属性设置为0(第一个项目):

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ComboBox1.SelectedIndex = 0
End Sub

当ComboBox显示时,第一个项目将被选中。

英文:

You can set the DropDownStyle property of your ComboBox control in Design mode (when laying out the controls on your form/user control).

Also, in Design mode, you can populate the items in the list by editing the Items property of the ComboBox.

Finally, you can use the ComboBox's SelectedIndex property to preset the item you want it to show by default. Here, in my Form's Load event, I set ComboBox1's SelectedIndex property to 0 (first item):

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ComboBox1.SelectedIndex = 0
End Sub

When the ComboBox is shown, the first item is selected.

答案2

得分: 0

这个答案可能比你所期望的要多一些,但我希望它提供了一些学习帮助。以下是代码部分的翻译:

这是用于在单击Save New Link时将数据添加到数据库的代码

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    If cbType.Items.Contains(cbType.Text) Then
        ' ComboBox 包含匹配项
    Else
        gvalertType = "15"
        frmAlert.ShowDialog()
        cbType.Text = ""
        cbType.DroppedDown = True
        cbType.Focus()
        Return
    End If

    If tbSiteName.Text = "" Then
        gvalertType = "2"
        frmAlert.ShowDialog()
        tbSiteName.Focus()
        Return
    End If

    If strType = "" Then
        gvalertType = "1"
        frmAlert.ShowDialog()
        cbType.Text = ""
        cbType.DroppedDown = True
        cbType.Focus()
        Return
    End If

    If tbUrl.Text = "" Then
        gvalertType = "3"
        frmAlert.ShowDialog()
        tbUrl.Focus()
        Return
    End If
End Sub

以下是frmAlert中的所有代码的翻译:

Public Class frmAlert

    Private Sub frmAlert_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If gvalertType = "1" Then
            tbAlert.Text = vbCrLf & "选择一个站点类型"
        ElseIf gvalertType = "2" Then
            tbAlert.Text = vbCrLf & "输入站点名称"
        ElseIf gvalertType = "3" Then
            tbAlert.Text = vbCrLf & "输入站点URL"
        ElseIf gvalertType = "4" Then
            tbAlert.Text = vbCrLf & "不要在此处单击" & vbCrLf & "点击站点URL"
        ElseIf gvalertType = "5" Then
            tbAlert.Text = vbCrLf & "单元格为空" & vbCrLf & "点击站点URL"
        ElseIf gvalertType = "6" Then
            tbAlert.Text = vbCrLf & "站点类型不能为空" & vbCrLf & "输入站点类型以保存新类型"
        ElseIf gvalertType = "7" Then
            tbAlert.Text = vbCrLf & "站点类型不能为空" & vbCrLf & "选择要删除的当前类型"
        ElseIf gvalertType = "8" Then
            tbAlert.Text = vbCrLf & "您正在创建重复项" & vbCrLf & "不允许重复项"
        ElseIf gvalertType = "9" Then
            tbAlert.Text = vbCrLf & "检查URL,它不符合规范" & vbCrLf & "URL格式不正确"
        ElseIf gvalertType = "15" Then
            tbAlert.Text = vbCrLf & "该站点类型不可用" & vbCrLf & "选择这些站点类型之一"
        End If
    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Close()
    End Sub
End Class

这是代码部分的翻译,没有包含其他内容。

英文:

This answer is a little more than you may be looking for but I hope it provides some learning Here is what the form looks like just consider the Save New Link
And understand I like my own Error Messages I call them Alerts and they are made with Show Dialog forms OK here is the form and the code If you need more help with the code flow feel free to expand your question
在VB.NET中,您可以使组合框仅由某些项目组成,例如不能在其中输入吗?

Here is the code for adding the data to the database when you click
Save New Link

 Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    If cbType.Items.Contains(cbType.Text) Then
        'Combobox Contains a Match
    Else
        gvalertType = "15"
        frmAlert.ShowDialog()
        cbType.Text = ""
        cbType.DroppedDown = True
        cbType.Focus()
        Return
    End If

    If tbSiteName.Text = "" Then
        gvalertType = "2"
        frmAlert.ShowDialog()
        tbSiteName.Focus()
        Return
    End If

    If strType = "" Then
        gvalertType = "1"
        frmAlert.ShowDialog()
        cbType.Text = ""
        cbType.DroppedDown = True
        cbType.Focus()
        Return
    End If

    If tbUrl.Text = "" Then
        gvalertType = "3"
        frmAlert.ShowDialog()
        tbUrl.Focus()
        Return
    End If

Here is all the code in the frmAlert

Public Class frmAlert

Private Sub frmAlert_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If gvalertType = "1" Then
        tbAlert.Text = vbCrLf & "Select A  Site Type"
    ElseIf gvalertType = "2" Then
        tbAlert.Text = vbCrLf & "Enter the Site Name"
    ElseIf gvalertType = "3" Then
        tbAlert.Text = vbCrLf & "Enter the Site URL"
    ElseIf gvalertType = "4" Then
        tbAlert.Text = vbCrLf & "No Clicking Here" & vbCrLf & "Click on Site Url "
    ElseIf gvalertType = "5" Then
        tbAlert.Text = vbCrLf & "Cell Is Empty" & vbCrLf & "Click on Site Url "
    ElseIf gvalertType = "6" Then
        tbAlert.Text = vbCrLf & "Site Type Can't be EMPTY" & vbCrLf & "Enter Site Type to Save New Type"
    ElseIf gvalertType = "7" Then
        tbAlert.Text = vbCrLf & "Site Type Can't be EMPTY" & vbCrLf & "Select a Current Type to Delete"
    ElseIf gvalertType = "8" Then
        tbAlert.Text = vbCrLf & "You are Creating DUPLICATES" & vbCrLf & "NO DUPLICATES"
    ElseIf gvalertType = "9" Then
        tbAlert.Text = vbCrLf & "Check the URL it is" & vbCrLf & "NOT Well Formed"
    ElseIf gvalertType = "15" Then
        tbAlert.Text = vbCrLf & "That Site Type NOT Available" & vbCrLf & "Select One of These Site Types"

    End If
End Sub

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
    Close()
End Sub

End Class

在VB.NET中,您可以使组合框仅由某些项目组成,例如不能在其中输入吗?

Here is what the Alert Form looks like

huangapple
  • 本文由 发表于 2023年5月18日 01:16:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定