Thread: event handler

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    11

    event handler

    Hello im having a problem with the following , i want the Apply button in the dialog box from the File->open , to display the input from the textbox in a messagebox without closing the dialog box.

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.Windows.Forms;
    
    public class ADialog:Form
    {
    	Label lb1;
    	TextBox tx1;
    	Button bt1;
    
    	public  event EventHandler Apply;
    
    	string subject;
    	
    	public ADialog()
    	{
    		ClientSize=new Size(350,350);
    		Text="Appointment Information";
    		FormBorderStyle=FormBorderStyle.FixedDialog;
    		this.ControlBox=false;
    		this.MaximizeBox=false;
    		this.MinimizeBox=false;
    		this.ShowInTaskbar=false;
    				
    		bt1=new Button(); bt1.Parent=this; bt1.Enabled=true; 
    		bt1.Text="Apply";
    		bt1.Location=new Point(20,300);
    		bt1.Click+=new EventHandler(bt1_Click);
    	
    		lb1=new Label();
    		lb1.Size=new Size(160,40);
    		lb1.Text="Appointment Subject";
    		lb1.Location=new Point(20,20);
    	
    		tx1=new TextBox();
    		tx1.Location=new Point(180,20);
    		tx1.Size=new Size(150,30);
    
    		this.Controls.Add(lb1);
    		this.Controls.Add(tx1);
    		this.Controls.Add(bt1);
    	}
    	public string Subject
    	{
    		get
    		{
    			return subject;
    		}
    		set
    		{
    			subject=value;
    		}
    	}
    
    	public bool ShowApply
    	{
    		get{ return bt1.Enabled;}
    		set{bt1.Enabled=value;}
    	}
    
    	private void bt1_Click(object sender, EventArgs e)
    	{
    		
    		subject=tx1.Text;
    		if(Apply!=null)	
    			Apply(this,new EventArgs());
    	}
    }
    
    class test:Form
    {
    	MenuItem File;
    	MainMenu mainMenu;
    
    	test()
    	{
    		MenuItem myOpen = new MenuItem("&Open", new EventHandler(MenuFileOpenOnClick), Shortcut.CtrlO);
    		File = new MenuItem("&File", new MenuItem[] { myOpen });
    		mainMenu = new MainMenu(new MenuItem[] { File });
    		this.Menu = mainMenu;
    	
    	}
    	public static void Main()
    	{
    		Application.Run(new test());
    	}
    	private void MenuFileOpenOnClick(object sender, EventArgs e)
    	{
    		ADialog dlg=new ADialog();
    		dlg.ShowDialog();
    		dlg.Apply+=new EventHandler(dlg_Apply);
    	}
    
    	private void dlg_Apply(object sender, EventArgs e)
    	{
    MessageBox.Show(((ADialog)sender).Subject);
    	}
    }
    When i click on Apply i want to display in the message box the input in the textbox.

    please help.

    thanks.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    In general, it's a very bad idea to cast the sender argument of an event and try to use it. I don't know why Microsoft designed their event structure like this, but there you go.

    I don't have a C# compiler anywhere around, but it looks like you really want to be passing the subject in the EventArgs. You can do this a couple of different ways. If you're using 2005, you can use the generic EventHandler to send a string:
    Code:
    public event EventHandler<string> Apply;
    Then you can send the string in your Apply.
    Code:
    if(Apply!=null)	
    	Apply(this,tx1.Text);
    Of course, then your handler has to look a bit different.
    Code:
    private void dlg_Apply(object sender, string e)
    {
    	MessageBox.Show(e);
    }
    If you're not using 2005, you have to derive a class from EventArgs that includes a string.
    Code:
    public class StringEventArgs : EventArgs
    {
       public readonly Str;
    
       public StringEventArgs(string str) : base()
       {
          Str = str;
       }
    }
    Then when you fire Apply, you should send your new EventArgs class:
    Code:
    if(Apply!=null)	
    	Apply(this,new StringEventArgs(tx1.Text));
    Finally, in your handler code, you should cast the args so you can use it.
    Code:
    private void dlg_Apply(object sender, EventArgs e)
    {
    	StringEventArgs args = e as StringEventArgs;
    	if (args != null)
    		MessageBox.Show(args.Str);
    }
    That's the general idea anyway. I wouldn't be surprised if it doesn't compile; I might have forgotten something.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  3. Finding controls from an event handler
    By bennyandthejets in forum C# Programming
    Replies: 2
    Last Post: 07-03-2005, 04:17 AM
  4. event handler for an array of buttons
    By GanglyLamb in forum C# Programming
    Replies: 4
    Last Post: 04-12-2005, 09:52 AM