-
Dec 21st, 2018, 04:52 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Passing a combobox as argument in a sub
Hi,
I perform same operation on different comboboxes. For that, I thought of a sub where I would pass the combobox as the parameter and later in the sub I would perform the operation on the argument.
Here is my code:
Code:
Call GetJFIMmodulesInCombo(CB_AssignJFIM)’ CB_AssignJFIM is a combobox
Sub GetJFIMmodulesInCombo(ThisCombo As ComboBox)
SQLcon.Open()
Dim MyQuery As String = "Select * from JFIM_Module_Overview"
Dim MyDataAdapter As New SqlDataAdapter(MyQuery, SQLcon)
Dim MyDataTable As New DataTable
MyDataAdapter.Fill(MyDataTable)
ThisCombo.Items.Clear()
'Cbox_JFIMs.Items.Add("New Module")
For i As Integer = 0 To MyDataTable.Rows.Count - 1
ThisCombo.Items.Add(MyDataTable.Rows(i)(1) & " (" & MyDataTable.Rows(i)(2) & ")")
Next i
SQLcon.Close()
End Sub
It is not working.
I thought of just to pass an arbitrary argument like 1, 2 etc and check that (if or case) in the sub and setup the for loop accordingly but that idea sounds somehow messy. If there any smart why of doing this?
Thanks for help.
-
Dec 21st, 2018, 06:07 AM
#2
Thread Starter
Fanatic Member
Re: Passing a combobox as argument in a sub
I am so sorry; it is actually working. it was just my combobox was hidden under a panel and I was focusing on the wrong combo box.
Sorry again.
And thanks if any one tries to help while I have been writing the apology note.
-
Dec 21st, 2018, 06:13 AM
#3
Re: [RESOLVED] Passing a combobox as argument in a sub
I would make three suggestions to change that code. Firstly, the Call keyword does nothing so there's no point using it. Secondly, instead of doing a "SELECT *" in your query and then processing the data locally, why not just get what you actually want using the query?
vb.net Code:
Dim MyQuery As String = "SELECT FirstColumn + ' (' + SecondColumn + ')' AS Alias FROM JFIM_Module_Overview"
Thirdly, don't add data item by item when you can bind:
vb.net Code:
With ThisCombo .DisplayMember = "Alias" .DataSource = MyDataTable End With
-
Dec 21st, 2018, 09:38 AM
#4
Thread Starter
Fanatic Member
Re: [RESOLVED] Passing a combobox as argument in a sub
Hi,
Thanks very much for the suggestions. As you can read and see, I am a very newbee who sometimes even doesn’t know what exactly he wants. But that won't stop me trying
You are right about the "call". It is just that I sometimes get confused where my subs are when I am reading code lines, I decided to use that "Call" to indicate the subs; but as you said it is not necessary. I will remove them.
You are right again about why not selecting only the needed column. Here the table has only two columns and I was using both. But I will try your query.
And thanks again for reminding of a more efficient way of populating the combos. I will implement that instead. I guess it is also faster and I am not doing anything to the combobox after it is populated.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|