none
How to pass a string from one event handler to another event handler?

    Question

  • I am trying to get a string from one even handler to another, but can't figure out what is going wrong.  I have a open file dialog box that comes up and from there I choose a file.  This is event one.  I would like to pass the filename chosen to another event handler that I have set up with a different string already set up to do other things.   I would even settle for using the same string in the second even if I can figure out how to do that.  Here is the code....

    private void chooseFileButton_Click(object sender, EventArgs e)

     {

    ...

    string filename = x;

    ...

    }

    private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)

    {

    ...

    string jobname = filename

    ....

    }




    Friday, August 10, 2012 3:17 PM

Answers

  • You cannot just pass some varibles between different controls by using events.

    - or you make this filename variable class accessible (it can be property as well)

    - or make a new event (custom one) with your own members (filelds, properties, methods) to trigger some method from some event (or the other way around).


    Mitja

    Friday, August 10, 2012 3:23 PM
  • @onemadscientist,

    are you trying to choose a file and process it within a BackgroundWorker and thus wanting to pass the file name?

    See http://msdn.microsoft.com/en-US/library/system.componentmodel.backgroundworker%28v=vs.80%29 for an explanation of the BackgroundWorker and have a look especially for the paragraph between the two yellow note blocks how to pass a parameter.

    If you need to pass more than one parameter I would recommend to create something like a "parameter class" to be passed to the worker.


    Cheers, Dany



    • Edited by DanyR_ Friday, August 10, 2012 4:52 PM spelling mistake
    • Marked as answer by onemadscientist Saturday, August 11, 2012 3:32 AM
    Friday, August 10, 2012 4:49 PM

All replies

  • Is this a web application? Windows form? etc...

    Web Developer

    Friday, August 10, 2012 3:18 PM
  • You cannot just pass some varibles between different controls by using events.

    - or you make this filename variable class accessible (it can be property as well)

    - or make a new event (custom one) with your own members (filelds, properties, methods) to trigger some method from some event (or the other way around).


    Mitja

    Friday, August 10, 2012 3:23 PM
  • Im agree with Mitja, just to complement, in case of a web application, you can do this with HiddenFields, viewstate, querystring etc..

    Web Developer

    Friday, August 10, 2012 3:32 PM
  • Im agree with Mitja, just to complement, in case of a web application, you can do this with HiddenFields, viewstate, querystring etc..

    Web Developer

    A web application won't use a BackgroundWorker.  Clearly that's not the case here.

    Friday, August 10, 2012 4:05 PM
  • Im agree with Mitja, just to complement, in case of a web application, you can do this with HiddenFields, viewstate, querystring etc..


    Web Developer

    A web application won't use a BackgroundWorker.  Clearly that's not the case here.

    As i say, IN CASE.


    Web Developer

    Friday, August 10, 2012 4:07 PM
  • Yes, it is not a web application, but a windows form that I am working with.
    Friday, August 10, 2012 4:21 PM
  • I'm not sure what you mean by making the variable class accessible.  Can you explain a bit more about this?
    Friday, August 10, 2012 4:21 PM
  • @onemadscientist,

    are you trying to choose a file and process it within a BackgroundWorker and thus wanting to pass the file name?

    See http://msdn.microsoft.com/en-US/library/system.componentmodel.backgroundworker%28v=vs.80%29 for an explanation of the BackgroundWorker and have a look especially for the paragraph between the two yellow note blocks how to pass a parameter.

    If you need to pass more than one parameter I would recommend to create something like a "parameter class" to be passed to the worker.


    Cheers, Dany



    • Edited by DanyR_ Friday, August 10, 2012 4:52 PM spelling mistake
    • Marked as answer by onemadscientist Saturday, August 11, 2012 3:32 AM
    Friday, August 10, 2012 4:49 PM