Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi Guys,

 

I wrote some simple code on having a progress bar. But i wish to do some enhancement. Its currently working as i want but i would appreciate if this two things can be completed as well.

 

1) Having the percentage in the progress bar itself. I mean each time it loops, the percentage which is placed in progress bar (acts as an indicator) increases as well..I dont want to place a label in progress bar, just wondering

weather is the any property in progress bar that allows me to provide indicator in percentage

 

2) By default, the progress bar is showing grey color, can i change it to green or something to make it look more appealing

 

int TotalRecords = 25000; //Initialized to 25000 arbitrarily 


progressBar1.Step = 500;
progressBar1.Minimum = 1;
progressBar1.Maximum = TotalRecords-1; 
		
for(int y = 0; y<TotalRecords; y++) 
{ 
	if (progressBar1.Value < progressBar1.Maximum)
	{
	   progressBar1.Value += progressBar1.Minimum;
	   Application.DoEvents();
	}
} 
            MessageBox.Show ("Done");

  • Leaders
Posted

made a quick example for you incase you aint resolved this.

you need to set the PBM_SETBKCOLOR & PBM_SETBKCOLOR on your bar via sendmessage, the percentage is a simple division :)

Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Private Const WM_USER As Int32 = &H400
Private Const CCM_FIRST As Int32 = &H2000
Private Const CCM_SETBKCOLOR As Int32 = (CCM_FIRST + 1)
Private Const PBM_SETBARCOLOR As Int32 = (WM_USER + 9)
Private Const PBM_SETBKCOLOR As Int32 = CCM_SETBKCOLOR
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	'/// progressbar colour ( the value part )
	SendMessage(ProgressBar1.Handle.ToInt32, PBM_SETBARCOLOR, 0, ColorTranslator.ToWin32(Color.LightGreen))
	'/// now the back colour of the bar
	SendMessage(ProgressBar1.Handle.ToInt32, PBM_SETBKCOLOR, 0, ColorTranslator.ToWin32(Color.White))
End Sub
Private Function BarPercentage(ByVal bar As ProgressBar) As Int32
	Return ((ProgressBar1.Value / ProgressBar1.Maximum) * 100)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Dim pThread As New Threading.Thread(AddressOf progThread)
	pThread.Start()
End Sub
Private Sub progThread()
	ProgressBar1.Step = 1
	For i As Int32 = ProgressBar1.Minimum To ProgressBar1.Maximum
		ProgressBar1.PerformStep()
		MyBase.Text = BarPercentage(ProgressBar1) & "% complete"
		System.Threading.Thread.Sleep(1)
	Next
End Sub

hope it helps.

Posted
I presume there isn't a purely .NET way to do it? I didn't turn anything up in my search either, as this was a question also on my mind I was debating a post then I saw this.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • Leaders
Posted
There is no purely .Net way to do this. Furthermore, I am pretty sure that you will have no control over color when the progress bar is using Visual Styles, so keep this in mind.
[sIGPIC]e[/sIGPIC]
Posted

Hi dynamic_sysop ,

 

Thank You very very much for the solution..Thankx for time spent on example..

Really appreciate it...really helps.thx..

Posted (edited)

hi,

 

i have implemented a small example with your code in c#" but the percentage does not change. It starts at 0% and only changed when 100% is achivied. Shouldn't it, as the progress bar progresses the percentadge change?

 

 

Here is the code in c#

 

[csharp]

[system.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="SendMessageA", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]

private static extern Int32 SendMessage(Int32 hwnd, Int32 wMsg, Int32 wParam, Int32 lParam);

private const Int32 WM_USER = 0X400;

private const Int32 CCM_FIRST = 0X2000;

private const Int32 CCM_SETBKCOLOR = (CCM_FIRST + 1);

private const Int32 PBM_SETBARCOLOR = (WM_USER + 9);

private const Int32 PBM_SETBKCOLOR = CCM_SETBKCOLOR;

 

private void Form1_Load(object sender, System.EventArgs e)

{

///// progressbar colour ( the value part )

SendMessage(progressBar1.Handle.ToInt32(), PBM_SETBARCOLOR, 0, ColorTranslator.ToWin32(Color.Gainsboro));

///// now the back colour of the bar

SendMessage(progressBar1.Handle.ToInt32(), PBM_SETBKCOLOR, 0, ColorTranslator.ToWin32(Color.White));

}

 

private Int32 BarPercentage(ProgressBar bar)

{

return ((progressBar1.Value / progressBar1.Maximum) * 100);

}

 

private void button1_Click(object sender, System.EventArgs e)

{

Thread pThread = new Thread(new System.Threading.ThreadStart(progThread));

pThread.Start();

}

 

private void progThread()

{

progressBar1.Step = 1;

 

for (Int i = progressBar1.Minimum; i <= progressBar1.Maximum; i++)

{

progressBar1.PerformStep();

base.Text = BarPercentage(progressBar1) + "% complete";

System.Threading.Thread.Sleep(100);

}

}

[/csharp]

thx

Edited by rfazendeiro

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...