10 Replies - 528 Views - Last Post: 24 October 2012 - 06:24 AM Rate Topic: -----

#1 tvellalott  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-August 12

Creating multiple instances of DropDownLists on Click

Posted 15 August 2012 - 06:12 PM

Hi guys,
Long time lurker, first time poster.
I am developing a little piece of software for my employer and I'm wondering the best solution. I know this is possible (anything is possible, amirite?) but I'm a bit of a mug programmer and I'm not sure of the best approach.
Attached is a picture of the form to give you an idea of the context of my question...

As you can see, there are 4 DropDownLists. What I want is to for another set of these same 4 DDLs to be generated on the click of a button, up to as many as is necessary.

I know you can make a bunch of them and hide them until a button is clicked, but that's not a good solution for obvious reasons. I've never actually dealt with code which generates more code. I'm hoping it's not outside of my relatively limited skills.

Thanks in advance,
Tom

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Creating multiple instances of DropDownLists on Click

#2 b2.f2  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 76
  • Joined: 21-May 07

Re: Creating multiple instances of DropDownLists on Click

Posted 16 August 2012 - 01:00 AM

i would do it like this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim droplists(3) As ComboBox
        Dim position As Integer = 10
        For i As Integer = 0 To droplists.Length - 1
            droplists(i) = New ComboBox
            droplists(i).Left = position
            position += 150
            droplists(i).Top = 20
            droplists(i).DataSource = New BindingSource(SomeLists(i), Nothing)
            Me.Controls.Add(droplists(i))

        Next
    End Sub



Just add a conditions and that’s it.
And you have to adjust the position obviously.

This post has been edited by b2.f2: 16 August 2012 - 01:02 AM

Was This Post Helpful? 1
  • +
  • -

#3 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 209
  • View blog
  • Posts: 1,371
  • Joined: 16-March 11

Re: Creating multiple instances of DropDownLists on Click

Posted 16 August 2012 - 04:56 AM

Combobox or drop down list?
Was This Post Helpful? 0
  • +
  • -

#4 b2.f2  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 76
  • Joined: 21-May 07

Re: Creating multiple instances of DropDownLists on Click

Posted 16 August 2012 - 05:04 AM

View Posttrevster344, on 16 August 2012 - 05:56 AM, said:

Combobox or drop down list?

There is no drop down list in vb.net so my guess is he meant combobox.
well drop down list is combobox in vb.net
Was This Post Helpful? 0
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 209
  • View blog
  • Posts: 1,371
  • Joined: 16-March 11

Re: Creating multiple instances of DropDownLists on Click

Posted 16 August 2012 - 06:12 AM

Well there is but it's a web control, was just curious.
Was This Post Helpful? 0
  • +
  • -

#6 tvellalott  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-August 12

Re: Creating multiple instances of DropDownLists on Click

Posted 24 October 2012 - 05:21 AM

OK, thanks a lot to b2.f2. I took what you gave me and made it create new ComboBox's (better get my terminology right, :P) really well...

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Me.btnNext.Location = New Point(Me.btnNext.Location.X, Me.btnNext.Location.Y + 28)

        Dim dropDownLists(3) As ComboBox

        For i As Integer = 0 To dropDownLists.Length - 1
            dropDownLists(i) = New ComboBox
            dropDownLists(i).DropDownStyle = ComboBoxStyle.DropDownList
            dropDownLists(i).Left = positionx
            dropDownLists(i).Top = positiony
            dropDownLists(i).Width = 175
            dropDownLists(i).Height = 20
            positionx += 181
            Me.Controls.Add(dropDownLists(i))
        Next
        positionx = 24
        positiony += 28
    End Sub



I now have two problems I need to resolve before it's perfect and I can do a little dance.

1) You have this line here...
 droplists(i).DataSource = New BindingSource(SomeLists(i), Nothing)



Now this style of applying data is familiar to me but I've never used the BindingSource thing before. Can someone link me to a relevant example?

2) If you click the button 7 or 8 times, it fills up the form, to the point where you can no longer click the button. How do you enable scrolling if needed?
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1386
  • View blog
  • Posts: 4,465
  • Joined: 25-September 09

Re: Creating multiple instances of DropDownLists on Click

Posted 24 October 2012 - 06:00 AM

Easiest way to scroll is to put them in a panel instead of directly onto the form. as the controls move beyond the bounds of the panel, scrollbars will appear allowing you to scroll through them.

On another note, have you thought about just using those original comboboxes to add to a listview or datagridview instead of creating the controls over and over?

In other words
select the options from the combobox, Click a button to add those selections to a grid, reset the comboboxes for the next entry.

At the end, you have all your selections in a nice grid layout to do with as you please.
Was This Post Helpful? 0
  • +
  • -

#8 tvellalott  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-August 12

Re: Creating multiple instances of DropDownLists on Click

Posted 24 October 2012 - 06:06 AM

I did think of that, but what happens if the person wants to go back and edit a previous entry?
Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1386
  • View blog
  • Posts: 4,465
  • Joined: 25-September 09

Re: Creating multiple instances of DropDownLists on Click

Posted 24 October 2012 - 06:07 AM

Take the selected item of the grid and use the columns to populate the comboboxes. Set a flag to editmode so that it updates the grid instead of adding a new row.

Edit:

Or, you could even create a datagridview with comboboxcolumn types in the cells. Then with each new row, they select from a combobox that appears when they enter the column.

This post has been edited by CharlieMay: 24 October 2012 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#10 tvellalott  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-August 12

Re: Creating multiple instances of DropDownLists on Click

Posted 24 October 2012 - 06:10 AM

OK, well I'll come back to that. It's 12.09am and I want to have some sort of prototype working by tomorrow, lol. The panel is a good idea but how do I create the new comboboxes on top of it?
Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1386
  • View blog
  • Posts: 4,465
  • Joined: 25-September 09

Re: Creating multiple instances of DropDownLists on Click

Posted 24 October 2012 - 06:24 AM

instead of me.controls.add, you would use PanelName.Controls.Add
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1