Click here to Skip to main content
15,914,594 members
Home / Discussions / C#
   

C#

 
QuestionHow do I bind TreeView Selected Item? Pin
User 1459901922-Oct-19 19:52
User 1459901922-Oct-19 19:52 
AnswerRe: How do I bind TreeView Selected Item? Pin
Mc_Topaz24-Oct-19 21:00
Mc_Topaz24-Oct-19 21:00 
QuestionWhy does the Form_Paint event run continuously and only draw once ? Pin
Member 245846722-Oct-19 15:53
Member 245846722-Oct-19 15:53 
AnswerRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Dave Kreskowiak22-Oct-19 17:56
mveDave Kreskowiak22-Oct-19 17:56 
GeneralRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Member 245846723-Oct-19 22:09
Member 245846723-Oct-19 22:09 
GeneralRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Dave Kreskowiak24-Oct-19 4:33
mveDave Kreskowiak24-Oct-19 4:33 
GeneralRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Member 245846724-Oct-19 16:59
Member 245846724-Oct-19 16:59 
GeneralRe: Why does the Form_Paint event run continuously and only draw once ? Pin
Dave Kreskowiak24-Oct-19 18:04
mveDave Kreskowiak24-Oct-19 18: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:
C#
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.
C#
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 0:45
Member 1461982622-Oct-19 0:45 
GeneralRe: C # Pin
OriginalGriff22-Oct-19 0:57
mveOriginalGriff22-Oct-19 0:57 
QuestionRe: C # Pin
ZurdoDev22-Oct-19 2:59
professionalZurdoDev22-Oct-19 2:59 
AnswerRe: C # Pin
OriginalGriff22-Oct-19 3:32
mveOriginalGriff22-Oct-19 3:32 
GeneralRe: C # Pin
ZurdoDev22-Oct-19 4:04
professionalZurdoDev22-Oct-19 4:04 
GeneralRe: C # Pin
Dave Kreskowiak22-Oct-19 6:07
mveDave Kreskowiak22-Oct-19 6:07 
GeneralRe: C # Pin
Luc Pattyn22-Oct-19 10:23
sitebuilderLuc Pattyn22-Oct-19 10:23 
GeneralRe: C # Pin
Richard Deeming23-Oct-19 1:25
mveRichard Deeming23-Oct-19 1:25 
GeneralRe: C # Pin
Luc Pattyn23-Oct-19 2:56
sitebuilderLuc Pattyn23-Oct-19 2:56 
QuestionLDAP query to ActiveDirectory being whimsical (search by custom attribute broken) Pin
Super Lloyd21-Oct-19 21:30
Super Lloyd21-Oct-19 21:30 
SuggestionRe: LDAP query to ActiveDirectory being whimsical (search by custom attribute broken) Pin
Richard MacCutchan21-Oct-19 22:41
mveRichard MacCutchan21-Oct-19 22:41 
GeneralRe: LDAP query to ActiveDirectory being whimsical (search by custom attribute broken) Pin
Super Lloyd22-Oct-19 1:07
Super Lloyd22-Oct-19 1:07 
GeneralRe: LDAP query to ActiveDirectory being whimsical (search by custom attribute broken) Pin
Richard MacCutchan22-Oct-19 1:31
mveRichard MacCutchan22-Oct-19 1:31 
Questiona WinForm ToolStripMenuItem quirk ? Pin
BillWoodruff21-Oct-19 4:26
professionalBillWoodruff21-Oct-19 4:26 
AnswerRe: a WinForm ToolStripMenuItem quirk ? Pin
Luc Pattyn21-Oct-19 9:14
sitebuilderLuc Pattyn21-Oct-19 9:14 
AnswerRe: a WinForm ToolStripMenuItem quirk ? Pin
Richard Deeming22-Oct-19 0:41
mveRichard Deeming22-Oct-19 0:41 
GeneralRe: a WinForm ToolStripMenuItem quirk ? Pin
BillWoodruff22-Oct-19 0:48
professionalBillWoodruff22-Oct-19 0: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.