How to load a progress bar in c# win form? - c#

I am trying to open a winForm in C# VS2012. and using this code structure for loading form. I am trying to see the loading status of form How can I do it.fm.LoadingStatus(); is for notation purpose which function i will call to show the correct loading status.
.
.
formOpen = Application.OpenForms["student"];
if (formOpen == null)
{
fm = new student();
fm.TopLevel = false;
fm.Parent = this.panel1;
fn.Show(); //I want to show this progress in loaging bar
int i=0;
progressBar1.Visible = true;
progressBar1.Value = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = ***fm.LoadingStatus()***;
progressBar1.Step = 100;
for (i = 0; i <= 100; i++)
{
progressBar1.PerformStep();
}
}
.
.

Related

MultiMonitor with form and panel

I have 3 monitors connection. I would like to cover up the entire 3 screens with the form and I would like to show the panel just in the center of the primary screen. How should I do this?
Right now I've covered up every screen with this code.
int form_width = 0;
int form_height = 0;
int form_x = 0;
int form_y = 0;
int sub_screen_width = 0;
int sub_screen_height = 0;
bool minus_x = false;
bool minus_y = false;
foreach (Screen screen in Screen.AllScreens)
{
form_width += screen.Bounds.Width;
form_height += screen.Bounds.Height;
if (form_x > screen.Bounds.X)
{
minus_x = true;
form_x = screen.Bounds.X;
}
if (form_y > screen.Bounds.Y)
{
minus_y = true;
form_y = screen.Bounds.Y;
}
if (screen.Bounds.X < 0)
sub_screen_width += screen.Bounds.Width;
if (screen.Bounds.Y < 0)
sub_screen_height += screen.Bounds.Height;
}
this.Width = form_width;
this.Height = form_height;
this.CenterToScreen();
this.Location = new Point(form_x, form_y);
What should I do for the panel??
You could make use of Screen.PrimaryScreen.WorkingArea.
panel1.Location = new Point(Screen.PrimaryScreen.WorkingArea.Left, Screen.PrimaryScreen.WorkingArea.Top);
panel1.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
Btw, To show the form in across Multiple Screen, You dont need to loop through all screens, instead you could make use of VirtualScreen.
this.Size = new System.Drawing.Size(SystemInformation.VirtualScreen.Width,
SystemInformation.VirtualScreen.Height);
this.Location = new Point(SystemInformation.VirtualScreen.Left,
SystemInformation.VirtualScreen.Top);
You could read more on VirtualScreen here

Progress bar doesn't function well

I have an app that reads the smart cards and take a little bit long time
so I need to show a progress bar. However, I followed some tutorials it didn't work for me. The problem is,as seen in the picture, the progress bar remains at the same point even after filling the fields with the data.
These are my codes, as shown within them, I want the progress bar to be completed when data displayed equals to true, and then hide it along with the "Pleas Wait !!!" label.
public void DataDisplayed()
{
//bool displayedData = false;
int t;
progressBar1.Minimum = 0;
progressBar1.Maximum = 200;
if (IDNumbertxt.Text != "" || FullNametxt.Text != "")
{
displayedData = true;
}
else
{
displayedData = false;
}
if (displayedData == false)
{
for (t = 0; t <= 100; t++)
{
progressBar1.Value = t;
}
}
else if (displayedData == true)
{
progressBar1.Value = 200;
waitlbl.Hide();
progressBar1.Hide();
}
Pleas, anyone could help ?
Thanks in advance
Try This.
for (t = 0; t <= 200; t++)
{
progressBar1.Value = t;
}
because your for loop stop at 100 which is the middle value of your progress bar
UPDATE
if (displayedData == false)
{
for (t = 0; t <= 100; t++)
{
progressBar1.Value = t;
}
}
else
{
progressBar1.Value = 200;
waitlbl.Hide();
progressBar1.Hide();
}
if you are using boolean you don't need to use an else if statement because boolean have only two answer it's either True or False
Also because your progressBar1.Hide(); was inside your else condition the progress bar will hide almost instantly after the progress bar value change to 200

How to Print Listed invoices each on another (different) Page in C# windows form Application?

Scenario:
I am working on Windows form Application where i have a today sale report in datagridview and after selecting invoiceID in for loop from datagridview I have to print invoices but each invoice on different page.
How can i shift to next page while printing.
My Code :
private void prnDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
CurrentY = 50;
CurrentX = 100;
for (int i = 0; i < dgvViewGeneralSaleReport.RowCount; i++)
{
if (dgvViewGeneralSaleReport.Rows[i].Cells[0].Value != " ")
{
invoiceID = Convert.ToInt32(dgvViewGeneralSaleReport.Rows[i].Cells[0].Value.ToString().Trim());
//// invoice design Start
leftMargin = 20;
// rightMargin = (int)e.MarginBounds.Right;
rightMargin = 830;
topMargin = (int)e.MarginBounds.Top;
bottomMargin = (int)e.MarginBounds.Bottom;
InvoiceWidth = (int)e.MarginBounds.Width;
InvoiceHeight = (int)e.MarginBounds.Height;
if (!ReadInvoice)
ReadInvoiceData();
SetInvoiceHead(e.Graphics); // Draw Invoice Head
SetOrderData(e.Graphics); // Draw Order Data
SetInvoiceData(e.Graphics, e); // Draw Invoice Data
//// invoice design End
ReadInvoice = true;
e.HasMorePages = true;
CurrentY = 50;
CurrentX = 100;
}
else
{
StopReading = true;
ReadInvoice = false;
e.HasMorePages = false;
break;
}
}
}
You need to place your loop outside of the PrintPage method. The call you make to start the printing needs to be the one in the loop.
Basically...
for (int i = 0; i < dgvViewGeneralSaleReport.RowCount; i++)
{
if (dgvViewGeneralSaleReport.Rows[i].Cells[0].Value != " ")
printDocument.Print();
}
Within the PrintPage method, continue to do all of the same things you are now, only that you will also have to maintain awareness of which page you are printing.
The important point here is that it's the individual call to PrintPage that defines the page. The control variable e.HasMorePages is the way you tell it to keep going.

How to set value of progress bar

I have written a user control using C# Winforms. In the user control, I have three textboxes:
txtStartNumber - input is of type: int.
txtEndNumber - input is of type: int.
txtQuantity - iput is of type: int. (value = txtEndNumber - txtStartNumber)
The progress bar denotes the no. of records added to the database and its total range is set to be equal to txtQuantity.
When one or more records are duplicate, the progress bar is stopped.
My questions are:
How to set the initial value of the progress bar?
How to manage the progress shown by progress bar?
How I save it to the database:
for (long i = from; i < to; i++)
{
for (int j = 0; j < (to - from); j++)
{
arrCardNum[j] = from + j;
string r = arrCardNum[j].ToString();
try
{
sp.SaveCards(r, 2, card_Type_ID, SaveDate, 2);
progressBar1.Value = j;
}
}
}
Try this:
private void StartBackgroundWork() {
if (Application.RenderWithVisualStyles)
progressBar.Style = ProgressBarStyle.Marquee;
else {
progressBar.Style = ProgressBarStyle.Continuous;
progressBar.Maximum = 100;
progressBar.Value = 0;
timer.Enabled = true;
}
backgroundWorker.RunWorkerAsync();
}
private void timer_Tick(object sender, EventArgs e) {
progressBar.Value += 5;
if (progressBar.Value > 120)
progressBar.Value = 0;
}
The Marquee style requires VisualStyles to be enabled, but it continuously scrolls on its own without needing to be updated. I use that for database operations that don't report their progress.
Here is another Progress Bar Tutorial
You can't use loop to do this with progressbar. There is a difference between running code in for, while, do...while loops or in timers. In loops code is immediately done and you can't see this, in timers you can. Even if you try to put in loops if counters, it will not works:
for(int i=a;i<b;++i)
{
if (cnt < 1000000)
{
IncrProgressBar();
cnt++;
}
else
{
cnt = 0;
}
}
If you want to use progressbar to do this then you must put in timer OnTick event code that adds data to database, and in this event increment progressbar value. It's similarly with changing form component's other properties (Text, Size, ...). If you want to see change on component you must use timers.
To change the value use:
private void timer1_Tick(object sender, EventArgs e)
{
progressBar2.Value = progressBar2.Value - 15;
}
In C#

Trying to create a new page when horizontal pos. of i extends past right margin

I am trying add a page when horizontal or the x position is greater than a counter in order to keep a right side margin. When I run the code I end up in an infinate loop of hundreds of pages all displaying the same first page graphics. Thinking it might have to do with my lack of understanding HasMorePages. I could use some help. Thanks.
public static class PrintWave
{
public static void PrintPreWave()
{
PrintDocument pd = new PrintDocument();
if (WaveTools.MySettings == null)
{
pd.DefaultPageSettings.Landscape = true;
}
else
{
pd.DefaultPageSettings = WaveTools.MySettings;
}
pd.OriginAtMargins = true;
pd.PrintPage += new PrintPageEventHandler(OnPrintPage);
PrintDialog dlg = new PrintDialog();
PrintPreviewDialog printPreviewDlg = new PrintPreviewDialog();
printPreviewDlg.Document = pd;
Form p = (Form)printPreviewDlg;
p.WindowState = FormWindowState.Maximized;
printPreviewDlg.ShowDialog();
}
private static void OnPrintPage(object sender, PrintPageEventArgs e)
{
string MyTag = string.Empty;
MyTag = WaveActions.ActiveId;
Wave MyWave = WaveHolder.FindWave(MyTag);
int MyCount = 0;
int xOffset = e.MarginBounds.Location.X;
int yOffset = e.MarginBounds.Location.Y;
if (MyWave != null)
{
Graphics g = e.Graphics;
g.SetClip(e.PageBounds);
Pen MyPen = new Pen(WaveTools.WaveColor, WaveTools.PenWidth);
float dx = (float)e.PageBounds.Width / MyWave.NumSamples;
float dy = (float)e.PageBounds.Height / 255;
if (MyWave.Normal == false)
{
g.ScaleTransform(dx, dy);
}
for (int i = 0; i < MyWave.NumSamples - 1; i++)
{
g.DrawLine(MyPen, i, MyWave.Data[i], i + 1, MyWave.Data[i + 1]);
MyCount = MyCount + 1;
if (MyCount > e.MarginBounds.Width)
{
e.HasMorePages = true;
MyCount = 0;
return;
}
else
{
e.HasMorePages = false;
return;
}
}
}
}
}
}
for (int i = 0; i < MyWave.NumSamples - 1; i++)
That's the core problem statement, you start at 0 every time PrintPage gets called. You need to resume where you left off on the previous page. Make the i variable a field of your class instead of a local variable. Implement the BeginPrint event to set it to zero.
The else clause inside the loop need to be deleted.

Categories