最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)不同窗體之間傳遞參數(shù)

 更新時(shí)間:2023年02月26日 13:56:11   作者:阿喵一定行  
這篇文章主要介紹了C#實(shí)現(xiàn)不同窗體之間傳遞參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C#不同窗體之間傳遞參數(shù)

最近導(dǎo)師安排C#寫(xiě)桌面GIS開(kāi)發(fā)。碰到諸多問(wèn)題。無(wú)奈不是計(jì)算機(jī)出身的我早就把編程基礎(chǔ)還給老師了。

開(kāi)發(fā)過(guò)程屬于敲代碼兩分鐘翻博客兩小時(shí)的狀態(tài),這邊將自己的問(wèn)題及解決方案總結(jié)。

主要兩個(gè)內(nèi)容:

①父窗口給子窗口傳遞參數(shù),

②子窗口給父窗口傳遞參數(shù)。

1.父窗口向子窗口傳遞參數(shù)

這個(gè)就比較簡(jiǎn)單了,級(jí)別高一點(diǎn)訪問(wèn)也輕松一點(diǎn)。具體的原理我就不講了(感覺(jué)說(shuō)不清楚OJ2…),總之使用類(lèi)的私有變量然后父窗口賦值就可以了。

父窗口(mainForm)代碼:

namespace demo_Params
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }
        
        //傳遞變量到子窗口
        private void btn_Input_BtnClick(object sender, EventArgs e)
        {
            childForm childForm = new childForm();//childForm為新建窗口類(lèi)
            childForm.str = this.tb_MainFormIn.InputText ;//str為子類(lèi)的公有變量
            if (childForm.ShowDialog() == DialogResult.OK) return;
        }

        
    }
}

子窗口(childForm)代碼:

namespace demo_Params
{
    public partial class childForm : Form
    {
        public childForm()
        {
            InitializeComponent();
        }

        //私有變量和賦值,value值在父窗口傳遞
        private string w_str;
        public string str
        {
            set
            {
                w_str = value;
            }
        }
        //顯示父窗口的變量到文本框
        private void btn_getPara_BtnClick(object sender, EventArgs e)
        {
            this.tb_childFormIn.Text = w_str;
        }
    }
}

父窗口向子窗口傳遞

2.子窗口向父窗口傳遞參數(shù)

這個(gè)就比較麻煩了,看了很多,自己用起來(lái)感覺(jué)不錯(cuò)的是通過(guò)委托事件和事件觸發(fā)執(zhí)行函數(shù)來(lái)解決參數(shù)的傳遞和接受。道理就不說(shuō)了,直接COPY用起來(lái)。

父窗口(mainForm)代碼:

namespace demo_Params
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }
        //接受參數(shù)初始化
        string str = "";      
        //打開(kāi)子窗口childForm
        private void btn_openWin_BtnClick(object sender, EventArgs e)
        {

            childForm childForm = new childForm();
            childForm.getParam += new backParam(fun_GetChildParam);//綁定事件
            if (childForm.ShowDialog() == DialogResult.OK) return;
        }
        //委托事件執(zhí)行方法
        void fun_GetChildParam(string w_childpara) 
        {
            str = w_childpara;
        }
        //顯示參數(shù)到文本框,看看參數(shù)能不能調(diào)用
        private void btn_Output_BtnClick(object sender, EventArgs e)
        {
            tb_MainFormOut.InputText = str;
        }
    }
}

子窗口(childForm)代碼:

namespace demo_Params
{

    public delegate void backParam(string str);//聲明委托
    public partial class childForm : Form
    {
        public childForm()
        {
            InitializeComponent();
        }
        public event backParam getParam;//委托事件,接受一個(gè)string變量
        
        //傳回變量 關(guān)閉窗口
        private void btn_childFormBack_BtnClick(object sender, EventArgs e)
        {
            getParam(this.tb_childFormIn.Text);//將變量委托
            this.DialogResult = DialogResult.OK;
        }
    }
}


3.代碼說(shuō)明

兩種傳遞參數(shù)的情況,我寫(xiě)在了一個(gè)程序里面。整理時(shí)為了區(qū)分,有所刪改。(子傳父代碼塊內(nèi)不含父?jìng)髯觾?nèi)容)

直接復(fù)制代碼至WPF項(xiàng)目中,應(yīng)該不能運(yùn)行。 使用了第三方控件,部分控件屬性、事件命名不同。大家如要復(fù)現(xiàn)使用TextBox和Button即可。

C#子窗體與父窗體之間的參數(shù)傳輸

最近在做項(xiàng)目時(shí)涉及到了子窗體與父窗體之間的參數(shù)傳輸問(wèn)題,通過(guò)查閱與學(xué)習(xí)總結(jié)了一種方法。

1.子窗體傳父窗體

Form1為主窗體,F(xiàn)orm2為子窗體。

實(shí)現(xiàn):在Form1上添加一個(gè)button1,點(diǎn)擊button1后顯示Form2,再點(diǎn)擊Form2的button1 在button1_Click事件中通過(guò)this.Owner將Form2的textBox2的值設(shè)置給Form1的textBox1(也可以將Form2中的某個(gè)值傳給Form1,然后在Form1進(jìn)行后續(xù)的處理,將示例代碼修改一下即可)

示例代碼:

    //Form1上的代碼(主窗體)
    public partial class Form1 : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //顯示Form2
            Form2 childForm = new Form2();
            childForm.Show();
            //定義Form2的“爸爸”為Form1 
            calForm.Owner = this;
            
 
            或者
            //Form2 childForm = new Form2();
            //childForm.Show(this);
        }
    }
 
 
    //Form2上的代碼(子窗體)
    public partial class Form2 : Form
    {
        public CalibrationForm()
        {
            InitializeComponent();
        }
        private void button_Click(object sender, EventArgs e)
        {
            MainForm mForm = (MainForm)this.Owner;
            //注意 如果textBox1是放在panel1中的 則先找panel1 再找textBox1
            ((TextBox)mForm.Controls["textBox1"]).Text = this.textBox2.Text;
 
            //也可直接將控件的屬性Modifiers修改為public 然后直接調(diào)用
            mForm.textBox1.Text = this.textBox2.Text;
        }
     }

2.父窗體傳子窗體

Form1為主窗體,F(xiàn)orm2為子窗體。

實(shí)現(xiàn):在Form1上添加一個(gè)button1,點(diǎn)擊button1后顯示Form2,然后點(diǎn)擊Form2的button1顯示Form1中的某個(gè)參數(shù)。(用構(gòu)造函數(shù)來(lái)實(shí)例化Form2窗體,然后把Form1的this指針傳進(jìn)去,這樣就可以在Form2中調(diào)用Form1的參數(shù)(此參數(shù)必須是public屬性的))

示例代碼:

    //Form1上的代碼(主窗體)
    public partial class Form1 : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        
        public string str = "你好!";
        private void button1_Click(object sender, EventArgs e)
        {
            //實(shí)例化子窗體,并將父窗體的指針this傳入
            Form2 childForm = new Form2(this);
            childForm.Show();
        }
    }
 
 
    //Form2上的代碼(子窗體)
    public partial class Form2 : Form
    {
        public CalibrationForm()
        {
            InitializeComponent();
        }
 
        private Form1 frmMain;
        public Form2(Form1 mForm)
        {
            InitializeComponent();
            this.frmMain = mForm;
        }
 
        private void button1_Click(object sender,EventArgs e)
 
        {
 
             this.textBox1.Text = mForm.str;  
 
        }
     }

子窗體與父窗體之間的參數(shù)傳輸方法不止這一種,還可以用委托等,這里只總結(jié)了個(gè)人認(rèn)為較為簡(jiǎn)單的一種方法,如果以后涉及委托會(huì)進(jìn)行補(bǔ)充。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

钟山县| 东乡族自治县| 政和县| 卢龙县| 县级市| 交城县| 卓资县| 镇坪县| 澄江县| 定陶县| 哈尔滨市| 昌宁县| 昭平县| 镇平县| 鱼台县| 玉山县| 陈巴尔虎旗| 当阳市| 永新县| 库伦旗| 太仓市| 孟村| 炉霍县| 屏边| 长丰县| 普兰县| 旬阳县| 宣化县| 融水| 蒙城县| 玉屏| 诸城市| 荥经县| 泽州县| 广安市| 介休市| 涟水县| 聂拉木县| 教育| 郎溪县| 胶南市|