Click here to Skip to main content
15,920,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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();
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();
   }
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.
 
Share this answer
 
As Griff mentioned, this should really be done by raising an event in the secondary form. Here's a working example:
C#
// 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; }
        }
    }
}

C#
// 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));
        }
    }
}

C#
// 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.
 
Share this answer
 
Comments
OriginalGriff 13-Jun-10 10:09am    
I thought of sending him over to your article, but I thought that if he gets confused by the 'new' keyword then actual code may be a thing for the future... :laugh:
DaveyM69 13-Jun-10 10:37am    
ROFL - Good point!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900