Home > other >  Pass Strings through click EventHandler
Pass Strings through click EventHandler

Time:01-08

I'm trying to send 3 strings to a void/EventHandler

 button.Click  = new DownloadGame(gameZip, gameExe, gameTitle);

private void DownloadGame(string gameZip, string gameExe, string gameTitle)
        {
            if (!File.Exists(gameExe))
            {
                MessageBox.Show("Already Installed!");
            }
            string GamesDirectory = Path.Combine(Environment.CurrentDirectory, "Games");
            if (!Directory.Exists(GamesDirectory))
                Directory.CreateDirectory(GamesDirectory);
            InstallGameFiles(Path.Combine(Directory.GetCurrentDirectory(), "Build", gameExe), gameZip, gameTitle);

How can I call the method with the arguments without this error?

Error CS0246 The type or namespace name 'DownloadGame' could not be found (are you missing a using directive or an assembly reference?)

CodePudding user response:

You cannot pass string to Click event. But you can create variable/property under class where this button belong. Also, when you are adding function to Event, you can't use 'new' keyword.

string gameZip, gameExe, gameTitle; // Set those variables i.e. here

button.Click  = DownloadGame;

private void DownloadGame(Object sender, EventArgs e)
{
    if (!File.Exists(gameExe))
    {
        MessageBox.Show("Already Installed!");
    }
    string GamesDirectory = Path.Combine(Environment.CurrentDirectory, "Games");
    if (!Directory.Exists(GamesDirectory))
        Directory.CreateDirectory(GamesDirectory);
    InstallGameFiles(Path.Combine(Directory.GetCurrentDirectory(), "Build", gameExe), gameZip, gameTitle);   
}

CodePudding user response:

The reason you're getting Error CS0246. is the new keyword:

// I N C O R R E C T
button.Click  = new DownloadGame(gameZip, gameExe, gameTitle);

Because of the new, the compiler is attempting to make a new instance of a class named DownloadGame. It can't find a class by that name because it doesn't exist (there is only a method by that name, thus the error).


The other reason that your syntax is incorrect is that the delegate signature must match. For example, a Winforms button handler would assign like this:

public MainForm()
{
    buttonDownload.Click  = onClickDownloadButton;
}
private void onClickDownloadButton(object sender, EventArgs e)
{
}

Regardless of the platform, once you type button.Click = the intellisense should offer to make a method with the correct signature.

And as far as what your code is attempting to do, handling the Click event is to receive the event and data that the Button class is notifying and is not intended to send strings or anything else.


The thing that would seem to make sense here is that you have already performed other UI actions to populate some of these file names. For example you may have a button or menu that invokes an "open file" flow, or a settings prompt (example).

private void onClickDownloadButton(object sender, EventArgs e)
{
    DownloadGame();
}
public void DownloadGame()
{
    if (!File.Exists(gameExe))
    {
        MessageBox.Show("Already Installed!");
    }
    string GamesDirectory = Path.Combine(Environment.CurrentDirectory, "Games");
    if (!Directory.Exists(GamesDirectory))
    {
        Directory.CreateDirectory(GamesDirectory);
    }
    InstallGameFiles(Path.Combine(Directory.GetCurrentDirectory(), "Build", gameExe), gameZip, gameTitle);
}
// These need to be populated some other 
// way before enabling buttonDownload.    
private string gameZip;
private string gameExe;
private string gameTitle;
  • Related