How do I write and call an event between classes for Winforms?

I am attempting to write a Winforms application that interfaces with an SQLite server for a class. One requirement is for users to be able to make an account, so I need to remove the login control and replace it with the actual app control. I figured that the best solution would be to make an event, but I cannot for the life of me get it to work. I’m not sure how to use delegates, but from my understanding I don’t strictly need a delegate for this purpose.

The code here is meant to be setting up an event that is called every time the user authentication goes through correctly. For now it just runs as long as any text at all is put inside of the username and password textboxes.

public partial class LoginControl : UserControl
    {
    public LoginControl()
    {
    InitializeComponent();
    }
            //event and bool for handling the login process

        String exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        string path = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;


        private bool isTrue = false;
        public EventHandler boolIsChanged;
        public void OnChange()
        {
            boolIsChanged?.Invoke(this, EventArgs.Empty);
        }
        private void btn_SubmitLogin_Click(object sender, EventArgs e)
        {
            // Example logic to handle login submission
            string password = txtbx_Password.Text;
            string username = txtbx_Username.Text;

            // Perform your authentication logic here
            bool loginSuccessful = AuthenticateUser(username, password);

            if (loginSuccessful)
            {
                MessageBox.Show("Login successful!");
                OnChange();
            }
            else
            {
                MessageBox.Show("Login failed. Please try again.");
            }
        }

The logic is (to the best of my knowledge) not called here, and is instead called from the Form directly. The code in the Form is this:

        private LoginControl loginControl;
        private AddFilesControl addFilesControl;
        public Form1()
        {
            InitializeComponent();
            loginControl = new LoginControl();
            loginControl.Dock = DockStyle.Fill;
            this.Controls.Add(loginControl);

            // Initialize AddFilesControl
            addFilesControl = new AddFilesControl();
            addFilesControl.Dock = DockStyle.Fill;
            addFilesControl.Visible = false; // Start with AddFilesControl hidden
            this.Controls.Add(addFilesControl);
        }

        
        public void LoginControl_LoginSuccessChanged()
        {
            loginControl.boolIsChanged += (s, args) =>
            {
                MessageBox.Show("Bool Changed I think");
            };
        }

The logic for what happens is in LoginControl_LoginSuccessChanged(), for now the placeholder is just showing a MessageBox. This functions purpose right now is solely to make sure that the event is being called properly. The MessageBox of course doesn’t show up because LoginControl_LoginSuccessChanged is never called or referenced. I know that I need to make sure this function is called properly, but I’m unsure of how or where to do that. I was under the impression that it would just go when the event activated, but now I’m not so sure about that. I believe that I can’t put the logic for what happens in Form1 inside of the loginControl, so putting the logic inside of that class isn’t an option for me.

New contributor

BenClites is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

  • Your public EventHandler boolIsChanged; line is definition of delegate field, not an event.
    You should use public event EventHandler boolIsChanged; to make an event.
  • Your code example is incomplete to replicate your problem.
    Form1.LoginControl_LoginSuccessChanged() is not called.
    Method name suggests that it’s meant to be an event handler, but neither it doesn’t follow EventHandler delegate convention, nor it doesn’t subscribe into any visible events.
  • If Form1.LoginControl_LoginSuccessChanged() is called in your code but not included in your question, It can be an order of execution issue.
    Form1.LoginControl_LoginSuccessChanged() should be invoked BEFORE LoginControl.OnChange() to make your “meant-to-be-anonymous-event-handler” properly invoked.
  • And I think subscribing an event on potentially-being-called-multiple-times event handler without check is a bad idea. It would cause duplicate subscription.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật