Programmatically create a progressbar

shawne

Well-known member
Joined
Feb 22, 2006
Messages
103
Location
Pennsylvania
Programming Experience
10+
I'm trying to create a progressbar on the fly, but I cannot get it to display on my form. This is in VS2k3/VB.NET. Here is my snippet:

Dim form1 As New myform
Dim progress As New ProgressBar

progress.Width = 320
progress.Height = 200
progress.Text = "Updateing record..."
progress.Name = "progress"
progress.Location = New Point((form1.Left + form1.Width) / 2, (form1.Top + form1.Height) / 2)
form1.Controls.Add(progress)
The code is part of a larger function within a module.
 
progress.visible = true
And you may want to make sure that it isn't being hidden behind something else...

-tg
 
It looks right, but only half of the 'picture' is showing, where are you calling this code? Are you displaying 'form1' instance afterwards? If you want to add the progressbar to current form instance use the 'Me' keyword like 'Me.Controls.Add(progress)'
 
Just saw TechGnome posted at the same time, the default value for Visible property is True, so this is not the problem.
 
you need to change this line ...
progress.Location = New Point((form1.Left + form1.Width) / 2, (form1.Top + form1.Height) / 2)
remove the form1.Left & form1.Top , they reffer to the current screen position of your form, so the Left could already be larger than your Form's width.
to this ...
VB.NET:
progress.Location = New Point((form1.Width) / 2, (form1.Height) / 2)
:)
 
Revised, no joy

Here is my revised code. Still not getting any results.

Dim form1 As New MyForm
Dim progress As New ProgressBar

progress.Parent = form1
progress.Width = 320
progress.Height = 200
progress.Text = "Updateing record..."
progress.Name = "progress"
progress.Location = New Point(((form1.Left + form1.Width) / 2) - 110, ((form1.Top + form1.Height) / 2) - 100)
progress.Visible = True
form1.Controls.Add(progress)
progress.BringToFront()
The location should put the box in the center of the parent form. The function is within a module and being called from a form. This function can be called from various locations in the application. I'm just trying to make the code re-useable.
 
dynamic sysop said:
you need to change this line ...

remove the form1.Left & form1.Top , they reffer to the current screen position of your form, so the Left could already be larger than your Form's width.
to this ...
VB.NET:
progress.Location = New Point((form1.Width) / 2, (form1.Height) / 2)
:)
I'll give it a shot. I was thinking new point was a point anywhere on your current screen resolution, not just the form grid.

**Tried it, still nutt'n
 
Your measures are off, to center this Progressbar object instance within the form1 object instance (of type myForm) this is how:
VB.NET:
progress.Location = New Point((form1.Width - progress.Width) / 2, (form1.Height - progress.Height) / 2)
tested and works just fine (using the form1 object instance Show method to show this instance of the myForm like form1.Show() afterwards as described in post3 of this thread)
 
tried your point, but i'm still not getting any results...if a control beneath it docks can that cause problems? I wouldn't see why, but i'm running out of ideas.
 
AHHH. ... i think finally understand whats going on, but I don't know that i can fix it easily....when i instantiate form1 in my function, i'm not looking at it until i do a .show, which will royally screw things up for me as the form that i'm trying to put the control on is the main interface and is already visible...

I'm GUESSING I would have to dispose of the original form and show the new one with the control on it .....gah ... there's no easy way to do what i'm trying except to either hide the control in design or just create a seperate form specifically for the progressbar....This sounds hairy...
 
No.... that's not what you want to do....trust me on that.

Since the sub that creates the progress bar is in a module, how about passing in the form as a parameter, then adding to it that way?

-tg
 
nm .. I figured it out ... I had already instantiated my form from my module during runtime. My module is set for startup. The problem was I was creating another instance of it and trying to operate off that .... Basically all I had to do is remove the line:

Dim form1 as new myForm

since it was already declared public at the begining of my module..

Thanks for all the help guys...I gotta throw this one in our in house app. designer since he said it was not possible or extremely difficult 8P
Thanks again!
 
Back
Top