Solved

Is it possible to pass a variable into an EventHandler? (C#)

Posted on 2008-10-24
15
1,123 Views
Last Modified: 2013-12-17
This may or may not be possible, but any help would be appreciated. I am subscribing to an event:

currentWindow.SizeChanged += new EventHandler(currentWindow_SizeChanged);

that fires this method:

private void currentWindow_SizeChanged(object sender, EventArgs e)
        {
//do stuff
        }

Is it possible to somehow pass the string into the method so my //do stuff portion can use it?
0
Comment
Question by:Pretzel_Jesus
  • 8
  • 7
15 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22800794
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22800799
Wait... Why do you want the size changed event handler to pass in an event? What exactly do you need
0
 

Author Comment

by:Pretzel_Jesus
ID: 22800810
Its kind of hard to explain, but essentially every time I press a button a new form window will appear using some textbox variables to open a new window, connect to a server and begin to stream video from it. Since these windows can be an quantity and created at will I have been putting the assoicated connections into a hashtable so I can retrieve them later as needed. At any rate I identify each window (connection) by their ip address in the hash table. So in order to ensure I am resizing the correct window I am trying to pass its ip so I can look it up in the hashtable. Is there a better way to accomplish what I am trying to do?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:RishadanPort
ID: 22800820
The event is invoked internally, when the size of a certain control has been changed. You do not have access to what is passed in from when the size event is called. You can however do something like this:

currentWindow.SizeChanged += new EventHandler(currentWindow_SizeChanged);

private void currentWindow_SizeChanged(object sender, EventArgs e)
{
     doStuffFunction(....)   <-- pass in arguments here
}

public void doStuffFunction(string arg){
}
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22800829
Why not just typecast the object parameter to windows form, and determine which form you are doing from that?
0
 

Author Comment

by:Pretzel_Jesus
ID: 22800832
I am self taught and still learning at the quickest pace I can. I do not know what you mean by this:

"Why not just typecast the object parameter to windows form, and determine which form you are doing from that?"

Sorry one problem with being self taught is that some complex concepts I understand perfectly and some of the simplest go right over my head. Can you give me an example of this?
0
 

Author Comment

by:Pretzel_Jesus
ID: 22800834
What I really need to know at the time the window is resizing is basically the IP address of the particular window being resized at that particular time.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22800841
1. Create a wrapper class around your windows form or what not
2. Have a variable that stores like the IP of this windows form... or whatever you want
3. handle the SizeChanged event --> currentWindow.SizeChanged += ...
4. in the SizeChanged event do something like this:

private void currentWindow_SizeChanged(object sender, EventArgs e)
{
     if(sender is WindowsFormWithUniqueID){
        WindowsFormWithUniqueID form = (WindowsFormWithUniqueID)sender;
        //now you have access to the form's unique IP or whatever
        string IP = form.IP;
     }
}
0
 

Author Comment

by:Pretzel_Jesus
ID: 22800845
Thats exactly what I am trying to do except the only way I currently have to identify any given form is by IP. So I still do not understand how to say:

"if(sender is WindowsFormWithUniqueID)"

If I need my IP address to make the form unique..
0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 500 total points
ID: 22800882
ok no problem. This is what you need to do..

See below
Step #1 Create your Form class that inherits a windows form
 
public class WindowsFormWithUniqueID : Form{
   ... put methods, constructors, or what not you want here...
 
   //Step #2 Define other properties
   private string ipAddress;
   private string uniqueID;
 
   public string IP{
      get{ return this.ipAddress; }
   }
 
   public string UniqueID{
      get{ return this.uniqueID;
   }
}
 
 
//Step 3 Initialize this type of form, and handle the event you want
 
public class Program{
   public Program(){
       //Create WindowsFormWithUniqueID object
       WindowsFormWithUniqueID form = new WindowsFormWithUniqueID(...); <-- pass in variables here
 
       //Handle event
       form.SizeChanged += new EventHandler(currentWindow_SizeChanged);
   }
 
   //create method, and determine if the sender is the 
   //WindowsFormWithUniqueID class
   private void currentWindow_SizeChanged(object sender, EventArgs e){
      if(sender is WindowsFormWithUniqueID){
            WindowsFormWithUniqueID form = (WindowsFormWithUniqueID)sender;
            //Now you have access to the IP and uniqueID
            MessageBox.Show("UniqueID = " + form.UniqueID);
            MessageBox.Show("IP = " + form.IP);
      }
   }
}

Open in new window

0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22800906
This statement:

"if(sender is WindowsFormWithUniqueID)"

is needed so when you do this:
WindowsFormWithUniqueID form = (WindowsFormWithUniqueID)sender;
you don't run into an exception.  You need to typecast the object to a WindowsFormWithUniqueID in order to get the IP / uniqueID defined in that class
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22800981
The thing that I am puzzled with is this statement:

"So in order to ensure I am resizing the correct window I am trying to pass its ip so I can look it up in the hashtable. Is there a better way to accomplish what I am trying to do?"

The SizeChanged event is called when a given window's size has been changed. That window calls this sizechanged event to all the objects that subscribed to the event.  --- However, it seems as if you, yourself, want to resize a window (not knowing which) by using the IP. This is not the same type of thing.
0
 

Author Comment

by:Pretzel_Jesus
ID: 22801020
I am dynamically creating the form instances from a single instance of a precreated form. So technically all of my windows will be firing off of the same event. Here is what happens when I click the button:

string ipAddress = textBox1.Text;
                string controlPort = "1518";
                string username = textBox2.Text;
                string password = textBox3.Text;
                string domain = "";
                string cameraName = "Camera Name";
                string cameraGuid = comboBox1.SelectedItem.ToString();
                string info = ipAddress + "|" + controlPort + "|" + username + "|" + password + "|" + domain + "|" + cameraName + "|" + cameraGuid;
                liveVideo test = new liveVideo();
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += new DoWorkEventHandler(test.connect);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                Video_Window videoWindow = new Video_Window();
                clientTable.Add(ipAddress + "currentConnection", test);
                clientTable.Add(ipAddress + "videoWindow", videoWindow);
                worker.RunWorkerAsync(info);

Since its takes a second or two to connect, I needed to run the connections in background workers so the ui doesnt freeze up. So I guess by subscribing to the resize event here all my created windows will use it.
0
 

Author Comment

by:Pretzel_Jesus
ID: 22801025
My main problem is I need to resize the panel on the form... so I am trying to reference the proper form so when the form resizes so does the appropriate panel:

private void currentWindow_SizeChanged(object sender, EventArgs e)
        {
                      currentWindow.Panel5.size = currentWindow.Size
        }

Does that make sense? Sorry its hard for me to describe this properly because of the way I learned to do things :).
0
 

Author Closing Comment

by:Pretzel_Jesus
ID: 31509854
Actually after looking over this I think I understand what is going on. Thanks for sticking with me on this one I appreciate it :).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question