Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1132
  • Last Modified:

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

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
Pretzel_Jesus
Asked:
Pretzel_Jesus
  • 8
  • 7
1 Solution
 
RishadanPortCommented:
Wait... Why do you want the size changed event handler to pass in an event? What exactly do you need
0
 
Pretzel_JesusAuthor Commented:
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
RishadanPortCommented:
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
 
RishadanPortCommented:
Why not just typecast the object parameter to windows form, and determine which form you are doing from that?
0
 
Pretzel_JesusAuthor Commented:
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
 
Pretzel_JesusAuthor Commented:
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
 
RishadanPortCommented:
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
 
Pretzel_JesusAuthor Commented:
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
 
RishadanPortCommented:
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
 
RishadanPortCommented:
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
 
RishadanPortCommented:
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
 
Pretzel_JesusAuthor Commented:
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
 
Pretzel_JesusAuthor Commented:
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
 
Pretzel_JesusAuthor Commented:
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

Independent Software Vendors: 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!

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now