|
Hi there,
I use a custom progressbar with text in a custom control and I update the text with a timer.The text is shown and everything works but the text over the progressbar mess up instead of being updated.When I minimize the main form and maximize it back, it updates. All of my attempts(using update(),refresh() and invalidate()) are useless.Could you please tell me what I should do?
note: I use the same control on another form but this time not in a custom control and it works like a charm
|
|
|
|
|
Is it possible to post some of the code?
Regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
//this code is in a custom control's class and the custom control is in main form
DateTime dt_starttime; //this takes the start time from a button on mainform when clicked
TimeSpan ts_elapsed;
public void tmrSure_Tick(object sender, EventArgs e) //this is triggered from the main form
{
// taking the elapsed time
DateTime dt_now = DateTime.Now;
ts_elapsed = dt_now.Subtract(dt_starttime);
theBestProgressBarEver1.CenterText = string.Format("{0:00}:{1:00}", ts_elapsed.Hours, ts_elapsed.Minutes); //CenterText is to show text
}
//normally the code is spreaded,I collected the pieces together to be able to post
thanks for your interest
|
|
|
|
|
|
thanks Programm3r
I solved the issue by putting ProgressBar.Refresh() on top in the timer event before the text property of the progressbar gets changed. But before, I had put it in the bottom of timer and didn't work. Anyway, thanks for dealing.
|
|
|
|
|
I want to Serialize a Dictionary in Binary format but I get an error message telling that this objecto is not Serializable, is there any way of do it or I need to create my own dictionary class.
Regards
|
|
|
|
|
There is an article here on CP about serializing Dictionaries (amongst other things). Unfortunately, for you, it uses XML Serialization. In case it may offer you some clues, here is a link Yet Another XML Serialization Library for the .NET Framework[^]
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi all,
I found the following code, that allows one to write a string on a ProgressBar ... Only thing is that I can't get it to work Can anyone else make it work, and if so ... what am I doing wrong?
SetProgressBarText(progressBar1, null, ProgressBarTextLocation.Centered, Color.Black, SystemFonts.DefaultFont);
private void SetProgressBarText(System.Windows.Forms.ProgressBar Target,
string Text,
ProgressBarTextLocation Location,
System.Drawing.Color TextColor,
System.Drawing.Font TextFont
)
{
if (Target == null)
throw new ArgumentException("Null Target");
if (string.IsNullOrEmpty(Text))
{
int percent = (int)(((double)(Target.Value - Target.Minimum) / (double)(Target.Maximum - Target.Minimum)) * 100);
Text = percent.ToString() + "%";
}
using (Graphics gr = Target.CreateGraphics())
{
gr.DrawString(Text,
TextFont,
new SolidBrush(TextColor),
new PointF(
Location == ProgressBarTextLocation.Left ? 5 :
progressBar1.Width / 2 - (gr.MeasureString(Text,
TextFont).Width / 2.0F),
progressBar1.Height / 2 - (gr.MeasureString(Text,
TextFont).Height / 2.0F)));
}
}
public enum ProgressBarTextLocation
{
Left,
Centered
}
Many thanks in advance
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
|

public class ProgressLabel: ProgressBar {
private static StringFormat sfCenter = new StringFormat() {
Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
private Color textColor = DefaultTextColor;
private string progressString;
public ProgressLabel() { SetStyle(ControlStyles.AllPaintingInWmPaint, true); }
protected override void OnCreateControl() {
progressString = null;
base.OnCreateControl();
}
protected override void WndProc(ref Message m) {
switch(m.Msg) {
case 15: if(HideBar) base.WndProc(ref m);
else {
ProgressBarStyle style = Style;
if(progressString == null) {
progressString = Text;
if(!HideBar && style != ProgressBarStyle.Marquee) {
int range = Maximum-Minimum;
int value = Value;
if(range > 42949672) { value = (int)((uint)value>>7); range = (int)((uint)range>>7); }
if(range > 0) progressString = string.Format(progressString.Length == 0 ? "{0}%" : "{1}: {0}%",
value*100/range, progressString);
}
}
if(progressString.Length == 0) base.WndProc(ref m);
else using(Graphics g = CreateGraphics()) {
base.WndProc(ref m);
OnPaint(new PaintEventArgs(g, ClientRectangle));
}
}
break;
case 0x402: goto case 0x406;
case 0x406: progressString = null;
base.WndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void OnPaint(PaintEventArgs e) {
Rectangle cr = ClientRectangle;
RectangleF crF = new RectangleF(cr.Left, cr.Top, cr.Width, cr.Height);
using(Brush br = new SolidBrush(TextColor))
e.Graphics.DrawString(progressString, Font, br, crF, sfCenter);
base.OnPaint(e);
}
public bool HideBar {
get { return GetStyle(ControlStyles.UserPaint); }
set { if(HideBar != value) { SetStyle(ControlStyles.UserPaint, value); Refresh(); } }
}
public static Color DefaultTextColor {
get { return SystemColors.ControlText; }
}
public Color TextColor {
get { return textColor; }
set { textColor = value; }
}
public override string Text {
get { return base.Text; }
set { if(value != Text) { base.Text = value; progressString = null; } }
}
public override Font Font {
get { return base.Font; }
set { base.Font = value; }
}
}
|
|
|
|
|
Thanks firda, been looking for that code all over.
|
|
|
|
|
how to use the same macro of excel in c#
Private Sub Worksheet_Activate()
Range("A1").Select
End Sub
|
|
|
|
|
|
the problem is im having a xls file and im opening it using workbook.open() and i select cell A10 and close it next time when i open the file i want the focus back on A1. i.e always the file is open the focus should be on cell A1 for that i used the macro which i stated in the previos thread. but i feel it would be better if i can do that also in the application.
below is the code
private void button1_Click(object sender, EventArgs e)
{
openexcel();
}
private static void openexcel()
{
Excel.Application xlApplication = new Excel.Application();
Excel.Workbooks xlBooks;
Excel.Worksheet xls;
xlBooks = xlApplication.Workbooks;
xlBooks.Open(Application.StartupPath + @"\ExcelFile01.xls", Type.Missing, false, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
xls = xlApplication.ActiveWorkbook.Worksheets[2] as Excel.Worksheet;
xls.Visible = Excel.XlSheetVisibility.xlSheetHidden;
xls = xlApplication.ActiveWorkbook.ActiveSheet as Excel.Worksheet;
xls.Rows.AutoFit();
//todo
focus back to cell A1
xlApplication.Visible = true;
System.Runtime.InteropServices.Marshal.ReleaseComObject(xls);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication);
}
|
|
|
|
|
Hi,
Wouldn't something like Cells(YourRow,YourColumn).Select work?
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
Have a look Developers Guide to the Excel 2007 Application Object[^], most of it is in VB.NET, but that's just syntax ....
I'm sure that you'll find something useful there ....
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
Is this going to be the same excel file you'll be opening every time? You could put an 'AutoOpen' macro in the excel file (which would run anytime the file is opened, just enable macros when you open it) to do all this preliminary stuff you're doing in C#. It would be much easier in VBA anyway.
The Code Demon Rises.
|
|
|
|
|
This should do it: Range.Activate Method[^] (Activates a single cell, which must be inside the current selection. To select a range of cells, use the Select method.)
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
Hi guys!
Maybe someone can help me with this:
I have a library C which controls a device and it's methods take enumerations as parameters.
Then I have a library B which references C and "wraps" the complex commands from library C in something useful and maintainable.
The last thing I have is an assembly A which does the UI stuff. It references ONLY library B.
Nevertheless B needs the values of the enumerations of C to control the device (and to fill comboboxes with the enum-Values)
I need a way to pass the information, which value of an enumeration is needed from A to B.
I do not want to reference C in A (that's how I would get the enumerations as well).
Which would be a good approach to solve this problem.
Help would really be appreciated!
|
|
|
|
|
You could just copy 'n paste the enum .. but that makes it harder to add/change values later (there are probably better solutions..)
|
|
|
|
|
Why not reference C in A? If B references C and A references B, then you have to distribute C with application A anyway. If you want A to have access to the values of the enumeration, referencing C is the 'right' and easiest way to go about it.
|
|
|
|
|
Thanks for your reply. I just hoped it would be possible to separate the C2 logic from the UI with the lib B as link.
|
|
|
|
|
I would put the enum in A; but you could put the enum in D.
|
|
|
|
|
Thanks for the hint, unfortunately I can't do this due to he fact, that lib C wasn't written by me.
|
|
|
|
|
Then write your own in B, and then translate.
|
|
|
|