C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト

Re[17]: 親フォームから子フォームのメソッドへアクセス [1]


(過去ログ 130 を表示中)

[トピック内 23 記事 (21 - 23 表示)]  << 0 | 1 >>

■77177 / inTopicNo.21)  Re[15]: 親フォームから子フォームのメソッドへアクセス
  
□投稿者/ ゆーきゃん (59回)-(2015/09/19(Sat) 09:54:34)
No77175 (魔界の仮面弁士 さん) に返信

本当にお答え助かります。
頭が上がりません。

連休に入り帰省しますので、プログラムを組む環境がなくなってしまいます。帰省明けに魔界の仮面弁士様の
御回答を元にプログラムを組み直させて頂きたいと思います。


引用返信 編集キー/
■77205 / inTopicNo.22)  Re[16]: 親フォームから子フォームのメソッドへアクセス
□投稿者/ ゆーきゃん (60回)-(2015/09/25(Fri) 12:01:08)
皆様大変お世話になっております。

皆様のお答え・アドバイスのおかげで何とか形にすることができました。
まだまだ至らない点もあると思いますが、一応プログラムソースを貼らせて頂きます。


●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;
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
{

    // 共通のDrawクラスを作成
    public static class Draw
    {
        public static void draw(object sender, PaintEventArgs e, float x1, float y1, float x2, float y2)
        {
            e.Graphics.DrawLine(Pens.Green, x1, y1, x2, y2);
        }
    }
 
    public partial class Form1 : Form
    {

        // Form2の宣言
        Form2 fm2 = new Form2();
      
        public float x1;
        public float y1;
        public float x2;
        public float y2;

        public Form1()
        {   
            InitializeComponent();           
        }

        public void pictureBox1_Paint(object sender, PaintEventArgs e)
        { 
            yomikomi();
            Draw.draw(sender,e,x1,y1,x2,y2);

        }

        public void yomikomi()
        {
            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(); //                     //

            fs.Close(); // ファイルを閉じる
        }


        private void button1_Click(object sender, EventArgs e)
        {
            fm2.set_textbox_form2(x1,y1,x2,y2);

            if (fm2.ShowDialog() == DialogResult.OK)
            {
                kakikomi();
                pictureBox1.Invalidate();
            }
        }       

        public void kakikomi()
        {
            using (BinaryWriter w = new BinaryWriter(File.OpenWrite(@"c:\data\test1.dat"))) // ファイルの書き込み
            {
                w.Write(fm2.pro._s_x1);
                w.Write(fm2.pro._s_y1);
                w.Write(fm2.pro._s_x2);
                w.Write(fm2.pro._s_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 class Property
    {
        public float send_x1;
        public float send_y1;
        public float send_x2;
        public float send_y2;

        public float _s_x1
        {
            set { send_x1 = value; }
            get { return send_x1; }
        }

        public float _s_y1
        {
            set { send_y1 = value; }
            get { return send_y1; }
        }

        public float _s_x2
        {
            set { send_x2 = value; }
            get { return send_x2; }
        }

        public float _s_y2
        {
            set { send_y2 = value; }
            get { return send_y2; }
        }
    }

    public partial class Form2 : Form
    {

        public Property pro = new Property();

        public Form2()
        {
            InitializeComponent();
        }

        public void set_textbox_form2(float _x1 , float _y1, float _x2, float _y2)
        {
            textBox1.Text = _x1.ToString();
            textBox2.Text = _y1.ToString();
            textBox3.Text = _x2.ToString();
            textBox4.Text = _y2.ToString();
        }

        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            Draw.draw(sender, e, pro.send_x1, pro.send_y1, pro.send_x2, pro.send_y2);
        }

        private void button2_Click(object sender, EventArgs e)
        {    
            this.Close();
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            pro.send_x1 = float.Parse(textBox1.Text);
            pictureBox2.Invalidate();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            pro.send_y1 = float.Parse(textBox2.Text);
            pictureBox2.Invalidate();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            pro.send_x2 = float.Parse(textBox3.Text);
            pictureBox2.Invalidate();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        { 
            pro.send_y2 = float.Parse(textBox4.Text);
            pictureBox2.Invalidate();
        }  
    }
}

引用返信 編集キー/
■77212 / inTopicNo.23)  Re[17]: 親フォームから子フォームのメソッドへアクセス
□投稿者/ Jitta from iPhone (1回)-(2015/09/26(Sat) 11:23:19)
ファイルの内容、データ構造をクラス化する。
そのクラスにGraphicsを渡して描かせる。

引用返信 編集キー/

<前の20件
トピック内ページ移動 / << 0 | 1 >>

このトピックに書きこむ

過去ログには書き込み不可

管理者用

- Child Tree -