C#:尝试从另一个Form调用UserControl函数

时间:2015-03-15 17:48:47

标签: c# user-controls public void

我的问题很简单。我想点击放在Form1上的button1,看看放在Form2上的UserControl1的背景颜色变为红色。显然这个结果没有得到满足。我是C#的新手,所以在回答这个问题时要考虑到这一点。提前谢谢你的帮助。

Form1的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserControlAccessTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UserControl1 userControl1 = new UserControl1();
            Form2 form2 = new Form2();
            form2.Show();
            userControl1.UserControlColorChange();

        }
    }
}

Form2的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserControlAccessTest
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

UserControl1的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserControlAccessTest
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public void UserControlColorChange()
        {
            UserControl1 userControl1 = new UserControl1();
            userControl1.BackColor = System.Drawing.Color.Red;
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {

        }
    }
}

2 个答案:

答案 0 :(得分:0)

你需要获得对form2上的UserControl1的实际实例的引用(假设你有一个)。 试试这个:

Form2 form2 = new Form2();
form2.Show();
form2.userControl1.UserControlColorChange();

答案 1 :(得分:0)

表格1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserControlAccessTest
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UserControl1 userControl1 = new UserControl1();
        Form2 form2 = new Form2();
        form2.Show();
        form2.changeColor();
    }
}
}

表格2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserControlAccessTest
{
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }
    public void changeColor()
    {
        this.BackColor = System.Drawing.Color.Red;
    }
}
}