Search code examples
vb.netcomboboxvisual-studio-2019richtextboxselectedindexchanged

ifselectedindex changed , show data in a textbox


I've linked an access database to my form. I have 1 table , 2 rows 1 = Researchtype short text 2 = Researchdetails (long text)

In my combobox1 i've binded my researchtype row so i can choose a type of research.

Question now: how can i bind the details data to the richtextbox below it in order to show the research data as soon as i choose a research type?

I've tried if else combos, try catch combos, i'm thinking i'm actually overthinking the issue here.

What would be the easiest way to "select from dropdown" and show the result in textbox. I'm a vb.net beginner

Public Class Onderzoeken
    Private Sub Onderzoeken_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PatientenDatabaseDataSetX.tbl_OnderzoeksTypes' table. You can move, or remove it, as needed.
        Me.Tbl_OnderzoeksTypesTableAdapter.Fill(Me.PatientenDatabaseDataSetX.tbl_OnderzoeksTypes)
    End Sub

    Private Sub cboxOnderzoek_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboxOnderzoek.SelectedIndexChanged
        If cboxOnderzoek.SelectedItem = Nothing Then

            cboxOnderzoek.Text = ""
        Else
            rtbBeschrijvingOnderzoek.Text = CStr(CType(cboxOnderzoek.SelectedItem, DataRowView)("OZ_Onderzoeksbeschrijving"))

        End If

    End Sub
End Class

I added the entire code of that page now , it's not much, but as stated: I added the binding source and displaymember "researchtype" to the combobox. So when i start the form, i can choose a type of research. Now i need to show the description of the research in the richtextbox


Solution

  • In the Form.Load...

    I have a function that returns a DataTable that contains columns called Name and Type. I bind the ComboBox to the DataTable and set the DisplayMember to "Name". Each Item in the ComboBox contains the entire DataRowView. I set the TextBox to the first row (dt(0)("Type")) Type column value so the correct information will be displayed for the initial selection.

    I put the code to change the textbox display in ComboBox1.SelectionChangeCommitted because the other change events will produce a NRE since .SelectedItem has not yet been set when the form loads. The commited event will only occur when the user makes a selection.

    First, cast the SelectedItem to its underlying type, DataRowView. Then you want the value of the Type column. This value is assigned to the text property of the textbox.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dt = LoadCoffeeTable()
        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "Name"
        TextBox1.Text = dt(0)("Type").ToString
    End Sub
    
    Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
        TextBox1.Text = DirectCast(ComboBox1.SelectedItem, DataRowView)("Type").ToString
    End Sub
    

    Just substitute Researchtype for Name and Researchdetails for Type.