从次要控制主要形式 [英] Controlling Primary Form from Secondary

查看:48
本文介绍了从次要控制主要形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在申请由两种形式组成的表格

第一种形式=主要形式
第二种形式=第二种形式(由三个名称分别为R,G和B的轨迹栏组成)

每当尝试以辅助形式移动跟踪栏时,我都在尝试几天同时更改主要"形式的背景颜色"

问题:
我使用.Show()来获取主要形式,而从次要形式中,我可以更改主要形式的颜色,但是问题是每次移动轨迹栏时,.Show()都会弹出新的主要形式

有没有一种方法可以将轨迹栏值发送到主表单",并可以更改主表单"的背景颜色,


我的应用程序的链接在这里
http://www.4shared.com/get/314190370 /5ec62112/color_2_forms.html [ ^ ]


请帮助我,我需要在我的作业中实现此行为

Form1 =空白表格(我想更改Form1颜色)

Form2代码如下(由三个跟踪栏组成)

i have been on application that consist of two forms

1st Form = Primary Form
2nd Form = Secondary Form ( Consists of three Track bars with names R, G ,B)

i m trying for few days to change the Background Color of Primary form simultaneously , whenever i move the trackbar in Secondary form

Problem:
i used .Show() to get hold of Primary form, and from secondary form ,i m able to change color of Primary Form but the Problem is that .Show() pops up the new primary form every time i move the trackbar

is there a way through which i can send trackbar values to Primary form and can change the Background Color of Primary Form ,


the link to my application is here
http://www.4shared.com/get/314190370/5ec62112/color_2_forms.html[^]


please help me , i need to implement this behavior in my assignment

Form1 = Blank Form ( i want to change Form1 Color )

Form2 Code is given below ( Consists of Three Track bar )

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace color_2_forms
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            
        }
        
        public void UpdateMe()
        {
            Form1 frm = new Form1();
            
            
                frm.Show();

                int R = trackBar1.Value;
                int G = trackBar2.Value;
                int B = trackBar3.Value;

            frm.BackColor = Color.FromArgb(R,G,B);

            textBox1.Text = " ( " + trackBar1.Value.ToString() + " , " 
                                + trackBar2.Value.ToString() +","
                            + trackBar3 .Value .ToString () + " ) ";
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Form1 frm = new Form1();
            UpdateMe();
        }

       private void trackBar2_Scroll(object sender, EventArgs e)
       {
            Form1 frm = new Form1();
            UpdateMe();
       }
       
       private void trackBar3_Scroll(object sender, EventArgs e)
       {
           Form1 frm = new Form1();
            UpdateMe();
       }



        
    }
}




请帮助我




Please HELP me

推荐答案

确定,您需要学习一些有关实例的信息.
每当您尝试更新表单时,代码要做的第一件事就是构造表单的新实例.
OK, you need to learn something about instances.
Whenever you are trying to update the form, the first thing your code does is construct a new instance of the form.
Form1 frm = new Form1();

这将创建表单的新副本.不是您可以看到的原始表格,而是一种新的表格.就像您有一辆汽车,而您想给它加油一样.为此,您购买了一辆新车,并将其加满.当用完时,再买一个并填满!

创建您的表单的一个副本,将其保留在form2的类级别,并进行引用.

This makes a NEW copy of the form. Not the original form that you can see, a new one. It is like you have a car, and you want to fill it with gas. To do this, you buy a new car, and fill it up. When that runs out, buy another and fill that up!

Create one copy of your form, keep it at class level in form2, and refer to that.

public partial class Form2 : Form
{
    Form1 frm = new Form1();
    public Form2()
    {
        InitializeComponent();
        frm.Show();
    }

    public void UpdateMe()
    {
       int R = trackBar1.Value;
       int G = trackBar2.Value;
       int B = trackBar3.Value;

       frm.BackColor = Color.FromArgb(R,G,B);
       textBox1.Text = " ( " + trackBar1.Value.ToString() + " , "
                             + trackBar2.Value.ToString() + " , "
                             + trackBar3 .Value .ToString () + " ) ";
    }

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        UpdateMe();
    }

   private void trackBar2_Scroll(object sender, EventArgs e)
   {
        UpdateMe();
   }

   private void trackBar3_Scroll(object sender, EventArgs e)
   {
        UpdateMe();
   }

将来,您想研究在Form2中构造一个事件,该事件会被form1要求用于"ColorChanged",并让form1处理它自己的颜色,但这应该可以助您一臂之力.

In future, you want to look at constructing an event in Form2 that form1 supscribes to for "ColorChanged" and let form1 handle it''s own colors, but that should get you going.


正如格里夫(Griff)所说,这实际上应该通过引发辅助形式的事件来完成.这是一个工作示例:
As Griff mentioned, this should really be done by raising an event in the secondary form. Here''s a working example:
// ColorEventArgs.cs
using System;
using System.Drawing;

namespace ColorChanger
{
    public class ColorEventArgs : EventArgs
    {
        private Color color;

        public ColorEventArgs(int red, int green, int blue)
            : this(Color.FromArgb(red, green, blue))
        { }
        public ColorEventArgs(Color color)
        {
            this.color = color;
        }

        public Color Color
        {
            get { return color; }
        }
    }
}


// FormSecondary.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ColorChanger
{
    public partial class FormSecondary : Form
    {
        // New event
        public event EventHandler<ColorEventArgs> ColorChanged;
        private TrackBar trackBarRed;
        private TrackBar trackBarGreen;
        private TrackBar trackBarBlue;
        public FormSecondary()
        {
            InitializeComponent();
            /* Constructing trackbars in code.
             * If doing in designer, remove the code below and set each ValueChanged event to
             * TrackBarValueChanged method. */
            // Red
            trackBarRed = new TrackBar();
            trackBarRed.Maximum = 255;
            trackBarRed.Location = new Point(12, 12);
            trackBarRed.ValueChanged += TrackBarValueChanged;
            // Green
            trackBarGreen = new TrackBar();
            trackBarGreen.Maximum = 255;
            trackBarGreen.Location = new Point(12, trackBarRed.Bottom + 12);
            trackBarGreen.ValueChanged += TrackBarValueChanged;
            // Blue
            trackBarBlue = new TrackBar();
            trackBarBlue.Maximum = 255;
            trackBarBlue.Location = new Point(12, trackBarGreen.Bottom + 12);
            trackBarBlue.ValueChanged += TrackBarValueChanged;
            // Add controls
            SuspendLayout();
            Controls.AddRange(new Control[]{
                trackBarRed,
                trackBarGreen,
                trackBarBlue
            });
            ResumeLayout(true);
        }
        // Raises the new event
        protected virtual void OnColorChanged(ColorEventArgs e)
        {
            EventHandler<ColorEventArgs> eh = ColorChanged; // copy to avoid race condition
            if (eh != null) // null check in case of no subscribers
                eh(this, e); // raise event
        }
        // Common handler for TrackBars
        private void TrackBarValueChanged(object sender, EventArgs e)
        {
            // Call method that raises the new event
            OnColorChanged(new ColorEventArgs(trackBarRed.Value, trackBarGreen.Value, trackBarBlue.Value));
        }
    }
}


// FormPrimary.cs
using System.Windows.Forms;

namespace ColorChanger
{
    public partial class FormPrimary : Form
    {
        public FormPrimary()
        {
            InitializeComponent();

            // formSecondary
            FormSecondary formSecondary = new FormSecondary();
            // Subscribe to new event
            formSecondary.ColorChanged += formSecondary_ColorChanged;
            formSecondary.Show();
        }

        // Handle new event
        void formSecondary_ColorChanged(object sender, ColorEventArgs e)
        {
            BackColor = e.Color;
            Text = string.Format("R={0}, G={1}, B={2}",
                BackColor.R,
                BackColor.G,
                BackColor.B);
        }
    }
}


如果您无法从评论中找出问题所在,那么建议您查看我的事件简单 [ ^ ]文章或某些文章与此主题相关的其他优秀文章/博客.


If you can''t figure out what''s going on from the comments then I suggest you look at my Events Made Simple[^] article or some of the other excellent articles/blogs that are around on this subject.


这篇关于从次要控制主要形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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