Click here to Skip to main content
14,383,811 members
   

C#

 
QuestionHow do I bind TreeView Selected Item? Pin
JohNYC42022-Oct-19 20:52
memberJohNYC42022-Oct-19 20:52 
AnswerRe: How do I bind TreeView Selected Item? Pin
Mc_Topaz24-Oct-19 22:00
memberMc_Topaz24-Oct-19 22:00 
QuestionWhy does the Form_Paint event run continuously and only draw once ? Pin
Member 245846722-Oct-19 16:53
memberMember 245846722-Oct-19 16:53 
AnswerRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Dave Kreskowiak22-Oct-19 18:56
mveDave Kreskowiak22-Oct-19 18:56 
GeneralRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Member 245846723-Oct-19 23:09
memberMember 245846723-Oct-19 23:09 
GeneralRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Dave Kreskowiak24-Oct-19 5:33
mveDave Kreskowiak24-Oct-19 5:33 
GeneralRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Member 245846724-Oct-19 17:59
memberMember 245846724-Oct-19 17:59 
GeneralRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Dave Kreskowiak24-Oct-19 19:04
mveDave Kreskowiak24-Oct-19 19:04 
Member 2458467 wrote:
I use the label control to debug the program instead of the Debug.Print("..."), I think the label control doesn't affect as you say.


Actually, it does work as I say, and it WILL lie to you when you have the UI thread tied up doing other things instead of letting it process the message pump so forms and controls can repaint themselves.

Don't believe me? Start a new Windows form project and drop a label and a button on the form. Double click the button to create an event handler and drop the following code into the button handler:
for (int i = 1; i < 20000000; i++)
    label1.Text = i.ToString();

Run it and click the button. Notice the label doesn't update? Also, you can't move the form around the screen either. It's doing this because you've got the UI thread tied up doing a long-running operation. It's can't process messages, like WM_PAINT, until that operation completes.


Member 2458467 wrote:
you say: "i never use the value of the Random so it's never being painted anywhere." What do I use to generate random numbers ?

You use Random to generate the values, but you're not understanding how Random works. When you create a new instance of Random and do not supply it with a seed value, it will use the current timer value. Do this multiple times in a row quick enough, and each one of the Random instances will return the exact same value!

Create a single class-level instance of Random and you can use it throughout the rest of your class code and not have to worry about consecutive instances returning the same values.
public class Form1 : Form
{
    private Random RNG = new Random();
.
.
.
    private void SomeMethod()
    {
        ...
        int x = RNG.Next(10000);
        ...
    }
}

In your code, you're getting the next random value and putting it in a variable, but then you never use that value in your paint code!

Member 2458467 wrote:
you say: "Form_Paint is a bit much for this. I'd create a Control specifically for the purpose ..." I haven't seen you create a Control specifically posted by you for everyone to see ?

Because I've got my own code to write that nobody is going to write for me and I'm not in the business of writing other peoples code for them.

But, yeah, I wrote a tons of controls for specific very specific things. You'd be better served by writing a control to do this instead of baking it into the Form code where it cannot be used on other forms without copying all the code for it.

Putting the code in its own control allows you to easily reuse the functionality in other applications, other forms in the same app, and even multiple instances of the control on the same form, all without duplicating in multiple places.

Actually, for this, I'd probably just forget the custom painting, even though it's easy to do, it's a bit more code and there's stuff you have to worry about when painting. I'd probably just create a new class, inherit from Label, add a timer to it, a few methods to start and stop the timer, a few properties like Interval for the timer, and in the Tick event of the timer, just update the number being displayed and call Invalidate to get the control to repaint itself.

...no custom painting required.

GeneralC # Pin
Member 1461982622-Oct-19 1:45
memberMember 1461982622-Oct-19 1:45 
GeneralRe: C # Pin
OriginalGriff22-Oct-19 1:57
protectorOriginalGriff22-Oct-19 1:57 
QuestionRe: C # Pin
ZurdoDev22-Oct-19 3:59
professionalZurdoDev22-Oct-19 3:59 
AnswerRe: C # Pin
OriginalGriff22-Oct-19 4:32
protectorOriginalGriff22-Oct-19 4:32 
GeneralRe: C # Pin
ZurdoDev22-Oct-19 5:04
professionalZurdoDev22-Oct-19 5:04 
GeneralRe: C # Pin
Dave Kreskowiak22-Oct-19 7:07
mveDave Kreskowiak22-Oct-19 7:07 
GeneralRe: C # Pin
Luc Pattyn22-Oct-19 11:23
professionalLuc Pattyn22-Oct-19 11:23 
GeneralRe: C # Pin
Richard Deeming23-Oct-19 2:25
mveRichard Deeming23-Oct-19 2:25 
GeneralRe: C # Pin
Luc Pattyn23-Oct-19 3:56
professionalLuc Pattyn23-Oct-19 3:56 
QuestionLDAP query to ActiveDirectory being whimsical (search by custom attribute broken) Pin
Super Lloyd21-Oct-19 22:30
memberSuper Lloyd21-Oct-19 22:30 
SuggestionRe: LDAP query to ActiveDirectory being whimsical (search by custom attribute broken) Pin
Richard MacCutchan21-Oct-19 23:41
protectorRichard MacCutchan21-Oct-19 23:41 
GeneralRe: LDAP query to ActiveDirectory being whimsical (search by custom attribute broken) Pin
Super Lloyd22-Oct-19 2:07
memberSuper Lloyd22-Oct-19 2:07 
GeneralRe: LDAP query to ActiveDirectory being whimsical (search by custom attribute broken) Pin
Richard MacCutchan22-Oct-19 2:31
protectorRichard MacCutchan22-Oct-19 2:31 
Questiona WinForm ToolStripMenuItem quirk ? Pin
BillWoodruff21-Oct-19 5:26
mveBillWoodruff21-Oct-19 5:26 
AnswerRe: a WinForm ToolStripMenuItem quirk ? Pin
Luc Pattyn21-Oct-19 10:14
professionalLuc Pattyn21-Oct-19 10:14 
AnswerRe: a WinForm ToolStripMenuItem quirk ? Pin
Richard Deeming22-Oct-19 1:41
mveRichard Deeming22-Oct-19 1:41 
GeneralRe: a WinForm ToolStripMenuItem quirk ? Pin
BillWoodruff22-Oct-19 1:48
mveBillWoodruff22-Oct-19 1:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.