iNET Interactive - Online Advertising Agency
          
Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Interface and Graphics > How To Dynamically add controls to a scrollable container?


Reply
 
Thread Tools Display Modes
  #1  
Old 11-07-2002, 08:29 AM
Draco97 Draco97 is offline
Centurion
 
Join Date: Nov 2002
Location: Arlington, VA
Posts: 114
Question How To Dynamically add controls to a scrollable container?

I am using the code below inside of an event procedure to allow the user to dynamically add buttons to a form. The problem is that once the user adds enough buttons, they "run off" the form. I understand forms are not scrollable by default, so I tried a frame and this did not work either. Could someone please provide me a quick and easy solution that does not involve adding much code? Is there a container I could use? How could I modify the code below to add the buttons dynamically to a container rather than a form?

Code:
Static i As Integer

Set btnObj = Controls.Add( "VB.CommandButton", "button" & (i + 1))
    With btnObj
        .Visible = True
        .Caption = "New Button" & Str(i + 1)
        .Top = 720 + i * 540
        .Left = 120 + i * 540
        .Width = 2175
    End With

i = i+1
Thanks in advance.
Reply With Quote
  #2  
Old 11-07-2002, 11:24 AM
Ales Zigon's Avatar
Ales Zigon Ales Zigon is offline
Dead dog's ghost
Forum Leader
* Expert *
 
Join Date: Feb 2001
Location: Celje, Slovenia, Europe
Posts: 2,601
Default

For containr, use Picture box instead (place one picture inside of another and use scroll bars to move it - search the forum for examples). For adding controls, use Load instead of Add.
Like this:
Code:
Form1.Load Button1(x)
where x is the index of the button in the control array of buttons.
__________________
Yes, MSDN comes with VB! Yes, you must have at least 25 post to have an avatar! No, you cant write your OS in VB! and NO, YOU CAN NOT DECOMPILE IT!

I'm sure there are things that are more important than me - I just can't thing of any...
Reply With Quote
  #3  
Old 11-07-2002, 11:29 AM
Thinker Thinker is offline
Iron-Fisted Programmer
Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

You need a scrollable container, and you need to set the control's
Container property to the container control object. I completely
agree with zigona, use a control array rather than trying to add
to the controls collection. You won't easily be able to get events
from ones added to the controls collection.
Look here for an example scrollable container control...
http://www.xtremevbtalk.com/show...threadid=31824
__________________
Posting Guidelines
Reply With Quote
  #4  
Old 11-07-2002, 12:30 PM
Draco97 Draco97 is offline
Centurion
 
Join Date: Nov 2002
Location: Arlington, VA
Posts: 114
Default Thanks, but...

Thanks to both of you. I understand about the control array. I do need to use one so I can write code for the click event.


Thinker: I downloaded the project, but I am new to VB and having trouble understanding it. Is there any "quick and dirty solution"? I don't think I can use the picturebox in a picturebox idea because I don't know how many buttons the user will add. Also, the control array is on a SSTab control on an MDIchild form. Please help.
Reply With Quote
  #5  
Old 11-07-2002, 01:52 PM
Thinker Thinker is offline
Iron-Fisted Programmer
Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

Believe me, if there were a quicker and dirtier solution I wouldn't
have bothered with the Scrollcontrol. I built it starting with an
article in msdn that described the process and gave a good deal
of working code, then I expanded on it.

It isn't fun or pretty to work with, but one shouldn't even be
doing this without a very good reason and the knowledge to
back that reason up. Otherwise, it is just a bad design idea.
__________________
Posting Guidelines

Last edited by Thinker : 11-07-2002 at 04:37 PM.
Reply With Quote
  #6  
Old 11-07-2002, 02:50 PM
Draco97 Draco97 is offline
Centurion
 
Join Date: Nov 2002
Location: Arlington, VA
Posts: 114
Question Thanks

Thanks Thinker. I appreciate your always helping me out.


Why do I keep reading and hearing - just "put a picturebox in a picturebox"? Am I correct in saying that won't work - because the size of the inner picture box is fixed and the user can add as many controls as he wants so the ones that "run off the bottom" will not be viewable?
Reply With Quote
  #7  
Old 11-07-2002, 04:42 PM
Thinker Thinker is offline
Iron-Fisted Programmer
Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

It is a standard method of doing this without adding extra
controls and it will work given enough code. It shares a similar
problem to the scroll control in that at design time you have to
either size the form and control/picturebox(s) larger and place
controls, or position controls using the Left and Top properties
without being able to see where the controls actually end up.
Your situation is even worst since the controls are added at run
time and you have to have a way to resize the container to match.
__________________
Posting Guidelines
Reply With Quote
  #8  
Old 11-07-2002, 05:12 PM
Thinker Thinker is offline
Iron-Fisted Programmer
Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

Assuming you have a binary compatible scrollcontrol OCX from the
Code Library download, you can try this test project to see how it
would work with dynamically created controls. When you run it,
just keep clicking top command button and when more than can
fit on the scrollcontroll have been loaded, a vertical scrollbar will
appear.
Attached Files
File Type: zip test sc.zip (1.3 KB, 31 views)
__________________
Posting Guidelines
Reply With Quote
  #9  
Old 11-07-2002, 08:21 PM
Draco97 Draco97 is offline
Centurion
 
Join Date: Nov 2002
Location: Arlington, VA
Posts: 114
Unhappy Thanks and Sorry

Thinker - you are such a big help!

I don't understand this: "binary compatible scrollcontrol OCX" - I searced this board and Google but I can't find this. I really want to try the project you made for me but I couldn't. Can you please help 1 more time? Thank you so much.
Reply With Quote
  #10  
Old 11-07-2002, 08:40 PM
Thinker Thinker is offline
Iron-Fisted Programmer
Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

Ok, I should have explained it, but I keep thinking people already
understand it. The ScrollCtrl project in the zip is an ActiveX control
project. It compiles to an OCX file. When ActiveX component
projects are compiled for the first time, a number of GUID numbers
are assigned to various aspects, and those numbers are stored in
in the registry when the component is registered. Those numbers
are also how a standard exe project knows how to find the OCX
controls that have been added to the toolbox using the
Components dialog. So, it is very important that those numbers
remain the same if at all possible. The way it no longer becomes
possible is when the interface of the component changes. To help
stop this from happening, there is an option on the Project
Properties - Components tab for setting binary compatibility to
a file (usually the first compiled instance of the component). What
I usually do (and did for this project) is to rename the first .OCX
file to .CMP. Then I point the binary compatibility to that .CMP file.
As long as you don't change this, or do something to the code to
break compatibility, you can just compile a new instance of the
.OCX file. As soon as you compile it, the VB IDE will automatically
register it on your system. You can then unload that project, and
load the test one I attacted above, and run it.
__________________
Posting Guidelines
Reply With Quote
  #11  
Old 11-07-2002, 09:29 PM
Draco97 Draco97 is offline
Centurion
 
Join Date: Nov 2002
Location: Arlington, VA
Posts: 114
Unhappy Thanks

Thanks for the great explanation. I'm sure others will be able to understand it but it was over my head.


I went to the first project and did run->start with full compile. Nothing happened - I am not even sure which file is the correct one. I tried several.


When I open the 2nd project I get an error message but I click Yes for continue load anyway. In the log it says:

Line 13: Class ScrollCtrl.ScrolledWindow of control SC1 was not a loaded control class.


When I run, I get the form, but when I click the button, I get an error - it can't get past the line:

SC1.ArrangeConstituentControls

It says Method or Data Member not found. If I comment this line out - the program looks like the stuff I've been doing all day - with buttons getting lost when space runs out.


Sorry Thinker - I'm sure you think I am hopeless by now - but I just started with VB6 last month and I am just trying to learn as much as I can.

Thanks yey again for everything.
Reply With Quote
  #12  
Old 11-07-2002, 10:00 PM
Thinker Thinker is offline
Iron-Fisted Programmer
Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

Do you not have the option to make ScrollCtrl.ocx on your File
menu? If you don't, then you might have the learning edition, and
you just won't be able to use this project. You can't 'run' an
ActiveX control project. You have to be able to compile it into an
OCX file.

If you can't make the control, you could rename the scrollctrl.cmp
file in the Zip to scrollctrl.ocx, and make sure it is in the same
directory as the test project. It should work then (you might have
to unzip the test project again if you saved it after the errors).
__________________
Posting Guidelines
Reply With Quote
  #13  
Old 11-08-2002, 07:55 AM
Draco97 Draco97 is offline
Centurion
 
Join Date: Nov 2002
Location: Arlington, VA
Posts: 114
Thumbs up Wow!!

Thank you so much!! I really appreciate all of your trouble - regardless of what the outcome was. But the outcome was great too. It works perfectly!!! I could not ask for more.


More Stupid Questions:


What is your philosophy on giving away code? Would you let people use your control in a commercial application? Would you call your control freeware?

How can I see the code for your control?


Anyway, thanks for everything. I'm working by myself without any programmers to talk to and bounce ideas off - so I really appreciate having a community that I can talk to. Without this board I would be really isolated.
Reply With Quote
  #14  
Old 11-08-2002, 08:25 AM
Thinker Thinker is offline
Iron-Fisted Programmer
Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

The code is in the Scrollctrl project. When you load it, just open
the code pane for the control. As I mentioned above, most of this
code started from an article in MSDN, so I don't even claim it as
my own anyway. Code in MSDN can be used and distributed (as
long as the MSDN EULA is followed). In this case, you can use this
control in other commercial apps as long as you understand you
don't have any exclusive rights to it.

I have posted a great deal of code in Tutor's Corner/Code Library
since I came here and the only requirement I make on anyone
using it is to not take credit for it themselves (crediting me and
this forum would be nice).
__________________
Posting Guidelines
Reply With Quote
  #15  
Old 11-08-2002, 09:45 AM
Draco97 Draco97 is offline
Centurion
 
Join Date: Nov 2002
Location: Arlington, VA
Posts: 114
Cool Thanks Thinker

I found the code. I will certainly give you and the board credit for it. It is very generous of you to share your work to help other people.


This was a really good thread. I hope others can benefit from it as well.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Advertisement: