14 Replies - 8602 Views - Last Post: 09 October 2011 - 12:55 PM Rate Topic: -----

#1 sidyusuf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 11

"select an item" combobox on form load event

Posted 09 October 2011 - 08:32 AM

Hi Frnds,
i have four comboboxs that are dependant on each other selection.Its working fine but but what i want is to show "select an item" on zero selectedindex of each combobbox when form load.How can i do that???????what i have done is like as:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.Open()
        Dim dt2 As New DataTable("class")
        Dim sql2 As String
        sql2 = ("SELECT Code+' : '+ Name as Name from class")
        Dim da2 As New OleDbDataAdapter
        da2 = New OleDbDataAdapter(sql2, con)
        da2.Fill(dt2)
        cmbclass.DropDownStyle = ComboBoxStyle.DropDownList
        cmbclass.DataSource = dt2
        cmbclass.DisplayMember = "Name"
        cmbclass.ValueMember = "Name"


        con.Close()
AddHandler cmbclass.SelectedIndexChanged, AddressOf cmbclass_SelectedIndexChanged
        

the other combobxes are populating like same code ...

Is This A Good Question/Topic? 0
  • +

Replies To: "select an item" combobox on form load event

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 08:41 AM

Since you have chosen to use a datasource to populate your combobox you're going to have to add "Choose an Item..." as the first element of your datasource.

Then of course you also have to make sure if someone selects element 0 you react accordingly.

Might just be a ton easier to put a label over the drop down to give instructions.
Was This Post Helpful? 0
  • +
  • -

#3 sidyusuf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 11

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 09:04 AM

yes sir u are right...i think putting a label will not be a gud idea as i have seen many application with "choose an item" text in combobox and they works fine....so i want to do it in same way but i m not getting any idea
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is online

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 09:57 AM

If you dont mind, you can get the table and loop through its columns, like:
cmbclass.Items.Add("Select Item")
For Each ro As DataRow In dt2.Rows
   cmbclass.Items.Add(ro("Name"))
Next
cmbclass.SelectedValue = 0

Was This Post Helpful? 0
  • +
  • -

#5 sidyusuf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 11

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 10:16 AM

View Postsmohd, on 09 October 2011 - 09:57 AM, said:

If you dont mind, you can get the table and loop through its columns, like:
cmbclass.Items.Add("Select Item")
For Each ro As DataRow In dt2.Rows
   cmbclass.Items.Add(ro("Name"))
Next
cmbclass.SelectedValue = 0

sir ,
i tried ur code but it gives an error like:
"Items collection cannot be modified when the DataSource property is set."
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is online

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 10:28 AM

You have to replace the code with this one.
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.Open()
        Dim dt2 As New DataTable("class")
        Dim sql2 As String
        sql2 = ("SELECT Code+' : '+ Name as Name from class")
        Dim da2 As New OleDbDataAdapter
        da2 = New OleDbDataAdapter(sql2, con)
        da2.Fill(dt2)
        cmbclass.DropDownStyle = ComboBoxStyle.DropDownList
        cmbclass.Items.Add("Select Item")
        For Each ro As DataRow In dt2.Rows
           cmbclass.Items.Add(ro("Name"))
        Next
        cmbclass.SelectedValue = 0

        con.Close()
AddHandler cmbclass.SelectedIndexChanged, AddressOf cmbclass_SelectedIndexChanged

Was This Post Helpful? 0
  • +
  • -

#7 sidyusuf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 11

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 11:07 AM

View Postsmohd, on 09 October 2011 - 10:28 AM, said:

You have to replace the code with this one.
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.Open()
        Dim dt2 As New DataTable("class")
        Dim sql2 As String
        sql2 = ("SELECT Code+' : '+ Name as Name from class")
        Dim da2 As New OleDbDataAdapter
        da2 = New OleDbDataAdapter(sql2, con)
        da2.Fill(dt2)
        cmbclass.DropDownStyle = ComboBoxStyle.DropDownList
        cmbclass.Items.Add("Select Item")
        For Each ro As DataRow In dt2.Rows
           cmbclass.Items.Add(ro("Name"))
        Next
        cmbclass.SelectedValue = 0

        con.Close()
AddHandler cmbclass.SelectedIndexChanged, AddressOf cmbclass_SelectedIndexChanged


thank you very much sir,it worked but
but it allows user to select "select item" from combobox. But i want user to select all items except "select item" text.Hope u r getting what i m trying to do...
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is online

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 11:24 AM

He can select it, but you have to make sure that nothing happens or an error message is given when try to use it as a data. Which means you need an if statement to check if user has selected index 0 or other, if index 0 then give him a message
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 11:40 AM

View Postsidyusuf, on 09 October 2011 - 12:07 PM, said:

thank you very much sir,it worked but
but it allows user to select "select item" from combobox. But i want user to select all items except "select item" text.Hope u r getting what i m trying to do...


View PosttlhIn`toq, on 09 October 2011 - 09:41 AM, said:

[...]Then of course you also have to make sure if someone selects element 0 you react accordingly.


Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, and so on like this guy:

    dis not b d'hood dawg... You are sitting at a real keyboard with a real monitor, not a cell phone. You are not here texting your high school posse to come to your kegger after their shift at Taco Bell. You are here asking for help from senior coding professionals who graciously donate their valuable time to helping the next generation of coders with their chosen craft. Please try to show them, yourself and the industry some respect by writing at least at an eighth grade level. (IE: English not ebonics or SMS, real words, punctuation and so on). If you can't take your own problem/question seriously enough to write like an adult, then why would you expect anyone else to take it seriously?

Was This Post Helpful? 0
  • +
  • -

#10 sidyusuf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 11

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 11:48 AM

View PosttlhIn`toq, on 09 October 2011 - 11:40 AM, said:

View Postsidyusuf, on 09 October 2011 - 12:07 PM, said:

thank you very much sir,it worked but
but it allows user to select "select item" from combobox. But i want user to select all items except "select item" text.Hope u r getting what i m trying to do...


View PosttlhIn`toq, on 09 October 2011 - 09:41 AM, said:

[...]Then of course you also have to make sure if someone selects element 0 you react accordingly.


Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, and so on like this guy:

    dis not b d'hood dawg... You are sitting at a real keyboard with a real monitor, not a cell phone. You are not here texting your high school posse to come to your kegger after their shift at Taco Bell. You are here asking for help from senior coding professionals who graciously donate their valuable time to helping the next generation of coders with their chosen craft. Please try to show them, yourself and the industry some respect by writing at least at an eighth grade level. (IE: English not ebonics or SMS, real words, punctuation and so on). If you can't take your own problem/question seriously enough to write like an adult, then why would you expect anyone else to take it seriously?

thank you sir for writing to me.Ur post is really helpful..
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 11:55 AM

View Postsidyusuf, on 09 October 2011 - 12:48 PM, said:

Ur post is really helpful..


Apparently not so much
Was This Post Helpful? 0
  • +
  • -

#12 sidyusuf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 11

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 11:58 AM

View Postsmohd, on 09 October 2011 - 11:24 AM, said:

He can select it, but you have to make sure that nothing happens or an error message is given when try to use it as a data. Which means you need an if statement to check if user has selected index 0 or other, if index 0 then give him a message

thank u sir,i really appreciate ur effort ...but is there any other way to perform this coz although its working fine but in some events its troubling me..or tell me the other way to populate comboboxes(depend to each other selection)with "select item" text.
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is online

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 12:48 PM

I have no any other way for now except using condition to check what user has selected as tlhIn`toq said. But may be others may come with better solutions
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 12:50 PM

That's pretty much it dude.
You either add a bunch of items to the .Items list or you set the DataSource.

If you need to populate combobox2 depending on the selection of combobox1 it remains exactly the same: You have to code it.

Handle the .SelectedIndexChanged event of the first combo box.
Then decide what items have to go into combobox2 based on the selection of combobox1.

I hate to break it to you, but its not all point and click in 5 minutes. There's more to it than just dragging a ready-made control to a form and setting a couple properties. You actually have to write code to produce the results and behavior you want. Then more code to handle the myriad of things the user will do that you didn't expect. Then more code to handle the environment: Like what to do if the network drops out, what to do if the hard drive stops in mid write, what to do if you don't have write privileges to a given directory, what if there is no such thing as a "c:" volume ...

This post has been edited by tlhIn`toq: 09 October 2011 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#15 sidyusuf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 11

Re: "select an item" combobox on form load event

Posted 09 October 2011 - 12:55 PM

ok sir ,i m getting ur perception....well thanks for ur concern..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1