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

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

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

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

C#

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

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

Q&A

解決済

2回答

1301閲覧

Windows10, Visual Studio 2019, C# .NET Core アプリで、Form の位置を表示させたい。

TAKASE_Hiroyuki

総合スコア21

Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

C#

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

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

1グッド

0クリップ

投稿2019/12/28 11:34

編集2019/12/28 13:44

#経緯

Windows10, Visual Studio 2019, C#, .NET Core で、Windows Forms App を作成しようと考えています。大変恐縮ですが、きわめて初心者です(申し訳ありません)。

#ソースコード

新しいプロジェクトの作成で、test01 というプロジェクトを、C:\hoga に作成しました。すると、Form1.cs Form1.Designer.cs Program.cs という3つのファイルができました。それぞれの内容は、以下に引用した通りです。この時点でビルドすると、特に問題なく Form が表示されます。

> Form1.cs

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 test01 12{ 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 } 21} 22

> Form1.Designer.cs

C#

1namespace test01 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.components = new System.ComponentModel.Container(); 32 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 33 this.ClientSize = new System.Drawing.Size(800, 450); 34 this.Text = "Form1"; 35 } 36 37 #endregion 38 } 39} 40

> Program.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Threading.Tasks; 5using System.Windows.Forms; 6 7namespace test01 8{ 9 static class Program 10 { 11 /// <summary> 12 /// The main entry point for the application. 13 /// </summary> 14 [STAThread] 15 static void Main() 16 { 17 Application.SetHighDpiMode(HighDpiMode.SystemAware); 18 Application.EnableVisualStyles(); 19 Application.SetCompatibleTextRenderingDefault(false); 20 Application.Run(new Form1()); 21 } 22 } 23}

#やってみたこと。ソースコード
さて、Formクラス は、名前空間:System.Windows.Forms に所属し、「アプリケーションのユーザーインターフェイスを構成するウィンドウまたはダイアログ ボックスを表します。」だそうです。Formクラスには、Form.Location プロパティがあって、画面座標における Form の左上隅を表す Point を取得することができます。上に引用したプログラムでは、Form1 という名前の Form がありますので、この Form1 の位置は、

C#

1int xxx, yyy; 2xxx = this.Location.X; 3yyy = this.Location.Y;

で取得できます。これを表示させるには、ラベルを使うのが良いだろうと考えました。そこで、 partial class Form1private void InitializeComponent() を次のように変更しました。

C#

1 private void InitializeComponent() 2 { 3 this.components = new System.ComponentModel.Container(); 4 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 5 this.ClientSize = new System.Drawing.Size(800, 450); 6 this.Text = "Form1"; 7 8 this.label1 = new System.Windows.Forms.Label(); 9 10 int xxx, yyy; 11 xxx = this.Location.X; 12 yyy = this.Location.Y; 13 string x1 = xxx.ToString("0"); 14 string y1 = yyy.ToString("0"); 15 16 this.label1.AutoSize = true; 17 this.label1.Location = new System.Drawing.Point(50, 150); 18 this.label1.Name = "label1"; 19 this.label1.Size = new System.Drawing.Size(100, 50); 20 this.label1.TabIndex = 1; 21 this.label1.Text = x1 + "," + y1; 22 23 }

#エラーメッセージ

ありません。しかし、上のラベルは表示されません。

#お願い

ラベルが表示されるようにするには、どうすればよいでしょうか。
御多用中、恐縮ですが、教えていただきますようお願い申し上げます。

#追記
上の引用中、
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

の部分を見落としていて、Form1.Designer.cs に手を加えてしまいました。しかし、だとすると、私が加筆した次の部分を、どこに書けばいいのかわかりません。もう少し、いろいろ試してみようと思います。

C#

1 this.label1 = new System.Windows.Forms.Label(); 2 3 int xxx, yyy; 4 xxx = this.Location.X; 5 yyy = this.Location.Y; 6 string x1 = xxx.ToString("0"); 7 string y1 = yyy.ToString("0"); 8 9 this.label1.AutoSize = true; 10 this.label1.Location = new System.Drawing.Point(50, 150); 11 this.label1.Name = "label1"; 12 this.label1.Size = new System.Drawing.Size(100, 50); 13 this.label1.TabIndex = 1; 14 this.label1.Text = x1 + "," + y1;

#解決方法
途中まで書いたものを、いったんすべて削除し、もういちど、最初から test01 をつくりました。今度は、Designer.cs には加筆せず、すべて Form1.cs に記載しました。私が望んでいたのは、「アプリそのものを移動させたときに、常に Form の位置が表示されるようにしたい」ということでしたので、そのようにさせていただきました。具体的には、次のとおりです。

> Form1.cs

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 test01 12{ 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 19 this.label1 = new System.Windows.Forms.Label(); 20 this.label1.AutoSize = true; 21 this.label1.Text = "★★ TEXT ★★"; 22 this.label1.Location = new System.Drawing.Point(50, 50); 23 this.label1.TabIndex = 0; 24 25 this.Controls.Add(this.label1); 26 this.LocationChanged += Form1_LocationChanged; // 常に今の値が欲しい場合 27 28 } 29 30 31 private void Form1_LocationChanged(object sender, EventArgs e) 32 { 33 int xxx = this.Location.X; 34 int yyy = this.Location.Y; 35 string x1 = xxx.ToString("0"); 36 string y1 = yyy.ToString("0"); 37 this.label1.Text = x1 + "," + y1; 38 } 39 40 private System.Windows.Forms.Label label1; 41 } 42}

御回答いただいた皆様、ありがとうございました。

TN8001👍を押しています

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

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

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

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

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

Zuishin

2019/12/28 11:43

Designer.cs を書き換えるなって書いてませんか?
TAKASE_Hiroyuki

2019/12/28 12:06

御回答いただき、ありがとうございます。 Designer.cs は、書き換えてはいけないんですね。 /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> この部分の英文ですね。だとすると、今回は、どのようにするのが妥当だったのでしようか・・・。すいません。自分でも、もうすこし研究してみます。
KOZ6.0

2019/12/28 13:26

.NET Core の Windows Forms はデザイナがついていません。 Preview 版ならついていますが、まだまだ洗練されていない感じです。 「Visual Studio 2019 Preview リリース ノート」 https://docs.microsoft.com/ja-jp/visualstudio/releases/2019/release-notes-preview .NET Framework で作成したほうが良いのでは? .NET Core を使うにしても、.NET Framework でデザイナを使って Designer.cs がどのように生成されるか学んでからとりかかったほうが良いと思います。
Zuishin

2019/12/28 13:28

コードを書き換える場合は Form1.Designer.cs ではなく Form1.cs を書き換えてください。しかし今回の場合はコードを直接書き換えるのではなく、Shift+F7 を押してデザイナを使ってラベルを貼り付けるのが正攻法です。そして Form1 の Move イベントでラベルの値を書き換えてください。
Zuishin

2019/12/28 13:32

ただし .NET Core の場合はデザイナが使えないので、.NET Framework を使うのが良いと思います。
TAKASE_Hiroyuki

2019/12/28 13:39

御回答いただき、ありがとうございました。今回の場合には、.NET Framework でデザイナを使ってできた Desinger.cs を見ながら、しかし、.NET Core では Designer.cs はそのままにして変更せず、Form1.cs を書き換えるという方法で対応しました。結果については、別途、書かせていただきました。ありがとうございました。
guest

回答2

0

label1.Parent の設定が抜けてますね

投稿2019/12/28 11:49

y_waiwai

総合スコア87774

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

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

TAKASE_Hiroyuki

2019/12/28 12:08

御回答いただき、ありがとうございます。 教えてていただいたことは、label1.Parent を Form1 にしなければならない、ということで間違いありませんでしょうか。方法は、よく分からないのですが、自分でもやってみます。
y_waiwai

2019/12/28 12:12

まずやってみようよ
guest

0

ベストアンサー

.NET Coreではデザイナはまだプレビューなので悩ましいですね(もう間もなく来るはずです)

.NET Frameworkで作ってみるとわかりますが
this.Controls.Add(this.label1);
が抜けいていますので表示されません。最後のあたりに追加してください。

座標を正しく表示するには、LoadLocationChangedイベントで値を設定することになると思います。

cs

1using System; 2using System.Drawing; 3using System.Windows.Forms; 4 5namespace test01 6{ 7 public partial class Form1 : Form 8 { 9 private Label label1; 10 11 public Form1() 12 { 13 InitializeComponent(); 14 15 // Form1.Designer.csでもいいですけど注意が必要です 16 this.label1 = new Label(); 17 this.label1.AutoSize = true; 18 this.label1.Location = new Point(50, 150); 19 this.label1.Name = "label1"; 20 this.label1.Size = new Size(100, 50); 21 this.label1.TabIndex = 1; 22 23 this.Controls.Add(this.label1); 24 25 this.Load += Form1_Load; // 開いた時の位置が分かればいい場合 26 this.LocationChanged += Form1_LocationChanged; // 常に今の値が欲しい場合 27 } 28 29 private void Form1_Load(object sender, EventArgs e) 30 { 31 int xxx = this.Location.X; 32 int yyy = this.Location.Y; 33 string x1 = xxx.ToString("0"); 34 string y1 = yyy.ToString("0"); 35 this.label1.Text = x1 + "," + y1; 36 } 37 38 private void Form1_LocationChanged(object sender, EventArgs e) 39 { 40 int xxx = this.Location.X; 41 int yyy = this.Location.Y; 42 string x1 = xxx.ToString("0"); 43 string y1 = yyy.ToString("0"); 44 this.label1.Text = x1 + "," + y1; 45 } 46 } 47}

投稿2019/12/28 12:20

編集2023/07/17 13:12
TN8001

総合スコア9321

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

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

TAKASE_Hiroyuki

2019/12/28 13:45

御回答いただき、ありがとうございました。 本文に追記させていただいたとおり、無事に、うまく動くようになりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問