日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

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

作者:阿喵一定行 ? 更新時(shí)間: 2023-06-18 編程語言

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

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

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

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

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

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

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

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

父窗口(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為新建窗口類
            childForm.str = this.tb_MainFormIn.InputText ;//str為子類的公有變量
            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è)就比較麻煩了,看了很多,自己用起來感覺不錯(cuò)的是通過委托事件和事件觸發(fā)執(zhí)行函數(shù)來解決參數(shù)的傳遞和接受。道理就不說了,直接COPY用起來。

父窗口(mainForm)代碼:

namespace demo_Params
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }
        //接受參數(shù)初始化
        string str = "";      
        //打開子窗口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.代碼說明

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

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

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

最近在做項(xiàng)目時(shí)涉及到了子窗體與父窗體之間的參數(shù)傳輸問題,通過查閱與學(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事件中通過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ù)來實(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)為較為簡單的一種方法,如果以后涉及委托會進(jìn)行補(bǔ)充。

總結(jié)

原文鏈接:https://blog.csdn.net/qq_38560619/article/details/107049844

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新