none
ComboBox to Textbox Output RRS feed

  • Question

  • Hello,

         I am in the process of reteaching myself visual basic. To help assist myself in relearning it I am creating a template generator. So far I have a ComboBox with a drop down that when the object is selected it displays that same object in the TextBox. What I am trying to do is when I select an Item in the ComboBox I want it to output with a fillable template instead. Something that relevant info can then be added to before copy and paste to where I need it to go.  The issue I run into is I can not remember how to do that. I assume it is an If Then statement but I cant seem to figure that part out. 

    I am using Template as the name for my ComboBox

    The code I have been trying is 

    If Template.SelectedItem = 0 Then

    TextBox.Text = ("Case Subject:" & vbNewLine & "Case type to be chosen:" & vbNewLine & "Case bod:")

    The first item in my drop down is Recall. When I select it that is all it says in the TextBox. What I am trying to have it show would be

    Case Subject:

    Case type to be chosen: 

    Case Body:

    Thank you in advance for any assistance you could give in this.

    Wednesday, May 1, 2019 7:27 PM

Answers

  • Hi

    Maybe something like this:

    ' Form1 with ComboBox1 and 
    ' multiline TextBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With ComboBox1
                .Items.Add("Recall")
                .Items.Add("item2")
                .Items.Add("item3")
                .Items.Add("item4")
                .Items.Add("item5")
            End With
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            TextBox1.Clear()
    
            Select Case ComboBox1.SelectedItem.ToString
                Case "Recall"
                    TextBox1.AppendText("Case Subject:" & vbCrLf)
                    TextBox1.AppendText("Case type to be chosen: " & vbCrLf)
                    TextBox1.AppendText("Case Body:" & vbCrLf)
                Case "item2"
                    TextBox1.AppendText("item2:" & vbCrLf)
                Case "item3"
                    TextBox1.AppendText("item3:" & vbCrLf)
                Case "item4"
                    TextBox1.AppendText("item4:" & vbCrLf)
                Case "item5"
                    TextBox1.AppendText("item5:" & vbCrLf)
            End Select
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Jriggs712 Wednesday, May 1, 2019 7:58 PM
    Wednesday, May 1, 2019 7:42 PM
  • Best to always not go by text but a key such as an integer e.g.

    Private bsData As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bsData = New BindingSource(
            New Dictionary(Of String, Integer) From {{"Recall", 0}, {"Subject", 1}, {"Body", 2}}, Nothing)
        ComboBox1.DataSource = bsData
        ComboBox1.DisplayMember = "Key"
        ComboBox1.ValueMember = "Value"
        AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    End Sub
    
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim test = ComboBox1.SelectedValue
        Select Case CInt(ComboBox1.SelectedValue)
    
            Case 0
                ' do something
            Case 1
                ' do something
            Case 2
                ' do something
        End Select
    End Sub

    To see it actually work

    Private bsData As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bsData = New BindingSource(
            New Dictionary(Of String, Integer) From {{"Recall", 0}, {"Subject", 1}, {"Body", 2}}, Nothing)
        ComboBox1.DataSource = bsData
        ComboBox1.DisplayMember = "Key"
        ComboBox1.ValueMember = "Value"
        AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    End Sub
    
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim test = ComboBox1.SelectedValue
        Select Case CInt(ComboBox1.SelectedValue)
    
            Case 0
                TextBox1.Text = "0"
            Case 1
                TextBox1.Text = "1"
            Case 2
                TextBox1.Text = "2"
        End Select
    End Sub

    Caveats, in real life I'd expected data not to be hard coded but it may be for you. A List(Of T) could be used rather than a dictionary e.g.

    Public Class DataItem
        Public Property DisplayName() As String
        Public Property Id() As Integer
    
        Public Overrides Function ToString() As String
            Return DisplayName
        End Function
    End Class

    Then

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dataList = New List(Of DataItem) From
                {
                    New DataItem() With {.DisplayName = "Recall", .Id = 0},
                    New DataItem() With {.DisplayName = "Subject", .Id = 1},
                    New DataItem() With {.DisplayName = "Body", .Id = 2}
                }
        ComboBox1.DataSource = dataList
        ComboBox1.DisplayMember = "DisplayName"
        ComboBox1.ValueMember = "id"
        AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    End Sub
    
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim test = ComboBox1.SelectedValue
        Select Case CInt(ComboBox1.SelectedValue)
    
            Case 0
                TextBox1.Text = "0"
            Case 1
                TextBox1.Text = "1"
            Case 2
                TextBox1.Text = "2"
        End Select
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jriggs712 Wednesday, May 1, 2019 8:05 PM
    Wednesday, May 1, 2019 7:59 PM
    Moderator

All replies

  • Hi

    Maybe something like this:

    ' Form1 with ComboBox1 and 
    ' multiline TextBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With ComboBox1
                .Items.Add("Recall")
                .Items.Add("item2")
                .Items.Add("item3")
                .Items.Add("item4")
                .Items.Add("item5")
            End With
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            TextBox1.Clear()
    
            Select Case ComboBox1.SelectedItem.ToString
                Case "Recall"
                    TextBox1.AppendText("Case Subject:" & vbCrLf)
                    TextBox1.AppendText("Case type to be chosen: " & vbCrLf)
                    TextBox1.AppendText("Case Body:" & vbCrLf)
                Case "item2"
                    TextBox1.AppendText("item2:" & vbCrLf)
                Case "item3"
                    TextBox1.AppendText("item3:" & vbCrLf)
                Case "item4"
                    TextBox1.AppendText("item4:" & vbCrLf)
                Case "item5"
                    TextBox1.AppendText("item5:" & vbCrLf)
            End Select
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Jriggs712 Wednesday, May 1, 2019 7:58 PM
    Wednesday, May 1, 2019 7:42 PM
  • Les

         Thank You. I will see if that fixes the issue I am having. I do appreciate your response. It is lots of fun to relearn as I go.

    Wednesday, May 1, 2019 7:52 PM
  • Best to always not go by text but a key such as an integer e.g.

    Private bsData As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bsData = New BindingSource(
            New Dictionary(Of String, Integer) From {{"Recall", 0}, {"Subject", 1}, {"Body", 2}}, Nothing)
        ComboBox1.DataSource = bsData
        ComboBox1.DisplayMember = "Key"
        ComboBox1.ValueMember = "Value"
        AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    End Sub
    
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim test = ComboBox1.SelectedValue
        Select Case CInt(ComboBox1.SelectedValue)
    
            Case 0
                ' do something
            Case 1
                ' do something
            Case 2
                ' do something
        End Select
    End Sub

    To see it actually work

    Private bsData As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bsData = New BindingSource(
            New Dictionary(Of String, Integer) From {{"Recall", 0}, {"Subject", 1}, {"Body", 2}}, Nothing)
        ComboBox1.DataSource = bsData
        ComboBox1.DisplayMember = "Key"
        ComboBox1.ValueMember = "Value"
        AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    End Sub
    
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim test = ComboBox1.SelectedValue
        Select Case CInt(ComboBox1.SelectedValue)
    
            Case 0
                TextBox1.Text = "0"
            Case 1
                TextBox1.Text = "1"
            Case 2
                TextBox1.Text = "2"
        End Select
    End Sub

    Caveats, in real life I'd expected data not to be hard coded but it may be for you. A List(Of T) could be used rather than a dictionary e.g.

    Public Class DataItem
        Public Property DisplayName() As String
        Public Property Id() As Integer
    
        Public Overrides Function ToString() As String
            Return DisplayName
        End Function
    End Class

    Then

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dataList = New List(Of DataItem) From
                {
                    New DataItem() With {.DisplayName = "Recall", .Id = 0},
                    New DataItem() With {.DisplayName = "Subject", .Id = 1},
                    New DataItem() With {.DisplayName = "Body", .Id = 2}
                }
        ComboBox1.DataSource = dataList
        ComboBox1.DisplayMember = "DisplayName"
        ComboBox1.ValueMember = "id"
        AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    End Sub
    
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim test = ComboBox1.SelectedValue
        Select Case CInt(ComboBox1.SelectedValue)
    
            Case 0
                TextBox1.Text = "0"
            Case 1
                TextBox1.Text = "1"
            Case 2
                TextBox1.Text = "2"
        End Select
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jriggs712 Wednesday, May 1, 2019 8:05 PM
    Wednesday, May 1, 2019 7:59 PM
    Moderator
  • Les,

         After trying it out it works perfect. I do appreciate your help on this. It did exactly what I could not remember. Based on that I think I can understand and do more. 

    Jeremy

    Wednesday, May 1, 2019 8:02 PM
  • Karen,

    Thank You. So if I understand your comments correct it is better to have an external source such as excel or SQL to pull from rather than to code in the list? I hard code the list in just because I am self taught and self reteaching. Any advice helps a lot. I really appreciate it.

    Jeremy

    Wednesday, May 1, 2019 8:07 PM
  • Karen,

    Thank You. So if I understand your comments correct it is better to have an external source such as excel or SQL to pull from rather than to code in the list? I hard code the list in just because I am self taught and self reteaching. Any advice helps a lot. I really appreciate it.

    Jeremy

    Your welcome, I created a learning project for you which loads the ComboBox from a comma delimited text file. I included a "select" item and include more data.

    You can download it here. Only requirement is you need to target Framework 3.5 or higher. I write it in VS2017.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 1, 2019 10:59 PM
    Moderator
  • Thank You,

    I am always happy to learn more. I am working with 2019 so should be good. I followed that link but i did not see anything available.

    Friday, May 3, 2019 1:02 PM
  • Thank You Karen. I went back in today and found that file. I do appreciate all the knowledge in here. it helps to relearn all this. 
    Saturday, May 4, 2019 6:09 PM