実現したいこと
Form2.Designer.csで表示位置やサイズを設定した、RichTextBoxをForm1でAppendTextメソッドなどで操作をしたい。
発生している問題・分からないこと
文字列をRichTextBoxにAppendTextで追加し、UI上に表示たいのですが、UI上に文字列に表示されない。
該当のソースコード
C#
1//新しく、Form1.csの中身を変えました。 2//変更はForm2からClassAになったこと、FormではなくUserControlを継承する 3//ようにしたことに因るものです。 4//void ShowForm2Modal()を削除しました。 5//Form1.cs 6using System.Diagnostics; 7using System.Windows.Forms; 8namespace NewApp 9{ 10 public partial class Form1 : Form 11 { 12 private ClassA _classA; 13 public Form1(ClassA classA) 14 { 15 _classA = classA; 16 InitializeComponent(); 17 18 B(); 19 } 20 private void B() 21 { 22 RichTextBox richTextBox = _classA.A(); 23 richTextBox.AppendText("void B"); 24 Debug.WriteLine("richTextBox:" + richTextBox); 25 } 26 27 } 28} 29 30 31 32
C#
1//新しく、Fomrs.csの中身を変えました。ファイル名は、そのままにしました。 2//Form2.cs 3using System; 4using System.Diagnostics; 5using System.Windows.Forms; 6using static System.Net.Mime.MediaTypeNames; 7 8namespace NewApp 9{ 10 public partial class ClassA : UserControl 11 public ClassA() 12 { 13 InitializeComponent(); 14 } 15 public RichTextBox A() 16 { 17 return richTextBox; 18 } 19 } 20} 21
C#
1//旧Form1.cs 2using System.Diagnostics; 3using System.Windows.Forms; 4namespace NewApp 5{ 6 public partial class Form1 : Form 7 { 8 private Form2 _form2; 9 public Form1(Form2 form2) 10 { 11 _form2 = form2; 12 InitializeComponent(); 13 14 B(); 15 } 16 private void ShowForm2Modal() 17 { 18 Form2 form2 = new Form2(); 19 form2.ShowDialog(); // Form2をモーダルダイアログとして表示 20 } 21 private void B() 22 { 23 RichTextBox richTextBox = _form2.A(); 24 richTextBox.AppendText("void B"); 25 Debug.WriteLine("richTextBox:" + richTextBox); 26 } 27 28 } 29} 30
C#
1//Fomr1Designer.cs 2namespace NewApp 3{ 4 partial class Form1 5 { 6 /// <summary> 7 /// Required designer variable. 8 /// </summary> 9 private System.ComponentModel.IContainer components = null; 10 11 /// <summary> 12 /// Clean up any resources being used. 13 /// </summary> 14 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 15 protected override void Dispose(bool disposing) 16 { 17 if (disposing && (components != null)) 18 { 19 components.Dispose(); 20 } 21 base.Dispose(disposing); 22 } 23 24 #region Windows Form Designer generated code 25 private void InitializeComponent() 26 { 27 AutoScaleDimensions = new SizeF(7F, 15F); 28 AutoScaleMode = AutoScaleMode.Font; 29 ClientSize = new Size(800, 450); 30 Name = "Form1"; 31 Text = "Form1"; 32 ResumeLayout(false); 33 } 34 35 #endregion 36 37 } 38} 39
C#
1//旧Form2.cs 2using System; 3using System.Diagnostics; 4 5namespace NewApp 6{ 7 public partial class Form2 : Form 8 { 9 public Form2() 10 { 11 InitializeComponent(); 12 13 14 15 richTextBox.AppendText("Test"); 16 17 } 18 private void Form2_Load(object sender, EventArgs e) 19 { 20 Debug.WriteLine("TEst"); 21 } 22 23 public RichTextBox A() 24 { 25 return richTextBox; 26 } 27 } 28} 29
C#
1//Form2.Designer.cs 2using System; 3using System.Windows.Forms; 4namespace NewApp 5{ 6 partial class Form2 7 { 8 private System.ComponentModel.IContainer components = null; 9 10 protected override void Dispose(bool disposing) 11 { 12 if (disposing && (components != null)) 13 { 14 components.Dispose(); 15 } 16 base.Dispose(disposing); 17 } 18 #region Windows Form Designer generated code 19 private void InitializeComponent() 20 { 21 22 // 23 // Form2 24 // 25 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 26 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 27 this.ClientSize = new System.Drawing.Size(284, 261); 28 29 this.Name = "Form2"; 30 this.Text = "Form2"; 31 this.ResumeLayout(false); 32 this.PerformLayout(); 33 34 35 this.richTextBox = new System.Windows.Forms.RichTextBox(); 36 this.TabPagerich = new System.Windows.Forms.TabPage(); 37 38 this.richTextBox.Dock = System.Windows.Forms.DockStyle.Fill; 39 this.richTextBox.Location = new System.Drawing.Point(315, 2); 40 41 this.richTextBox.Size = new System.Drawing.Size(386, 268); 42 this.richTextBox.TabIndex = 0; 43 //this.richTextBox.Text = "1tteme"; 44 45 //Tab? 46 //this.TabPagerich.Controls.Add(this.TabPagerich); 47 this.TabPagerich.Controls.Add(this.richTextBox); 48 49 50 this.TabPagerich.Location = new System.Drawing.Point(4, 22); 51 this.TabPagerich.Name = "TabPagerich"; 52 this.TabPagerich.Padding = new System.Windows.Forms.Padding(3); 53 this.TabPagerich.Size = new System.Drawing.Size(392, 274); 54 this.TabPagerich.TabIndex = 0; 55 this.TabPagerich.Text = "RichText"; 56 this.TabPagerich.UseVisualStyleBackColor = true; 57 //TabControl?? 58 this.tabControl1 = new System.Windows.Forms.TabControl(); 59 this.tabControl1.Controls.Add(this.TabPagerich); 60 this.tabControl1.Location = new System.Drawing.Point(12, 12); 61 this.tabControl1.Name = "tabControl1"; 62 this.tabControl1.SelectedIndex = 0; 63 this.tabControl1.Size = new System.Drawing.Size(400, 300); 64 this.tabControl1.TabIndex = 0; 65 66 67 this.tabControl1.ResumeLayout(false); 68 this.Controls.Add(this.tabControl1); 69 70 71 } 72 73 74 75 76 private System.Windows.Forms.RichTextBox richTextBox; 77 private System.Windows.Forms.TabPage TabPagerich; 78 private System.Windows.Forms.TabControl tabControl1; 79 #endregion 80 81 } 82 83} 84 85
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
Debug.WriteLine("richTextBox:" + richTextBox);
とすると、richTextBox:System.Windows.Forms.RichTextBox, Text: Testvoid Bというように出力され、AppednTextが働いていることが見て取れます。
補足
特になし
回答1件
あなたの回答
tips
プレビュー