473,385 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# :Windows App single event handler

Hi there,

I just started learning C# and i dont really have much experience with object oriented programming languages. I have to write a method in C# which can call all the panels. I dont know how to do this. My supervisor told me to do that. He doesn't want me to write the same code over and over again for the same purpose. I m stuck on this from a long time now. Any help will be appreciated. Thanks,
Shani

Expand|Select|Wrap|Line Numbers
  1.  
  2.         private void panel1ToolStripMenuItem_Click_1(object sender, EventArgs e)
  3.         {
  4.             if (panel1.Visible == true)
  5.             {
  6.                 this.panel1.Visible = false;
  7.             }
  8.             else
  9.             {
  10.                 this.panel1.Visible = true;
  11.             }
  12.         }
  13.  
  14.         private void panel2ToolStripMenuItem_Click_1(object sender, EventArgs e)
  15.         {
  16.             if (panel2.Visible == true)
  17.             {
  18.                 this.panel2.Visible = false;
  19.             }
  20.             else
  21.             {
  22.                 this.panel2.Visible = true;
  23.             }
  24.         }
  25.  
  26.         private void panel3ToolStripMenuItem_Click(object sender, EventArgs e)
  27.         {
  28.             if (panel3.Visible == true)
  29.             {
  30.                 this.panel3.Visible = false;
  31.             }
  32.             else
  33.             {
  34.                 this.panel3.Visible = true;
  35.             }
  36.         }
  37.  
  38.         private void panel4ToolStripMenuItem_Click(object sender, EventArgs e)
  39.         {
  40.             if (panel4.Visible == true)
  41.             {
  42.                 this.panel4.Visible = false;
  43.             }
  44.             else
  45.             {
  46.                 this.panel4.Visible = true;
  47.             }
  48.         }
  49.  
  50.         private void panel5ToolStripMenuItem_Click(object sender, EventArgs e)
  51.         {
  52.             if (panel5.Visible == true)
  53.             {
  54.                 this.panel5.Visible = false;
  55.             }
  56.             else
  57.             {
  58.                 this.panel5.Visible = true;
  59.             }     
  60.         }
  61.  
Nov 9 '07 #1
7 1441
Plater
7,872 Expert 4TB
So you need a single event handler that will be assigned to all of the menu items and that single event handler needs to know which panel to show/hide?

Use the "object sender" argument to determine what to do.
Maybe like:
Expand|Select|Wrap|Line Numbers
  1. private void MenuItem_Click(object sender, EventArgs e)
  2. {
  3.    if (sender ==panel1ToolStripMenuItem)
  4.    {//change Panel1
  5.       panel1.Visible = !Panel1.Visible;
  6.    }
  7.    else if (sender ==panel2ToolStripMenuItem)
  8.    {//change Panel2
  9.       panel2.Visible = !Panel2.Visible;
  10.    }
  11.    //etc etc etc
  12. }
  13.  

I can't really offer you much more then that. We're not supposed to do people's assignments for them.
Nov 9 '07 #2
JosAH
11,448 Expert 8TB
@OP: I've removed your identical post from the Java Forum; good luck.

kind regards,

Jos
Nov 9 '07 #3
Hey Plater,

Thanks for your reply. I tried what you suggested.
Expand|Select|Wrap|Line Numbers
  1. if (sender == panel1ToolStripMenuItem)
  2.             {//change Panel1
  3.                 panel1.Visible = !Panel1.Visible;
  4.             }
  5.             else if (sender == panel2ToolStripMenuItem)
  6.             {//change Panel2
  7.                 panel2.Visible = !Panel2.Visible;
  8.             }
  9.             else if (sender == panel3ToolStripMenuItem)
  10.             {//change Panel3
  11.                 panel3.Visible = !Panel3.Visible;
  12.             }
  13.             else if (sender == panel4ToolStripMenuItem)
  14.             {//change Panel4
  15.                 panel4.Visible = !Panel4.Visible;
  16.             }
  17.             else if (sender == panel5ToolStripMenuItem)
  18.             {//change Panel5
  19.                 panel5.Visible = !Panel5.Visible;
  20.             }
  21.  
It says that "The name 'Panel1' does not exist in the current context." When i run the windows form. It still works. May be it showing me the last successful built. When i had seperate evenhandler for each panel. How do i correct this?
Thanks again,
Nov 9 '07 #4
Plater
7,872 Expert 4TB
I thought I just copy/pasted what you had, maybe I misspelled/mis-capitalized your objects?
Nov 9 '07 #5
Expand|Select|Wrap|Line Numbers
  1. yes, i noticed later. You mis-capitalized. I commented out the code for each seperate panel and I ran what you suggested. Now, When i try to run the program, it gives me the following error.
  2.  
  3. 'WindowsApplication1.Form1' does not contain a definition for 'panel1ToolStripMenuItem_Click_1'
  4.  
  5. so does it shows the same error message for all panels. I went to the Designer form and I deleted         
  6.  
  7. this.panel1ToolStripMenuItem.Click += new System.EventHandler(this.panel1ToolStripMenuItem_Click_1);
  8.  
  9. for all panels. Now, when i run the program it gets built successfully. But when i click the menuitems panel 1, 2... it doesn't disappear the panels I dont want.
  10. Thanks,
  11.  
Nov 9 '07 #6
Plater
7,872 Expert 4TB
Go to the designer (like the visual part and not the code)
and select each menu item.
In the properties window click the lightening bolt to see the events.
In the Click event, select that same singular eventhandler for each menu item.
Nov 9 '07 #7
Thank you so much!
I just followed your instructions and it worked. Your so helpful. Thanks again
Nov 9 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Derek Wolf | last post by:
I am having some difficulties implementing an UnHandledException handler for the AppDomain.UnhandledException event in a windows service for unhandled exceptions that occur on a System.Timers.Timer...
5
by: RAJ | last post by:
hi plz tell me how to know "how window is going to close"... i have to right code for X button of forms... plz telll me thanks bye
5
by: Dave | last post by:
How do I check in a Windows Forms app if any controls have changed? I have a form that collects data, and I want to prompt the user if they try to exit the app, or load a new file, without saving...
7
by: Doug Stiers | last post by:
I have a VB app that I'm installing as a Windows Service. I want a subroutine in the app to run every 30 minutes during business hours. How do I do this in VB? I set the startup type as automatic...
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
5
by: Martin | last post by:
I have a menu, a rather big one. It contains about 150 items. Normally this would mean that I'd have about 150 event handlers: Private Sub Menu1_Click(ByVal sender As System.Object, ByVal e As...
5
by: Sudharsan | last post by:
Hi I have developed a Windows Control Library in VC.NET The component will play back video files and this component is used in C# applications to display multiple videos simultaneously. The...
8
by: hoofbeats95 | last post by:
I don't think this should be this complicated, but I can't figure it out. I've worked with C# for several years now, but in a web environment, not with windows form. I have a form with a query...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
7
by: tshad | last post by:
What exactly is FileWatcher doing? When you drop 100 files in a folder it is watching, it normally will fire of the event 100 times. In my case, I do all my processing on the first event so I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.