How do I unsubscribe event?

patrick

Well-known member
Joined
Dec 5, 2021
Messages
305
Programming Experience
1-3
hello
How do I unsubscribe?


public delegate void UIChangedEventHandler(object sender, object e);
public event UIChangedEventHandler UIChanged;

UIChanged?.Invoke(sender, message);

How do I unsubscribe?
==> UIChanged -= UIChangedEventHandler(object sender, object e)
Is that correct?
 



Keyboard input does not work.
It's because of threads...
KeyPress Event does not work...
Its becasue of thread invoke

C#:
public string str;
public delegate void UIChangedEventHandler(object sender, object e);
public event UIChangedEventHandler UIChanged;

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
           UIChanged?.Invoke(sender, str);
}


C#:
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
           KeyPress Event does not work...
           becasue of thread Invoke
}




Please help me enter KeyPress event in Invoke state.


I can't actually code.

Please Help me... Please...
 
Last edited:
What does this question have to do with unsubscribing an event? It sounds like your problem right now is that you are either not getting any event notifications, or you are not able to fire your own UIChanged event?

How is listbox1_Keypress() related to listbox1_SelectedIndexChanged()? What does one have to do with the other?

Is the problem that listbox1_KeyPressed() is not being called?

How did you come to the conclusion that the problem has to do with threads? You said that you are not getting any errors, but Visual Studio would have thrown an exception if you were trying to call across threads.

As an aside, there is a difference your own UIChanged?.Invoke() vs trying listbox1?.Invoke(). The former is just a regular invocation of a delegate. The latter does a postback into Windows message queue so the delegate is run on the UI thread later.

I highly recommend putting together a complete and minimal code and sharing it with us. And then follow that up with a clear and complete explanation of what you are trying to achieve, and what behavior you are seeing. Tell us about what things you have tried.

Just saying "X doesn't work" doesn't provide enough information. I could equally say "my friend John doesn't work". It would be better to say that "my friend John just sits on the couch watching TV all day, eating all my snacks, and drinking all my beer".
 
Last edited:
Keyboard input does not work.
It's because of threads...
Do not enter here KeyPress.
KeyPress Event is not called.
becasue of thread Invoke

C#:
If there is [ UIChanged?.Invoke(sender, str); ]  in  [ listBox1_SelectedIndexChanged ]
1.  After clicking the first index in the ListBox, I pressed the keyboard arrow key.
2,. KeyPress Event is not called.
3.  BUT,  I pressed the keyboard arrow ↑ , ↓  key, 
but  [ listBox1_KeyPress]  was not called, but [ listBox1_SelectedIndexChanged ]  was called.

C#:
If you delete [ UIChanged?.Invoke(sender, str); ] from listBox1_SelectedIndexChanged,
1.  After clicking the first index in the ListBox, I pressed the keyboard arrow key.
2. KeyPress Event is called.
3. After, [ listBox1_SelectedIndexChanged ]  is called.

C#:
public string str;
public delegate void UIChangedEventHandler(object sender, object e);
public event UIChangedEventHandler UIChanged;

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
             str = "abc";
            UIChanged?.Invoke(sender, str);
}

C#:
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
            Do not enter here KeyPress.
           KeyPress Event is not called.
           becasue of thread Invoke
}



Please help me enter KeyPress event in Invoke state.


I can't actually code.

Please Help me... Please...
 
Last edited:
You are still showing us incomplete code. Show us the code of the delegate that you are setting UIChanged to. You might be accidentally setting yourself up for re-entrancy bugs.
 
I am only seeing KeyDown events for the arrow keys. I'm seeing KeyPress events when I start typing text. This is expected if you read Jessica Fosler's blog post regarding WinForms keyboard input.


1728149380664.png


C#:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing;

namespace WinFormsNetCore
{
    class MainForm : Form
    {
        public delegate void UIChangedEventHandler(object sender, object e);
        public event UIChangedEventHandler UIChanged;

        readonly TextBox _textBox;
        readonly ListBox _listBox;

        MainForm()
        {
            _listBox = new ListBox()
            {
                Dock = DockStyle.Left,
                TabIndex = 0,
            };

            var splitter = new Splitter()
            {
                Dock = DockStyle.Left,
                MinSize = 100,
                MinExtra = 100,
                SplitPosition = 200,
                TabIndex = 1,
            };

            _textBox = new TextBox()
            {
                Dock = DockStyle.Fill,
                ReadOnly = true,
                Multiline = true,
                TabIndex = 2,
            };

            for (int i = 0; i < 10; i++)
                _listBox.Items.Add($"Item {i + 1}");

            _listBox.SelectedIndexChanged += Listbox_SelectedIndexChanged;
            _listBox.KeyDown += ListBox_KeyDown;
            _listBox.KeyPress += ListBox_KeyPress;

            Size = new Size(600, 400);
            SuspendLayout();
            Controls.AddRange(new Control[] { _textBox, splitter, _listBox });
            ResumeLayout(true);
        }

        private void Listbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            _textBox.AppendText($"{DateTime.Now}: SelectedIndexChanged\r\n");
        }

        private void ListBox_KeyDown(object sender, KeyEventArgs e)
        {
            _textBox.AppendText($"{DateTime.Now}: KeyDown\r\n");
        }

        private void ListBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            _textBox.AppendText($"{DateTime.Now}: KeyPress\r\n");
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}
 
And here is with the UIChanged event notification hooked up. Notice that the KeyPressed and KeyDown events still work:
C#:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing;

namespace WinFormsNetCore
{
    class MainForm : Form
    {
        public delegate void UIChangedEventHandler(object sender, object e);
        public event UIChangedEventHandler UIChanged;

        readonly TextBox _textBox;
        readonly ListBox _listBox;

        MainForm()
        {
            _listBox = new ListBox()
            {
                Dock = DockStyle.Left,
                TabIndex = 0,
            };

            var splitter = new Splitter()
            {
                Dock = DockStyle.Left,
                MinSize = 100,
                MinExtra = 100,
                SplitPosition = 200,
                TabIndex = 1,
            };

            _textBox = new TextBox()
            {
                Dock = DockStyle.Fill,
                ReadOnly = true,
                Multiline = true,
                TabIndex = 2,
            };

            for (int i = 0; i < 10; i++)
                _listBox.Items.Add($"Item {i + 1}");

            _listBox.SelectedIndexChanged += Listbox_SelectedIndexChanged;
            _listBox.KeyDown += ListBox_KeyDown;
            _listBox.KeyPress += ListBox_KeyPress;

            UIChanged += MainForm_UIChanged;

            Size = new Size(600, 400);
            SuspendLayout();
            Controls.AddRange(new Control[] { _textBox, splitter, _listBox });
            ResumeLayout(true);
        }

        private void Listbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            _textBox.AppendText($"{DateTime.Now}: SelectedIndexChanged\r\n");

            var listBox = (ListBox) sender;
            UIChanged?.Invoke(this, listBox.Items[listBox.SelectedIndex].ToString());
        }

        private void ListBox_KeyDown(object sender, KeyEventArgs e)
        {
            _textBox.AppendText($"{DateTime.Now}: KeyDown\r\n");
        }

        private void ListBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            _textBox.AppendText($"{DateTime.Now}: KeyPress\r\n");
        }

        private void MainForm_UIChanged(object sender, object e)
        {
            _textBox.AppendText($"{DateTime.Now}: UIChanged {e}");
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}

1728155896040.png
 
Back
Top Bottom