Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding A Progressbar To ListBox and/or Statusbar

Status
Not open for further replies.

apc2003

Programmer
Aug 29, 2003
54
0
0
GB
I have an application where we have a statusbar at the base of a form, and a list box on another form that we would like to show a progressbar in one or more of the listbox items or statusbar panels...

As of yet I am unable to make this work, and am very reluctant to overriding OnPaint events etc...

I was hoping for some code something like:
Code:
StatusBar1.Panels[0].Controls.Add(progressBar1);
But as I found out this isn't that easy...

Can anyone suggest a solution for a ListBox Item and a StatusBar Panel?

Thanks in advance...

 
With .NET it is easy. One way is to use Inherit Control project or simply create a dll with the following code.
So, you need a progress bar in a StatusBar then inherit from StstusBar and add there the controls you want such ProgressBar, ComboBox, TreeView etc.

Code:
using System;
using System.Windows.Forms;

namespace MySB
{
	/// <summary>
	/// Summary description for MyStatusBar.
	/// </summary>
	public class MyStatusBar: System.Windows.Forms.StatusBar
	{
		public System.Windows.Forms.ProgressBar progress = new System.Windows.Forms.ProgressBar();
		public MyStatusBar()
		{
			this.Controls.Add(progress);
			this.Panels.Add("progress");
			this.Panels[0].AutoSize=StatusBarPanelAutoSize.Spring;
			this.ShowPanels=true;//false
		}
		private void MyStatusBar_Resize(object sender, System.EventArgs e)
		{
			progress.Left=0;
			progress.Width=this.Panels[0].Width;
			progress.Height=this.Height -2;
			progress.Top= 2;
			Invalidate();
		}
		public int Value
		{
			get {return progress.Value;}
			set {progress.Value= value;}
		}
	}
}
The above code will produce the MySB.dll.
Next, add this control (MySB.dll) in the ToolBox by creating it in My User Control tab.
If you have not yet this one, add new tab using ToolBox->Add Tab. Next select My User Control tab and use Add/Remove to add the above control myStatusBar.
Browse to locate the MySB.dll. You should see now in ToolBox on your VS.NET the MyStatusBar contril.
Next, add this control in your form as a regular StatusBar.
Build and run. You should see the status bar there.
Now, to see the progress bar working in the status bar just run this code:
Code:
private void button1_Click(object sender, System.EventArgs e)
{
	TestProgressBar(10);
}
private void TestProgressBar(int num)
{
	
	this.myStatusBar1.Visible = true;
	myStatusBar1.progress.Minimum = 1;
	myStatusBar1.progress.Maximum = num;
	myStatusBar1.progress.Value = 1;
	myStatusBar1.progress.Step = 1;
	// Loop through .
	for (int x = 1; x <= num; x++)
	{
		myStatusBar1.progress.PerformStep();
		System.Threading.Thread.Sleep(1000);

	}
}
-obislavu-
 
Thanks thats a great help...

I take it that to add a Progressbar to a ListBoxItem would follow the same principles of Inheritance etc... ?

Thanks again...
 
Yes, but with some modification because a ListBox has no Panels for example.
Here is a minimal code that will display a ProgressBar in a user listbox control docked at the upperleft corner of the listbox.
Create a dll with the following code:
Code:
using System;

namespace MyLB
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class MyListBox:System.Windows.Forms.ListBox
	{
		public System.Windows.Forms.ProgressBar progress = new System.Windows.Forms.ProgressBar();
		public MyListBox()
		{
			this.Controls.Add(progress);
			this.Resize+=new EventHandler(MyListBox_Resize);

		}
		private void MyListBox_Resize(object sender, System.EventArgs e)
		{
			Invalidate();
		}
	}
}
Next, in the project were you want to use this control, add this new control, named MyListBox in the ToolBox tab.
ToolBox->Add/Remove Item->Browse and locate the MyLB.dll. You should see a new control in the selected tab.
Now, drag it on your form as a regular control. You should see a rectangle in the listbox.
Add the following code to see the ProgressBar working inside of the listbox:
Code:
private void TestProgressBar2(int num)
{
	this.myListBox1.progress.Visible = true;
	myListBox1.progress.Minimum = 1;
	myListBox1.progress.Maximum = num;
	myListBox1.progress.Value = 1;
	myListBox1.progress.Step = 1;

	// Loop through 
	for (int x = 1; x <= num; x++)
	{
		myListBox1.progress.PerformStep();
		System.Threading.Thread.Sleep(1000);

	}
	this.myListBox1.Items.Add("blablabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
}
You have to decide where to show the ProgressBar inside of the ListBox and how to handle the area when adding/removing items from the listbox.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top