-1

In my program I have two windows opened at the same time, one is a WPF form and the other one is a WinForms form.

I need to change a label background color that's on a WPF form, by clicking a button that's placed on the WinForms form.

How can I do it?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2558874
  • 57
  • 2
  • 2
  • 5
  • Are they the same program? If so, you need an object reference of one form in the other, then thread the value through. – gunr2171 Oct 09 '13 at 19:23
  • Yes, it's the same program. If both of them were winforms forms I could do form2.label1.backcolor, but I don't know how to change a WPF background color. – user2558874 Oct 09 '13 at 19:28
  • this kind of application is funny. I think you should mix more things into it such as a `Command Prompt`? let them dance together and you will have fun :)) – King King Oct 09 '13 at 19:28
  • I didn't even _know_ you could have a winform and a WPF form in the same application. What is the reason behind this? Have you looked up changing the background of a WPF label normally? – gunr2171 Oct 09 '13 at 19:29
  • You should show your code so that we can help you better with less effort to suppose/guess/presume – King King Oct 09 '13 at 19:30

3 Answers3

1

You could set up an EventHandler to publish an event notification when the Button control in your WinForms form is pressed. This event would be monitored by your WPF Window/UserControl to detect when the event is triggered and then request the desired color to set your WPF Label to.

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="150" Width="225">
    <Grid>
        <Label Content="Label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="197" Background="{Binding LabelBgColour}"/>
    </Grid>
</Window>

MainWindow.xaml.cs

namespace WpfApplication1
{
    using System.Windows;
    using System.ComponentModel;
    using System.Windows.Media;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            var f = new Form1();
            f.ChangedColourEventHandler += TriggeredChangedColourEventHandler;
            f.Show();
        }

        private Brush labelBgColour;
        public Brush LabelBgColour
        {
            get
            {
                return this.labelBgColour;
            }
            set
            {
                this.labelBgColour = value;
                OnPropertyChanged();
            }
        }

        private void TriggeredChangedColourEventHandler(object sender, Brush color)
        {
            this.LabelBgColour = color;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

Form1.cs

namespace WpfApplication1
{
    using System;
    using System.Windows.Forms;
    using System.Windows.Media;

    public partial class Form1 : Form
    {
        public EventHandler<Brush> ChangedColourEventHandler;

        public Form1()
        {
            InitializeComponent();
        }

        private Brush bgColour;
        private Brush BgColour
        {
            get
            {
                return this.bgColour;
            }
            set
            {
                this.bgColour = value;
                TriggerChangedColourEventHandler(value);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.BgColour = (Brush)new BrushConverter().ConvertFromString("#FF00B25A");
        }

        private void TriggerChangedColourEventHandler(Brush color)
        {
            var handler = ChangedColourEventHandler;
            if (handler != null)
            {
                handler(this, color);
            }
        }
    }
}

Form1 has a single Button control named button1 (as can be determined from the example cs). I have omitted the Form1.Design.cs for brevity.

This example has the WPF window go to the WinForm to get the desired Color value. It could be easily adapted so that the Color value is sent as part of the EventHandler event (EventArgs).

  • 1
    Note that you can simplify the code a bit by just having the event pass the color as an argument of the method it fires, so that the handler doesn't need to reach back into the form to get the color. The primary advantage is that the WPF window can then forget about the form rather than holding onto it as an instance field. The general approach is exactly correct though. – Servy Oct 09 '13 at 20:47
  • The only thing I found in this answer is `BrushConverter`, the OP wants to know how to convert a hexadecimal color string into `Color` so your code is too redundant and I'm sure it may confuse the OP. – King King Oct 09 '13 at 20:59
  • @Servy That is the method I would usually take, pass the desired information as part of the event. For some reason I decided not to show it that way in this example. In light of your comment I have made amendments. –  Oct 09 '13 at 21:05
  • @KingKing I believe I have answered the question asked in the original post which was (paraphrasing) 'How do I change a WPF label background colour from a WinForm on a button press'. The OP may have subsequently requested information on how to convert a hex code to **other**, however, that was not part of the original question. If the OP finds the example confusing, he/she is free to ask questions for which any of us can assist. [: –  Oct 09 '13 at 21:10
  • Thanks! This is all I needed: this.BgColour = (Brush)new BrushConverter().ConvertFromString("#FF00B25A"); – user2558874 Oct 09 '13 at 22:23
0

I'm sure there are a lot of ways to achieve this. Some smaller and more obscure and some more longwinded but more clear.

Like you could do something through the win32 api, communicate through a memory-mapped file or use some form off communication transport like NamedPipes or MSMQ.

You can look at some of these resources for ideas:

Best of luck! :)

Community
  • 1
  • 1
Gaute Løken
  • 7,522
  • 3
  • 20
  • 38
  • I'm sure we don't need any kind of `IPC` here because the 2 windows are in the same program. – King King Oct 09 '13 at 19:31
  • Ahh.. I see that was clarified while I was researching the sources and typing out my answer. – Gaute Løken Oct 09 '13 at 19:32
  • With that premise in place, you could hook the button-clicked event on the form from the creating class, and have that listener call a method in the other form. – Gaute Løken Oct 09 '13 at 19:36
0

Assuming that the Winform object is available in WPF form and the Button1 is public, you need to add code similar to below in the WPF form.

WinForm.Button1.Click += new System.EventHandler(Button1_Click);

private void Button1_Click(object sender, EventArgs e)
{
// Change label background
}
monkxyz
  • 38
  • 4
  • 1
    Actually what I was looking for was what you didn't say (//Change label backgroud). I've just found that the code is wpfform.label1.Background = System.Windows.Media.Brushes.DarkRed; but I need to use an specific color like this #FF00B25A – user2558874 Oct 09 '13 at 19:39
  • You really need to learn to spend more time phrazing your problem, cause now you've had several people waste their time trying to solve problems you didn't have. – Gaute Løken Oct 09 '13 at 19:48
  • @user2558874 see my answer for the solution. – King King Oct 09 '13 at 19:58