実現したいこと
FormAにあるスタートボタンを押したらデリゲートを作成し、500ms毎に値を1ずつカウントアップし、テキストボックスに表示出来るようにしたいです。
500ms毎に値をカウントアップさせる関数は、FormBから呼び出す仕様にしたいです。
発生している問題・分からないこと
プログラム内部ではFormBから関数が呼び出されていて、500ms毎に値がカウントアップされているところまでは出来ました。(Massagebox.show()を使って確認しました。)。
しかしFormAのテキストボックスには、初期値のみ表示されているだけで数値がカウントアップされていないです。(恐らく、表示を行う関数が1回しか呼ばれていない事が原因です。)。
InvokeをFormBの該当関数内に記載してみたりインスタンスを生成したりしたのですがエラーをはくだけで何も変わりませんでした。色々調べたりもしましたが、解決への糸口が見つけられずに困っています。
テキストボックスの値を変更させるためには、どのようにしたら良いかアドバイス頂けないでしょうか?
該当のソースコード
FormA
1using System; 2using System.CodeDom; 3using System.Collections.Generic; 4using System.ComponentModel; 5using System.ComponentModel.Design; 6using System.Data; 7using System.Diagnostics.Eventing.Reader; 8using System.Drawing; 9using System.Globalization; 10using System.Linq; 11using System.Runtime.ConstrainedExecution; 12using System.Runtime.InteropServices; 13using System.Security.Cryptography.X509Certificates; 14using System.Text; 15using System.Threading; 16using System.Threading.Tasks; 17using System.Windows.Forms; 18using System.Windows.Forms.DataVisualization.Charting; 19using System.Web; 20using static System.Windows.Forms.VisualStyles.VisualStyleElement; 21using System.Timers; 22using TextBox = System.Windows.Forms.TextBox; 23using Timer = System.Windows.Forms.Timer; 24using System.Runtime.InteropServices.WindowsRuntime; 25 26namespace TimeCount 27{ 28 public partial class FormA : Form 29 { 30 int value1 = 5; 31 int value2 = 0; 32 int value3 = 100; 33 34 public void Value1() { 35 value1++; 36 } 37 public void Value2() { 38 value2++; 39 } 40 public void Value3() 41 { 42 Value3++; 43 } 44 45 public System.Timers.Timer timer500ms = new System.Timers.Timer { AutoReset = true, Interval = 1000 }; 46 public delegate void DelegateProcess(); 47 public DelegateProcess timer500msDP; 48 49 public void ResultOutput(object sender, EventArgs e) 50 { 51 textBox1.Text = value1.Tostring(); 52 textBox2.Text = value2.Tostring(); 53 textBox3.Text = value3.Tostring(); 54 } 55 56 public void buttonStart(object sender, EventArgs e) 57 { 58 FormB fmB = new FormB(); 59 timer500msDP = new DelegateProcess(ResultOutput); 60 this.Invoke(timer500msDP); 61 timer500ms.Start(); 62 timer500ms.Elapsed += fmB.500ms; 63 } 64 65 private void buttonStop(object sender, EventArgs e) 66 { 67 timer1s.Stop(); 68 }
FormB
1using System; 2using System.CodeDom; 3using System.Collections.Generic; 4using System.ComponentModel; 5using System.ComponentModel.Design; 6using System.Data; 7using System.Diagnostics.Eventing.Reader; 8using System.Drawing; 9using System.Globalization; 10using System.Linq; 11using System.Runtime.ConstrainedExecution; 12using System.Runtime.InteropServices; 13using System.Security.Cryptography.X509Certificates; 14using System.Text; 15using System.Threading; 16using System.Threading.Tasks; 17using System.Windows.Forms; 18using System.Windows.Forms.DataVisualization.Charting; 19using static System.Windows.Forms.VisualStyles.VisualStyleElement; 20using System.Timers; 21using static CanTestTool_XlDrvLib.Form2; 22using Timer = System.Windows.Forms.Timer; 23using vxlapi_NET; 24 25namespace TimeCount 26{ 27 public partial class FormB : Form 28 { 29 FormA fmA = new FormA(); 30 31 public void 500ms(Object source, System.Timers.ElapsedEventArgs e) 32 { 33 fmA.Value1(); 34 fmA.Value2(); 35 fmA.Value3(); 36 } 37 } 38}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
InvokeやBeginInvokeを使用してみましたが、「ウィンドウハンドルが作成される前コントロールで Invoke または BeginInvoke を呼び出せません」のエラーが出て来ました。
その為、while文を使ってウィンドウハンドルが作成されるまでsleep(100)を繰り返して見ましたが、何も変わらなかったです。
補足
特になし

回答1件
あなたの回答
tips
プレビュー