Click here to Skip to main content
15,880,651 members
Home / Discussions / C#
   

C#

 
AnswerRe: 'out' keyword, to use or not to use Pin
Ian A Davidson20-Apr-16 0:09
Ian A Davidson20-Apr-16 0:09 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill20-Apr-16 4:34
professionalFoothill20-Apr-16 4:34 
QuestionWhile button is clicked Pin
tvks19-Apr-16 8:30
tvks19-Apr-16 8:30 
AnswerRe: While button is clicked Pin
clapclap19-Apr-16 8:32
clapclap19-Apr-16 8:32 
AnswerRe: While button is clicked Pin
koolprasad200319-Apr-16 18:26
professionalkoolprasad200319-Apr-16 18:26 
GeneralRe: While button is clicked Pin
CHill6020-Apr-16 1:25
mveCHill6020-Apr-16 1:25 
GeneralRe: While button is clicked Pin
koolprasad200320-Apr-16 1:33
professionalkoolprasad200320-Apr-16 1:33 
AnswerRe: While button is clicked Pin
CHill6020-Apr-16 2:14
mveCHill6020-Apr-16 2:14 
Don't use a Timer. Use the methods on the Button MouseDown to start the update and MouseUp to stop the update.

You should probably do the updating on a separate Thread ... this CodeProject article can be adapted to do what you want - Simple Threading[^]

For example I have a button btnDemo - when I hold it "down" with my mouse a label lblDemo will get a number displayed. When I release the button then the label will get the text "stopped"...

Here is the code that handles the mouseclicks on the button
C#
private void btnDemo_MouseDown(object sender, MouseEventArgs e)
{
    demoThread = new Thread(ThreadProcSafe);
    btnDemo.Text = "Stop Demo";
    demoThread.Start();
}

private void btnDemo_MouseUp(object sender, MouseEventArgs e)
{
    if (!stopDemo)
    {
        stopDemo = true;
        if (!demoThread.Join(1000))
        {
            demoThread.Abort();
        }
        SetText("Stopped");
        btnDemo.Text = "Start Demo";
    }
}
And here is the code that actually sets up the thread and SetText functons (with some very basic error handling). These are the bits you need to change to put your message in your TextBox
C#
delegate void SetTextCallback(string text);
private Thread demoThread;
private volatile bool stopDemo;

private void ThreadProcSafe()
{
    var x = 0;
    while (!stopDemo)
    {
        SetText((x++).ToString());
        Thread.Sleep(600);
    }
}
private void SetText(string text)
{
    try
    {
        if (lbl_right.Disposing) return;

        if (lblDemo.InvokeRequired)
        {
            var d = new SetTextCallback(SetText);
            BeginInvoke(d, text);
        }
        else
            lblDemo.Text = text;
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
}

If you read the article it points out that sometimes the threads won't close properly when you attempt to close the application. Because we have the thread starting on a mouse down and ending on the mouse-up this is less likely but still worth adding this method into the form -
C#
protected override void OnClosed(EventArgs e)
{
    stopDemo = true;

    if (!demoThread.Join(1000))
    {
        demoThread.Abort();
    }
    base.OnClosed(e);
}

PraiseRe: While button is clicked Pin
tvks20-Apr-16 10:57
tvks20-Apr-16 10:57 
GeneralRe: While button is clicked Pin
CHill6020-Apr-16 11:01
mveCHill6020-Apr-16 11:01 
GeneralRe: While button is clicked Pin
tvks27-Apr-16 2:55
tvks27-Apr-16 2:55 
QuestionReflection and "security" Pin
Super Lloyd19-Apr-16 7:17
Super Lloyd19-Apr-16 7:17 
QuestionLinkButton isnt recognized properly Pin
Collectoons19-Apr-16 4:19
Collectoons19-Apr-16 4:19 
SuggestionRe: LinkButton isnt recognized properly Pin
Richard MacCutchan19-Apr-16 5:09
mveRichard MacCutchan19-Apr-16 5:09 
GeneralRe: LinkButton isnt recognized properly Pin
Collectoons19-Apr-16 5:16
Collectoons19-Apr-16 5:16 
GeneralRe: LinkButton isnt recognized properly Pin
Richard MacCutchan19-Apr-16 5:26
mveRichard MacCutchan19-Apr-16 5:26 
AnswerRe: LinkButton isnt recognized properly Pin
Richard Deeming19-Apr-16 5:56
mveRichard Deeming19-Apr-16 5:56 
GeneralRe: LinkButton isnt recognized properly Pin
Collectoons19-Apr-16 6:03
Collectoons19-Apr-16 6:03 
GeneralRe: LinkButton isnt recognized properly Pin
Richard Deeming19-Apr-16 6:06
mveRichard Deeming19-Apr-16 6:06 
GeneralRe: LinkButton isnt recognized properly Pin
Collectoons19-Apr-16 6:08
Collectoons19-Apr-16 6:08 
QuestionError Starting Windows Service Pin
Member 1098552419-Apr-16 2:52
Member 1098552419-Apr-16 2:52 
AnswerRe: Error Starting Windows Service Pin
Eddy Vluggen19-Apr-16 2:57
professionalEddy Vluggen19-Apr-16 2:57 
QuestionSocket Example UWP Pin
Miwin Solutions19-Apr-16 1:13
Miwin Solutions19-Apr-16 1:13 
AnswerRe: Socket Example UWP Pin
Jochen Arndt19-Apr-16 2:18
professionalJochen Arndt19-Apr-16 2:18 
GeneralRe: Socket Example UWP Pin
Miwin Solutions20-Apr-16 2:16
Miwin Solutions20-Apr-16 2:16 

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.