winform不同窗体之间的传值功能实现方案

版权声明: https://blog.csdn.net/qq_33323054/article/details/79501959
winform不同窗体之间的传值功能实现方案(在窗体feature中获取窗体Login中的数据值)

方案一:

执行结果:在feature中获取login中combobox1.Text的值并且将其赋值给了ft

1.在login中输入如下代码


        public string StringValue
        {
            get { return comboBox1.Text; }
            set { comboBox1.Text = value; }
        }

2.在login中新建feature时代码:

Feature fe=new Feature(this);

fe.Show();

3.然后在feature中做如下操作:

        login lg;
        string ft;
        public Feature(login lg): this()
        {
            this.lg = lg;
            ft = lg.StringValue;
        }

        public void SaveImage(OpenFileDialog openF)
        {
            string P_str = openF.FileName;//开始读取图片
            FileStream fs = new FileStream(P_str, FileMode.Open, FileAccess.Read);
            BinaryReader br=new BinaryReader(fs);
            byte[] img = br.ReadBytes((int)fs.Length);//将图片转换为字节流

            StringBuilder strsql = new StringBuilder();
            strsql.Append("update hospital.login set photo=@Photo where id='" + ft + "';");//构造sql语句,@photo在调用的函数中用数据的字节流替代
            ExecuteSql.SaveImage(strsql,img);
        }

加粗部分就是调用步骤。

方案二:

在Login中执行如下代码:

Feature fe=new Feature();

fe.user=combobox1.text;

然后在ferture中user的值就是login中combobox1.text的值。

//很明显方案二要方便很多。而刚开始开发时并不知情,使用方案一吃了不少苦头。

不过在使用方案二之前必须先要确定login和Feature之间的父子窗体关系

猜你喜欢

转载自blog.csdn.net/qq_33323054/article/details/79501959
今日推荐