WindowsFormのメインフォーム内で別のフォームのインスタンスを作成してShow
メソッドを呼び出してしまうと、メインフォームにフォーカスを戻すことができません。
ログウィンドウとして常にサブフォームを出しておきたいといった場合、どのようにすれば良いでしょうか。
重い処理の最中にそのサブウィンドウ内のTextBoxコントロールのTextプロパティを更新していくといった使い方をしたいと思っています。
解決済み
C#
1 LoggingForm loggingForm = new LoggingForm(this); 2 loggingForm .Show();
環境
Windows10
CPU Ryzen7 3700
RAM 54GB
GPU RTX2080
MB MSI X570 GAMING PLUS
IDE: Rider EAP
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/22 23:23
2020/01/22 23:30
退会済みユーザー
2020/01/22 23:36
回答4件
0
重い処理の最中にそのサブウィンドウ内のTextBoxコントロールのTextプロパティを更新していくといった使い方をしたいと思っています。
データバインドを使います。
WindowsFormのメインフォーム内で別のフォームのインスタンスを作成してShowメソッドを呼び出してしまうと、メインフォームにフォーカスを戻すことができません。
状況がわかりません。Showメソッドに親フォームを渡せば、子フォームが必ず前になりますが、フォーカスは移ります。もしかして、重い処理の最中でしょうか。
15分考えてわからないから聞くのは良いですが、質問文は1時間以上推敲しながら作ってください。
投稿2020/01/22 23:38
総合スコア995
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/22 23:48
2020/01/23 00:01
2020/01/23 00:11
2020/01/23 06:19
0
自己解決
各所で紹介されているコードと同じようなコードなのに変だと思い、RiderではなくVisualStudioでソリューションを開いたら期待した動作になりました。
お騒がせしました。
投稿2020/01/22 23:35
総合スコア217
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/22 23:44
2020/01/22 23:50
2020/01/22 23:57
2020/01/23 00:06
退会済みユーザー
2020/01/23 00:17 編集
2020/01/23 00:19
2020/01/23 00:19
2020/01/23 00:21
2020/01/23 00:22
退会済みユーザー
2020/01/23 00:24 編集
2020/01/23 00:25
2020/01/23 00:26
2020/01/23 00:31
退会済みユーザー
2020/01/23 00:48
2020/01/23 00:49 編集
2020/01/23 00:54
2020/01/23 01:01
2020/01/23 01:04
0
新しいフォームを表示するを参照してください。
投稿2020/01/22 22:09
総合スコア10222
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
解決済みですが。
気になったので検証してみましたが再現しませんでした。
Riderで作成したソリューションはVisualStudio2019でそのまま読み込み可能なので
Rider/VisualStudio2019の差異で発生しているとは考えにくいですね。
JetBrains Rider 2019.3.1
Build #RD-193.5662.14, built on December 20, 2019
を入れて検証してみましたが以下で問題なくmodeless表示になってました。
※ RiderメニューからのRun/DebugおよびBuild後WinFormsApp1.exeをExplorerから実行
- 空ソリューション作成
- Project追加で.Net Core Desktop Application追加。(WinFormsApp1)
Target Frameworkはnetcoreapp3.1 - Form2を追加。
- Form1のMouseDoubleClickイベントを追加し、Form2をインスタンス化後、Show MethodをCallする
- Form1.cs
C#
1namespace WinFormsApp1 2{ 3 public partial class Form1 : Form 4 { 5 public Form1() 6 { 7 InitializeComponent(); 8 } 9 10 private void Form1_MouseDoubleClick(object sender, MouseEventArgs e) 11 { 12 Form2 frm = new Form2(); 13 frm.Show(); 14 } 15 }
- Form1.cs
C#
1namespace WinFormsApp1 2{ 3 public partial class Form2 : Form 4 { 5 public Form2() 6 { 7 InitializeComponent(); 8 } 9 } 10}
- Form1.Designer.cs
C#
1namespace WinFormsApp1 2{ 3 partial class Form1 4 { 5 /// <summary> 6 /// Required designer variable. 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// Clean up any resources being used. 12 /// </summary> 13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 21 base.Dispose(disposing); 22 } 23 24 #region Windows Form Designer generated code 25 26 /// <summary> 27 /// Required method for Designer support - do not modify 28 /// the contents of this method with the code editor. 29 /// </summary> 30 private void InitializeComponent() 31 { 32 this.SuspendLayout(); 33 // 34 // Form1 35 // 36 this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); 37 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 38 this.ClientSize = new System.Drawing.Size(933, 562); 39 this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 40 this.Name = "Form1"; 41 this.Text = "Form1"; 42 this.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDoubleClick); 43 this.ResumeLayout(false); 44 } 45 46 #endregion 47 }
- Form2.Designer.cs
C#
1namespace WinFormsApp1 2{ 3 partial class Form2 4 { 5 /// <summary> 6 /// Required designer variable. 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// Clean up any resources being used. 12 /// </summary> 13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 21 base.Dispose(disposing); 22 } 23 24 #region Windows Form Designer generated code 25 26 /// <summary> 27 /// Required method for Designer support - do not modify 28 /// the contents of this method with the code editor. 29 /// </summary> 30 private void InitializeComponent() 31 { 32 this.components = new System.ComponentModel.Container(); 33 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 34 this.ClientSize = new System.Drawing.Size(800, 450); 35 this.Text = "Form2"; 36 } 37 38 #endregion 39 } 40}
投稿2020/01/23 01:54
編集2020/01/23 02:20総合スコア7914
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/23 02:14
2020/01/23 02:16
2020/01/23 02:19
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。