将值从usercontrol传输到form1 [英] Transfer value from usercontrol to form1

查看:195
本文介绍了将值从usercontrol传输到form1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Form1。还有一个UserControl1。
Form1中的
我单击一个Label并显示UserControl1。

在UserControl1中我更新了一个公共字符串值。

现在我需要将该值放在Form1中的Label上。



什么是最好的方法?



我尝试过:



仅使用c#默认实现。

解决方案

请参阅此处:在两个表单之间传输信息,第2部分:孩子到父母 [ ^ ] - 它是关于表单的,但是父表单和子用户控件是完全相同的过程,甚至是代码。


这是我的方法:

我认为更合乎逻辑,更简单。

只需隐藏和取消隐藏用户控件,并通过公共变量传输数据。

 // UserControl1 
公共部分类UserControl1:UserControl
{
public UserControl1()
{
InitializeComponent();
}
//从Form1关闭UserControl
private void label1_Click(object sender,EventArgs e)
{
this.Visible = false;
}


public string data =;
// crystal
private void button1_Click(object sender,EventArgs e)
{
data =crystal;
关闭();
}
// gas
private void button2_Click(object sender,EventArgs e)
{
data =gas;
关闭();
}
// cell
private void button3_Click(object sender,EventArgs e)
{
data =cell;
关闭();
}

// ----------------------------------- ---------------------------------------------

// Form1

private void pictureBox1_Click(object sender,EventArgs e)
{
controlClicked =pictureBox1;
userControl11.Visible = true;
}


string mainData =;
string controlClicked =;
private void userControl11_VisibleChanged(object sender,EventArgs e)
{
if(userControl11.Visible == false)
{
if(userControl11.data ==crystal) )
{
mainData =crystal;
if(controlClicked ==pictureBox1)
{
pictureBox1.BackgroundImage = Image.FromFile(1.png);
}
}
if(userControl11.data ==gas)
{
mainData =gas;
}
if(userControl11.data ==cell)
{
mainData =cell;
}

// -------------------------
}
}


I have Form1. And a UserControl1.
in Form1 I click a Label and make UserControl1 show.
In UserControl1 I update a public string value.
Now I need that value to be on the Label in Form1.

What is the best approach?

What I have tried:

using only c# default implementations.

解决方案

See here: Transferring information between two forms, Part 2: Child to Parent[^] - it's written about forms, but a Parent Form and a Child UserControl is exactly the same process, and even code.


Here is my approach:
More logical and more simpler I think.
Just hide and unhide the usercontrol, and transfer data between through public variables.

//UserControl1
 public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        //Close the UserControl from Form1
        private void label1_Click(object sender, EventArgs e)
        {
            this.Visible = false; 
        }


        public string data = ""; 
        //crystal
        private void button1_Click(object sender, EventArgs e)
        {
            data = "crystal";
            Close();
        }
        //gas
        private void button2_Click(object sender, EventArgs e)
        {
            data = "gas";
            Close();
        }
        //cell
        private void button3_Click(object sender, EventArgs e)
        {
            data = "cell";
            Close();
        }

//--------------------------------------------------------------------------------

//Form1

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            controlClicked = "pictureBox1";
            userControl11.Visible = true;
        }

        
        string mainData = "";
        string controlClicked = "";
        private void userControl11_VisibleChanged(object sender, EventArgs e)
        {
            if (userControl11.Visible == false )
            {
                if (userControl11.data == "crystal")
                {
                    mainData = "crystal";
                    if (controlClicked == "pictureBox1")
                    {
                        pictureBox1.BackgroundImage = Image.FromFile("1.png");
                    }
                }
                if (userControl11.data == "gas")
                {
                    mainData = "gas";
                }
                if (userControl11.data == "cell")
                {
                    mainData = "cell";
                }

                //-------------------------
            }
        }


这篇关于将值从usercontrol传输到form1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆