-1

I'm trying to pass a program name from my form to a button click event. But I keep getting the following error message:

"No overload for “method” matches delegate 'EventHandler' "

I've read up on similar problems here and tried numerous suggestions but nothing seems to fix my specific problem. I'm hoping someone can tell me where I'm going wrong. Here are some of the posts I've read:

No overload for 'method' matches delegate 'System.EventHandler'

C# CS0123: No overload for matches delegate Eventhandler

Error - No overload for "method" matches delegate 'System.EventHandler'?

I don't seem to have a clear understanding of how to match up the signatures of my event methods. The documentation I've read hasn't helped. Any assistance from a knowledgeable person would be greatly appreciated.

My code is as follows:

using System;
using System.Windows.Forms;


namespace ProgramOne
{
    public partial class frmLogin : Form
    {
        public frmLogin(string pgmName)
        {
            InitializeComponent();
            Click += (sender, EventArgs) => { BtnSubmit_Click(sender, EventArgs, pgmName); };
        }


        private void BtnSubmit_Click(object sender, EventArgs e, string pgmName)
        {
            txtUserId.Text = txtUserId.Text.Trim();
            txtPassword.Text = txtPassword.Text.Trim();
            if (txtUserId.Text == "" || txtPassword.Text == "")
            {
                MessageBox.Show("Please provide a valid UserID and Password");
                return;
            }

            bool securityCheckPassed = true;
            if (securityCheckPassed)
            {
                //Open new form
                MessageBox.Show(frmLogin.pgmName);
            }
        }
    }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
csharpMind
  • 89
  • 3
  • 14
  • `sender, EventArgs` has two parameters and `BtnSubmit_Click` has three parameters. This assignment won't work. – Chetan Jun 09 '21 at 19:07
  • 2
    Store the pgmName in some private variable and use it in button click directly instead of trying to pass a parameter to it. – Chetan Jun 09 '21 at 19:09
  • Statement should be `Click += (sender, e) => { BtnSubmit_Click(sender, e, pgmName); };` – jalepi Jun 09 '21 at 19:10
  • `Click += (s, e) => BtnSubmit_Click(s, e, pgmName);` but I find the design doubtful. –  Jun 09 '21 at 19:11
  • user1344783 That change didn't work. I got the same error. – csharpMind Jun 09 '21 at 19:17
  • Olivier Rogier I tried your suggestion as well, but I got the same error. – csharpMind Jun 09 '21 at 19:18
  • Chetan Ranpariya I would like to do that, but the program name is found outside the click event. How do I get the name into a variable within the BtnSubmit event handler? – csharpMind Jun 09 '21 at 19:21
  • 1
    Just declare it: `private string pgmName = "";` Then in the constructor, set it: `this.pgmName = pgmName;` Your message box would just reference it: `MessageBox.Show(pgmName);` – LarsTech Jun 09 '21 at 19:24
  • LarsTech Thank you very much for pointing me in the right direction. It worked like a champ. I'm going to post the modified code that you and Chetan Ranpariya recommended so other newbies (like me) can be helped. – csharpMind Jun 09 '21 at 19:50
  • 1
    We don't put answers in the question here. Just post your answer in the box provided. – LarsTech Jun 09 '21 at 20:14
  • I edited my original post with the answer. Is that incorrect? I'll put it in the Answer box if necessary. I thought that box was only for others to use. – csharpMind Jun 09 '21 at 20:21
  • 1
    See [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – LarsTech Jun 09 '21 at 20:35
  • Thanks again LarsTech. I'll update the answer. – csharpMind Jun 09 '21 at 20:59

1 Answers1

2

LarsTech and Chetan Ranpariya provided excellent suggestions that helped me solve this problem. My solution appears below for other C# newbies like me to use:

using System;
using System.Windows.Forms;

namespace ProgramOne
{
    public partial class frmLogin : Form
    {
        private string pgmName;                      <-- Declare the variable
        public frmLogin(string pgmName)
        {
            InitializeComponent();
            this.pgmName = pgmName;                  <-- Initialize with passed value
        }

        private void BtnSubmit_Click(object sender, EventArgs e)  <-- Remove pgmName from original code above
        {
            txtUserId.Text = txtUserId.Text.Trim();
            txtPassword.Text = txtPassword.Text.Trim();
            if (txtUserId.Text == "" || txtPassword.Text == "")
            {
                MessageBox.Show("Please provide a valid UserID and Password");
                return;
            }

            bool securityCheckPassed = true;
            if (securityCheckPassed)
            {
                //Open new form
                MessageBox.Show(pgmName);           <-- Access the name
            }
        }
    }
}

The program name now shows up in the BtnSubmit_Click event handler for me to use to launch subsequent forms as needed. Just replace "MessageBox.Show(pgmName);" with "GoToForm(frmName);" (replacing the sample name values with your own names).

csharpMind
  • 89
  • 3
  • 14