質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

3回答

7205閲覧

C# 子formで親formの変数を取得したい

n.k8808

総合スコア25

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

1クリップ

投稿2017/05/03 10:17

編集2022/01/12 10:55

子formで親formの変数を取得したいです
(と言う名前は存在しません)と出てきます

どうすればいいですか
お願いします。

form1

C#

1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10 11namespace WindowsFormsApp1 12{ 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void button1_Click(object sender, EventArgs e) 21 { 22 string n1 = textBox1.Text; 23 } 24 } 25}

C#

1namespace WindowsFormsApp1 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 base.Dispose(disposing); 21 } 22 23 #region Windows Form Designer generated code 24 25 /// <summary> 26 /// Required method for Designer support - do not modify 27 /// the contents of this method with the code editor. 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.textBox1 = new System.Windows.Forms.TextBox(); 32 this.button1 = new System.Windows.Forms.Button(); 33 this.SuspendLayout(); 34 // 35 // textBox1 36 // 37 this.textBox1.Location = new System.Drawing.Point(1, 0); 38 this.textBox1.Name = "textBox1"; 39 this.textBox1.Size = new System.Drawing.Size(100, 19); 40 this.textBox1.TabIndex = 0; 41 // 42 // button1 43 // 44 this.button1.Location = new System.Drawing.Point(108, 0); 45 this.button1.Name = "button1"; 46 this.button1.Size = new System.Drawing.Size(75, 23); 47 this.button1.TabIndex = 1; 48 this.button1.Text = "button1"; 49 this.button1.UseVisualStyleBackColor = true; 50 this.button1.Click += new System.EventHandler(this.button1_Click); 51 // 52 // Form1 53 // 54 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 55 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 56 this.ClientSize = new System.Drawing.Size(284, 261); 57 this.Controls.Add(this.button1); 58 this.Controls.Add(this.textBox1); 59 this.Name = "Form1"; 60 this.Text = "Form1"; 61 this.ResumeLayout(false); 62 this.PerformLayout(); 63 64 } 65 66 #endregion 67 68 private System.Windows.Forms.TextBox textBox1; 69 private System.Windows.Forms.Button button1; 70 } 71}

form2

C#

1// 2 // label1 3 // 4 this.label1.AutoSize = true; 5 this.label1.Location = new System.Drawing.Point(1, 1); 6 this.label1.Name = "label1"; 7 this.label1.Size = new System.Drawing.Size(35, 12); 8 this.label1.TabIndex = 0; 9 this.label1.Text = n1;

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

情報が少ないですが、インスタンスフィールドにクラス名経由でアクセスしようとしている感じですかね。
親フォームが子フォームを生成するときに引数としてthisを渡すのがいいです。

投稿2017/05/03 10:31

yuba

総合スコア5568

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

thisを渡すのが一番らくだと思いますが、

データ渡し専用のクラスを作成してその中に変数を用意し
変数を渡す方法はいかがでしょうか?

投稿2017/05/08 08:23

dekky0910

総合スコア93

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

親フォームのインスタンスを渡してください。

投稿2017/05/03 10:31

Zuishin

総合スコア28656

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

n.k8808

2017/05/06 23:34

前回の質問でもありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問