.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

การรับและส่งค่าระหว่าง Form Options · View
paedotnet
Posted: Friday, October 24, 2008 9:44:44 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk

การรับ-ส่งค่าระหว่าง form 
   ในกรณีที่ต้องการรับ-ส่งค่าระหว่าง Window Form สามารถทำได้หลายวิธีในตัวอย่างนี้จะแสดงการส่งและ
รับค่าโดยใช้วิธี การสร้าง Delegate , constructor และ Method

การส่งและรับค่าโดยใช้ Delegate

1. สร้าง Window Form ขึ้นมาสอง Form ซึ่งมีชื่อเป็น Form1,Form2 โดยในตัวอย่างนี้จะทำการส่งค่า
    ที่รับมาจาก CheckBox ไปให้กับ Form2
   ใน Form1 ให้ลาก CheckedListBox และ Button มาวางดังรูป

จากนั้นคลิกที่รูปลูกศรด้านบนขวาของ
    CheckedListBox แล้วเลือก Edit Item ดังรูป 

 

จากนั้นจะมีหน้าต่าง String Collection Editor ขึ้นมา
    ให้ใส่ข้อมูลลงไปสำหรับตัวเลือกแต่ละตัวของ checkbox พอใส่ข้อมูลเสร็จแล้วคลิกที่ปุ่ม OK จะได้ดังรูป


2.  จากนั้นดับเบิลคลิกที่ปุ่ม Button แล้วจะเข้าสู่หน้า Form1.cs เพื่อเขียนโค้ดให้เขียนโค้ดด้านบนการประกาศคลาสดังนี้

    public delegate void SendData(CheckedListBox chkList);


3. ใน event button1_Click เขียนโค้ดดังนี้

     private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            SendData send = new SendData(form2.GetData);
            send(checkedListBox1);
            form2.Show();
        }

4.   จากนั้นไปที่ Form2 ลาก Label มาวางใน Form คลิกที่ Label แล้วไปที่ properties กำหนดตรงช่อง Text ให้เป็นค่าว่าง
        จากนั้นไปที่ Form2.cs เขียนโค้ดดังนี้

         public void GetData(CheckedListBox chkList)
        {
            System.Collections.IEnumerator i = chkList.CheckedItems.GetEnumerator();
            while (i.MoveNext())
            {
                label1.Text += i.Current.ToString()+"\n";
            }
        }

ผลลัพธ์จะได้ดังรูป

 

ตัวอย่างโค้ดทั้งหมด

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public delegate void SendData(CheckedListBox chkList);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            SendData send = new SendData(form2.GetData);
            send(checkedListBox1);
            form2.Show();
        }
    }
}


Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
         
        }
        public void GetData(CheckedListBox chkList)
        {
            System.Collections.IEnumerator i = chkList.CheckedItems.GetEnumerator();//chkList.CheckedIndices.GetEnumerator();
            while (i.MoveNext())
            {
                label1.Text += i.Current.ToString()+"\n";
            }
        }
    }
}


การส่งและรับค่าระหว่าง Form โดยใช้ constructor

1. ในตัวอย่างนี้ใน Form1 ให้ลาก TextBox,Button มาวางส่วน Form2 ก็ลาก Label มาวาง
    จากนั้นดับเบิลคลิกที่ button ใน Form1 แล้วเขียนโค้ดดังนี้

      private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this.textBox1.Text);
            form2.Show();
        }


2. จากนั้นไปที่ form2 เขียนโค้ดดังนี้

     public Form2(string data)
        {
            InitializeComponent();
            label1.Text = data;
        }


ผลลัพธ์จะได้ดังรูป

 

ตัวอย่างโค้ด

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this.textBox1.Text);
            form2.Show();
        }
    }
}

ตัวอย่างโค้ด

 Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form2 : Form
    {
        public Form2(string data)
        {
            InitializeComponent();
            label1.Text = data;
        }
        private void Form2_Load(object sender, EventArgs e)
        {
         
        }
    }
}


การรับส่งค่าระหว่าง Form โดยใช้ Method
ในตัวอย่างนี้ให้ลาก TextBox,Button มาวางใน form1 และลาก label มาวางใน form2
จากนั้นไปที่ form1
เขียนโค้ดดังนี้

 private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this.textBox1.Text);
            form2.GetData(SendData());
            form2.Show();
        }
        public string SendData()
        {
            return textBox1.Text;
        }

จากนั้นไปที่ Form2 แล้วเขียนโค้ดดังนี้

  public void GetData(string str)
        {
            label1.Text = str;
        }

ผลลัพธ์จะได้ดังรูป

 

 

ตัวอย่างโค้ด

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.GetData(SendData());
            form2.Show();
        }
        public string SendData()
        {
            return textBox1.Text;
        }
    }
}

Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
         
        }
        public void GetData(string str)
        {
            label1.Text = str;
        }
    }
}

 

     



[With great power comes great responsibility]
mrxc
Posted: Saturday, October 25, 2008 9:44:58 AM
Rank: มือเทพ
Groups: Member

Joined: 3/6/2008
Posts: 196
Location: TH

การใช้ delegate ที่แท้จริงนั้นส่วนมากจะนิยมให้เวลา object ที่ถูกเรียกต้องการส่งอะไรมายัง object ที่เรียกใช้ครับ เพราะว่า object ที่เรียกใช้จะสามารถส่งอะไรไปหา object ที่ถูกเรียกได้โดยง่าย ไม่จำเป็นจะต้องมีการใช้ delegate เช่นการเรียกใช้ properties, object, method ที่เป็น public ได้ทันที

 

แต่ในทางกลับกัน object ที่ถูกเรียกใช้จะไม่สามารถทำแบบนั้นได้เพราะว่า object ที่ถูกเรียกใช้นั้นจะไม่เห็น properties, object, method ภายในของ object ที่เรียกใช้ตัวเอง ดังนั้นจึงแก้ปัญหาด้วย pointer หรือ delegate ที่เรียกกันนั้นเอง

 

ดังนั้นการแสดงตัวอย่างผมอยากจะแสดงตัวอย่างที่ object ที่ถูกเรียกใช้ส่งค่ากลับไปยัง object ที่เรียกใช้มากกว่าครับ เพราะว่าที่เห็นเกิดปัญหากัน และแก้กันไม่ตกจะเป็นจุดนี้มากกว่าครับ

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by