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

ถามเรื่องการส่งค่าข้าม form คับ Options · View
ein
Posted: Thursday, October 09, 2008 9:08:02 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 10/8/2008
Posts: 4
เช่นว่า ถ้ามีการรับค่า(ตัวเลข) จาก numericUpDown ที่ form1 แล้ว เราต้องการ นำค่านี้ ไปเป็นตัวแปรใน form 2 ต้อง ทำ ยังไงครับ เขียนตัวอย่างไห้ดูก้ดีครับ
paedotnet
Posted: Friday, October 17, 2008 1:50:16 PM

Rank: มือเทพ
Groups: Member

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

ในตัวอย่างนี้ผมจะใช้ Delegate ในการรับและส่งค่าระหว่าง Form นะครับ
1. สร้าง form 2 form ขึ้นมา ใน form แรก ให้ลาก numericUpDown,Button มาวางใน form , ใน form  2 ลาก label มาวางเพื่อแสดงค่าของ numericupdown ที่ถูกส่งมาจาก form1
2.ใน form 1 คลิกที่ปุ่ม Button แล้วเขียนโค้ดดังนี้


   private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            SendDataToForm2 mydelegate = new SendDataToForm2(form2.GetData);
            mydelegate(this.numericUpDown1.Value.ToString());
            form2.Show();
        }

***ให้ประกาศ Delegate ก่อนคลาสด้วยดังนี้
public delegate void SendDataToForm2(string txt);

3. ที่ Form2 เพิ่มเมธอดังนี้


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


4. ผลลัพธ์ดังรูป


5. เมื่อกำหนดตัวเลขใน numericupdown แล้วคลิกทีปุ่ม Send ก็จะมี form 2 เกิดขึ้นมาดังรูป

ตัวอย่างโค้ดหมดใน 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 WindowsFormsApplication2
{
    public delegate void SendDataToForm2(string txt);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            SendDataToForm2 mydelegate = new SendDataToForm2(form2.GetData);
            mydelegate(this.numericUpDown1.Value.ToString());
            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 WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
        }
        public void GetData(string txt)
        {
            label1.Text = txt;
        }
    }
}

 



[With great power comes great responsibility]
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