Label animation inside a switch case c# -


on face of it seems quite simple. have switch case , if condition met print text label animation. in case type writer animation.

i have made animation cant seem integrate similar version switch case itself. help?

type writer animation code c#:

public partial class form1 : form {     int _charindex = 0;     string _text = "this test.";      public form1()     {         initializecomponent();     }      private void form1_load(object sender, eventargs e)     {         _charindex = 0;         label1.text = string.empty;         thread t = new thread(new threadstart(this.typewritetext));         t.start();     }       private void typewritetext()     {         while (_charindex < _text.length)         {             thread.sleep(50);             label1.invoke(new action(() =>             {                 label1.text += _text[_charindex];             }));             _charindex++;         }     }  } 

}

and animation code needs placed this:

switch case code:

        void testengine(object sender, speechrecognizedeventargs e)     {         switch (e.result.text)         {           case "test":           //label animation code goes here           break; 

thanks in advance!

short answer - move code in method , call anywhere want.
long answer
while works, doesn't make sense because worker thread sleeping , calling ui thread. system.windows.forms.timer based approach appropriate concrete case. "modern" approach based on async/await. if need flexibility, last 1 best choice. whatever choose, you'll hit reentrancy problem @ point , need handle it. best prepare helper utility class , use anywhere. here example:

using system; using system.drawing; using system.threading; using system.threading.tasks; using system.windows.forms;  namespace tests {     static class program     {         [stathread]         static void main()         {             application.enablevisualstyles();             application.setcompatibletextrenderingdefault(false);             application.run(new testform());         }          class testform : form         {             public testform()             {                 var label = new label { parent = this, autosize = true, top = 8, left = 8 };                 animatehelper = new animatehelper(label);                 int left = 8;                 foreach (var action in (buttonaction[])enum.getvalues(typeof(buttonaction)))                 {                     var button = new button { parent = this, autosize = true, text = action.tostring(), left = left };                     button.top = displayrectangle.bottom - button.height - 8;                     button.click += (sender, e) => execute(action);                     left += button.width + 8;                 }             }             protected override void dispose(bool disposing)             {                 if (disposing && animatehelper != null) animatehelper.cancel();                  base.dispose(disposing);             }             enum buttonaction { typewritetext, repeattext, cancel }             private void execute(buttonaction action)             {                 // original question                 switch (action)                 {                     case buttonaction.typewritetext:                         typewritetext("this typewriter text animantion test.");                         break;                     case buttonaction.repeattext:                         repeattext("this repeating text animantion test.");                         break;                     case buttonaction.cancel:                         animatehelper.cancel();                         break;                 }             }             animatehelper animatehelper;             void typewritetext(string text)             {                 animatehelper.execute(async (output, ct) =>                 {                     bool clear = true;                     try                     {                         if (string.isnullorempty(text)) return;                         output.forecolor = color.blue;                         (int length = 1; ; length++)                         {                             if (ct.iscancellationrequested) return;                             output.text = text.substring(0, length);                             if (length == text.length) break;                             await task.delay(50, ct);                         }                         clear = false;                     }                     { if (clear) output.text = string.empty; }                 });             }             void repeattext(string text)             {                 animatehelper.execute(async (output, ct) =>                 {                     try                     {                         if (string.isnullorempty(text)) return;                         output.forecolor = color.red;                         while (true)                         {                             (int length = 1; length <= text.length; length++)                             {                                 if (ct.iscancellationrequested) return;                                 output.text = text.substring(text.length - length);                                 await task.delay(50, ct);                             }                             (int pad = 1; pad < text.length; pad++)                             {                                 if (ct.iscancellationrequested) return;                                 output.text = new string(' ', pad) + text.substring(0, text.length - pad);                                 await task.delay(50, ct);                             }                             if (ct.iscancellationrequested) return;                             output.text = string.empty;                             await task.delay(250, ct);                         }                     }                     { output.text = string.empty; }                 });             }         }          class animatehelper         {             label output;             task task;             cancellationtokensource cts;             public animatehelper(label output) { this.output = output; }             void reset()             {                 if (cts != null) { cts.dispose(); cts = null; }                 task = null;             }             public void cancel() { dontcare(cancelasync()); }             async task cancelasync()             {                 if (task != null && !task.iscompleted)                 {                     try { cts.cancel(); } catch { }                     try { await task; } catch { }                 }                 reset();             }             public void execute(func<label, cancellationtoken, task> action) { dontcare(executeasync(action)); }             async task executeasync(func<label, cancellationtoken, task> action)             {                 await cancelasync();                 cts = new cancellationtokensource();                 task = action(output, cts.token);                 try { await task; } catch { }                 reset();             }             // make compiler happy             static void dontcare(task t) { }         }     } } 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -