0

I'm working in VisualStudio.

I have this Form:

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 Test
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }

        public static int signal = 0;

        public void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

And this User Control:

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 Test
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();

            if (Form1.signal == 1)
            {
                MessageBox.Show("Signal received!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {

        }
    }
}

I bust my head to try to display the 'MessageBox' from 'UserControl1' when 'button1' from 'Form1' is clicked. Basically, I want to change the value of 'signal' to 1 when the 'button1' is pressed. I'm newbie but I'm pressed by time here so a good help will be very welcome. Any ideas? Thank you!

ZiggZagg
  • 1,397
  • 11
  • 16
Bogdan
  • 3
  • 1
  • 2
    Even if you change the value of the variable "signal" in your Form, how can the UserControl now when this happens? You need to define a custom **event**, define the corresponding event handler, and connect the two. – Sergio Monteleone Apr 15 '18 at 09:35
  • Like I said, I'm newbie and my knowledge is quite limited. Of course the best way to solve this is to take the time and learn the program but unfortunately I don't have that time. I will do that later. An example with my case of what you said would be great! – Bogdan Apr 15 '18 at 09:38

3 Answers3

1

The button1_Click is the event that is going to be triggered when you click on the button from the Form.

Either create an event like another user suggested, or refer to this question and create a custom message box with your UserControl as its content.

Jonathan Perry
  • 2,953
  • 2
  • 44
  • 51
0

When your UserControl is loaded it sees Form1.signal as 0 since you initialized it with that value. Your UserControl1 will never be aware of the change on Form1.

What you need to do is make listener function in UserControl1 that will be triggered, let's say every 10milliseconds and that will check if Form1.signal == 1.

Check Interval library for that, and I suggest you find time to learn a bit of C#. The language is awesome.

Lazar Nikolic
  • 4,261
  • 1
  • 22
  • 46
  • I will do that for sure! I'm just very pressed by time now to make a small project. But, will the signal be changed to 1 when button is pressed? It will be changed inside the button but will this be transmitted outside? I know how to make intervals but I don't know how to make a listener. – Bogdan Apr 15 '18 at 09:53
  • Yes it will.When you change value of your signal variable, your listener function will know that something like that happened. Listener function will be called wit the help of interval every 10 milliseconds (for example) – Lazar Nikolic Apr 15 '18 at 09:58
  • I put an 'await Task.Delay(10);' besides the 'if' statement with the 'MesageBox' inside a 'while' loop and it worked. Is this what a listener suppose to be or there is something predefined in library? – Bogdan Apr 15 '18 at 10:32
0

The best way to deal with communication between containers is to implement an observer class

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. (wikipedia)

the way i do this is creating an Observer class:

1    public delegate void dlFuncToBeImplemented(int signal);
2    public static event dlFuncToBeImplemented OnFuncToBeImplemented;
3    public static void FuncToBeImplemented(int signal)
4    {
5         OnFuncToBeImplemented(signal);
6    }

so basically: first line says that there would be a function that somebody else will implement

second line is creating an event that occur when the delegated function will call

and the third line is the creation of the function that calls the event

so in your UserControl you should add a function like this:

private void ObserverRegister()//will contain all observer function registration
{
    Observer.OnFuncToBeImplemented += Observer_OnFuncToBeImplemented;
    /*and more observer function registration............*/
}


void Observer_OnFuncToBeImplemented(int signal)//the function that will occur when  FuncToBeImplemented(signal) will call 
{
    MessageBox.Show("Signal received!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

and in your Form you should do something like:

public static int signal = 0;

public void button1_Click(object sender, EventArgs e)
{
      Observer.FuncToBeImplemented(signal);//will call the event in the user control
}

and now, you can register this function to a whole bunch of other controls and containers and they will all get the signal

I hope this would help :)

Dor Lugasi-Gal
  • 1,430
  • 13
  • 35