| ■77125 / ) |
親フォームから子フォームのメソッドへアクセス |
□投稿者/ ゆーきゃん (46回)-(2015/09/15(Tue) 16:15:15)
|
分類:[.NET 全般]
いつもお世話になっております。
C#/VisualStudio2013/windowsformアプリケーションでプログラムを組んでおります。
先日、子フォームから親フォームのメソッドへアクセスという内容で、質問させて頂きました。
http://bbs.wankuma.com/index.cgi?mode=al2&namber=77101
今度は親フォームから子フォームのメソッドへのアクセスが思うようにいかず、足が止まってしまいました。
プログラムの流れは以下の通りです。
Form1(親フォーム)には、picturebox1、button1
Form2(子フォーム)には、picturebox2、button2, textBox1~4
があります。
親フォームのbutton1を押すと、Form2が表示され、picturebox2に「test1.dat」に保存された座標値(x1(100),y1(100))~ (x2(150),y2(100))を元に線を描画します。
textBox1~4には、x1,y1,x2,y2の値が表示され、値を入力し直すと、線の長さも変更されるようになっております。
button2を押すと、textBox1~4の値を「test1.dat」に書き出し、データを保存、Form2を閉じるようになっております。
ここで質問なのですが、Form1が開いた際に、Form2の「yomikomiメソッド」へアクセスし、picturebox1にその座標値を元にした線を描画したいのですが、どのようにプログラムを組むことで可能でしょうか?
Form1側で、
((Form2)Application.OpenForms["Form2"]).yomikomi();
Form2.yomikomi();
など追加してみたのですが、静的関数でないなどの理由から、思い通りの結果を出力できませんでした。
何か良い方法・プログラムを省略できるポイントなどありましたら、ご指導頂けないでしょうか。
以下作成途中のプログラムを掲載させて頂きます。
● Form1 ==================================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (Form2 fm = new Form2(draw))
{
fm.ShowDialog();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// draw(sender, e);
}
public void draw(object sender, PaintEventArgs e)
{
// e.Graphics.DrawLine(Pens.Green, x1, y1, x2, y2);
}
}
}
● Form2 ==================================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
namespace test1
{
public partial class Form2 : Form
{
float x1;
float y1;
float x2;
float y2;
public Form2(PaintEventHandler drawMethod)
{
InitializeComponent();
if (drawMethod != null)
{
this.pictureBox2.Paint += drawMethod;
}
}
public Form2(): this(null)
{
}
private void Form2_Load(object sender, EventArgs e)
{
yomikomi(sender, e);
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Green, x1, y1, x2, y2);
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
x1 = int.Parse(textBox1.Text);
}
private void textBox2_TextChanged_1(object sender, EventArgs e)
{
y1 = int.Parse(textBox2.Text);
}
private void textBox3_TextChanged_1(object sender, EventArgs e)
{
x2 = int.Parse(textBox3.Text);
}
private void textBox4_TextChanged_1(object sender, EventArgs e)
{
y2 = int.Parse(textBox4.Text);
pictureBox2.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
using (BinaryWriter w = new BinaryWriter(File.OpenWrite(@"c:\data\test1.dat"))) // ファイルの書き込み
{
w.Write(x1);
w.Write(y1);
w.Write(x2);
w.Write(y2);
}
this.Close();
}
public void yomikomi(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"c:\data\test1.dat", FileMode.Open, FileAccess.Read); // ファイルを開く
BinaryReader br = new BinaryReader(fs); // ファイルの読み取り
x1 = br.ReadSingle(); // 4バイト浮動小数点値を読み取り、4バイト進める
y1 = br.ReadSingle(); // //
x2 = br.ReadSingle(); // //
y2 = br.ReadSingle(); // //
textBox1.Text = x1.ToString(); // 読み取った値をtextboxに代入
textBox2.Text = y1.ToString(); // //
textBox3.Text = x2.ToString(); // //
textBox4.Text = y2.ToString(); // //
fs.Close(); // ファイルを閉じる
}
}
}
|
|