Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ComboBox with textbox in vb.net2003

Posted on 2006-04-01
14
Medium Priority
?
336 Views
Last Modified: 2008-01-09
hi..

I have one combobox and one textbox..
the combobox has a numbers, and the textbox has the name..
numbers and names are comming from one table that's being in the database..

each number has one name in the table..

when the user press on the combobox it will show the numbers that being in the table

what I want is how can I display the name automatically in the textbox when the user define the number in the combobox.

thanks
0
Comment
Question by:Jassimi
  • 7
  • 7
14 Comments
 
LVL 14

Accepted Solution

by:
PockyMaster earned 60 total points
ID: 16351266
You can do like this:

create a class Containing number and  Name

public class MyData
 private _sName as string
private _iNumber as integer

public sub New (sName as string, iNumber as integer )
_sName =sName
_iNumber = iNumber
end sub

public Readonly Property Name as string
return _sName
end property

public readonly Property Number as Integer
return _iNumber
End Property

End Class

Use this class when you get your data

e.g.

Public Function MyDataFunction() as ArrayList

dim arData as new ArrayList

While Not ...  (psuedo code)
 arData.Add (New MyData (dr!Name, dr!Number))

End While

In your form:

cboData.DisplayMember = "Number"
cboData.DataSource = MyDataFunction()


Then add an event in the change of your combobox
and implement there:

dim myItem as MyData = DirectCast(cboData, MyData)
txtMyName.Text= myItem.Name

Get it??

Have fun!
0
 
LVL 2

Author Comment

by:Jassimi
ID: 16368223
hi..

sorry for late..
but I was trying your method..
I'm sorry to tell u that I didn't get it..

can u explain more about how to do that

thanks
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16368272
Show me your data retrieval method, and I'll put it in
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:Jassimi
ID: 16374613
the data coming from suppliers datatable source
number feild is (supplier_id)
name  feild id (supplier_name)

returning to the form:
the combobox is(supplier_id)
the textbox is (supplier_name)


so when the user select the supplier_id from combobox.. the supplier_name will display automatically in the (supplier_name) textbox
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16374668

(dtSuppliers will be filled by your query)

dim arResults as new ArrayList

dim data as MyData
For each dr as dataRow in dtSuppliers.Rows
  data = new MyData(dr!Supplier_Name,dr!Supplier_ID)
  arResults.Add(data)
Next

cboSuppliers.DisplayMember = "Number"
cboSuppliers.DataSource = arResults


 Private Sub cboSuppliers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSuppliers.SelectedIndexChanged
        If Not cboSuppliers.SelectedItem Is Nothing Then
            txtSupplierName.Text = DirectCast(cboSuppliers, Mydata).Name
        End If
    End Sub


You might have to change the names of the combobox and the textbox to your own, but I guess it should be more clear now
0
 
LVL 2

Author Comment

by:Jassimi
ID: 16381126

it's showing some errors on the following statements:
1- name: in (Public ReadOnly Property Name() As String)
                error: 'ReadOnly' property must provide a 'Get'

2- numver: in (Public ReadOnly Property number() As String_
                 error: 'ReadOnly' property must provide a 'Get'

3- return_sName..  error(statement cannot apear withen a proberty body.End of propertry assumed)

4- end property ..  error(must be proceded by matching 'Property')

5- return_iNumber.. error(statement cannot apear withen a proberty body.End of propertry assumed)


0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16382324
Yeah, I typed it just as an example :D
Didn't put it in my environment first :D
I assumed you could figure that one out yourself.

So yeah,

Public Readonly Property Name as string
Get
Return _sName
End Get

End Property
0
 
LVL 2

Author Comment

by:Jassimi
ID: 16384588
ok..

now it's showing another error in for statement:
 For Each dr As DataRow In dtSuppliers.Rows

the error says: Object reference not set to an instance of an object

and the statement become with yellow color
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16384607
In my example I wrote:
dtSuppliers is a datatable you filled somewhere..
did you do that?
0
 
LVL 2

Author Comment

by:Jassimi
ID: 16385186
which one u mean?

sorry but I didn't get it
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16385268
Your statement:

the data coming from suppliers datatable source
number feild is (supplier_id)
name  feild id (supplier_name)

returning to the form:
the combobox is(supplier_id)
the textbox is (supplier_name)


so when the user select the supplier_id from combobox.. the supplier_name will display automatically in the (supplier_name) textbox
---------------

So I stated : dtSuppliers is the DataTable obj, which is filled with the Supplier Data (which you need to write code for :D )

Which could be something like

Dim conn as new OleDb.OleDbConnection (YourconnectionString)
Dim dtSuppliers As New DataTable
Dim MyDataAdapter As New  OleDbDataAdapter
Dim MyCommand As New  OleDbCommand
MyCommand.CommandType = CommandType.Text
'I have to guess your table name
MyCommand.CommandText = "SELECT SUPPLIER_ID, SUPPLIER_NAME FROM SUPPLIERS ORDER BY SUPPLIER_NAME "
MyCommand.Connection = Conn
MyDataAdapter.SelectCommand = MyCommand
MyDataAdapter.Fill(dtSuppliers )

 MyCommand.Dispose()
    MyDataAdapter.Dispose()
   
0
 
LVL 2

Author Comment

by:Jassimi
ID: 16385756
ok.. I did that and the error is gone..

but now it's showing another error on this statement:
txtSupplierName.Text = DirectCast(cboSuppliers, Mydata).Name

the error says: Specified cast is not valid.

and the statement become with yellow color also..

sorry for bothering u
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16389227
Oops, my mistake..

txtSupplierName.Text = DirectCast(cboSuppliers.SelectedItem, Mydata).Name
0
 
LVL 2

Author Comment

by:Jassimi
ID: 16393712
ok..

it's working now..

thank u
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question