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

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

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

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

Q&A

解決済

1回答

2478閲覧

書き方はどちらがよいでしょうか

4477

総合スコア91

C#

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

0グッド

0クリップ

投稿2015/09/01 13:53

VisualStudioを使わないでC#を書くのは非効率、変なことなのかもしれませんが、Windowsフォームアプリケーションを手書きで1から書くとしたら、次のどちらの書き方がよいでしょうか。もしくは、普通はもっと違う書き方をするものでしょうか。

■書き方 A

C#

1public class TestApp 2{ 3 public static void Main() 4 { 5 System.Windows.Forms.Form f = new System.Windows.Forms.Form(); 6 f.Width = 400; 7 f.Height = 300; 8 f.Text = "おみくじソフト"; 9 10 System.Windows.Forms.Label a = new System.Windows.Forms.Label(); 11 a.Width = 400; 12 a.Height = 100; 13 a.Text = "ボタンをクリックしてください"; 14 a.Name = "a"; 15 f.Controls.Add(a); 16 17 OmikujiButton b = new OmikujiButton(); 18 b.Text = "おみくじをひく"; 19 b.Location = new System.Drawing.Point(0, 150); 20 f.Controls.Add(b); 21 22 System.Windows.Forms.Application.Run(f); 23 } 24} 25 26public class OmikujiButton : System.Windows.Forms.Button 27{ 28 protected override void OnClick(System.EventArgs e) 29 { 30 System.Random rnd = new System.Random(); 31 int n = rnd.Next(0, 4); 32 33 System.String m; 34 if (n == 1) { 35 m = "大吉"; 36 } else if (n == 2) { 37 m = "小吉"; 38 } else if (n == 3) { 39 m = "吉"; 40 } else { 41 m = "凶"; 42 } 43 44 System.Windows.Forms.Form f = this.FindForm(); 45 System.Windows.Forms.Control a = f.Controls.Find("a", false)[0]; 46 a.Text = "今日の運勢は " + m; 47 } 48}

■書き方 B

C#

1public class TestApp 2{ 3 static System.Windows.Forms.Form f; 4 static System.Windows.Forms.Label a; 5 static System.Windows.Forms.Button b; 6 7 public static void Main() 8 { 9 f = new System.Windows.Forms.Form(); 10 f.Width = 400; 11 f.Height = 300; 12 f.Text = "おみくじソフト"; 13 14 a = new System.Windows.Forms.Label(); 15 a.Width = 400; 16 a.Height = 100; 17 a.Text = "ボタンをクリックしてください"; 18 a.Name = "a"; 19 f.Controls.Add(a); 20 21 b = new System.Windows.Forms.Button(); 22 b.Click += new System.EventHandler(TestApp.b_Click); 23 b.Text = "おみくじをひく"; 24 b.Location = new System.Drawing.Point(0, 150); 25 f.Controls.Add(b); 26 27 System.Windows.Forms.Application.Run(f); 28 } 29 30 public static void b_Click(object sender, System.EventArgs e) 31 { 32 System.Random rnd = new System.Random(); 33 int n = rnd.Next(0, 4); 34 35 System.String m; 36 if (n == 1) { 37 m = "大吉"; 38 } else if (n == 2) { 39 m = "小吉"; 40 } else if (n == 3) { 41 m = "吉"; 42 } else { 43 m = "凶"; 44 } 45 46 a.Text = "今日の運勢は " + m; 47 } 48}

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

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

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

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

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

guest

回答1

0

ベストアンサー

変な事ではありません。
統合開発環境は学習の敵だと思ってます。

こんな感じですかね。。。
結局VSで見慣れている形に近づいていく気がする・・・

C#

1public class TestApp 2{ 3 public static void Main() 4 { 5 System.Windows.Forms.Application.Run(new MainForm()); 6 } 7} 8 9public class MainForm : System.Windows.Forms.Form 10{ 11 private System.Windows.Forms.Label a; 12 private System.Windows.Forms.Button b; 13 14 public MainForm() 15 { 16 this.Width = 400; 17 this.Height = 300; 18 this.Text = "おみくじソフト"; 19 20 a = new System.Windows.Forms.Label(); 21 a.Width = 400; 22 a.Height = 100; 23 a.Text = "ボタンをクリックしてください"; 24 a.Name = "a"; 25 this.Controls.Add(a); 26 27 b = new System.Windows.Forms.Button(); 28 b.Text = "おみくじをひく"; 29 b.Location = new System.Drawing.Point(0, 150); 30 b.Click += b_Click; 31 this.Controls.Add(b); 32 33 } 34 35 private void b_Click(object btn, System.EventArgs e) 36 { 37 System.Random rnd = new System.Random(); 38 int n = rnd.Next(0, 4); 39 40 System.String m; 41 if (n == 1) 42 { 43 m = "大吉"; 44 } 45 else if (n == 2) 46 { 47 m = "小吉"; 48 } 49 else if (n == 3) 50 { 51 m = "吉"; 52 } 53 else 54 { 55 m = "凶"; 56 } 57 58 a.Text = "今日の運勢は " + m; 59 } 60 61}

投稿2015/09/01 14:21

編集2015/09/01 14:22
wakuwaku

総合スコア386

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

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

sho_cs

2015/09/01 14:39

私も書くとしたら同じようになります。 C#の場合、IDEとの密な連携も言語の特徴であると思うので敵とは思いませんが、IDEなしでもかけたほうがいいとは思います。
4477

2015/09/01 23:46

フォームを作って、フォームにボタンなどを持たせるのが、確かにいい形だなと思いました。 自分が書いた例だと、やりたいことが増えたら都度 XxxButton : Button なのか、メインのクラスがボタンやフォームを持つのも違和感があるし…と思ってました。VSを使わずにいきなりC#を始めてみまして、どこまでいけるかなと試しているところです。大変助かりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問