Solved

Menu variable passing...

Posted on 2004-09-26
8
305 Views
Last Modified: 2012-06-22
Problem History:

I have never used c# before but I wanted to learn a programming language so I dived in and started making a personal toolkit for a java based game.

My Problem:

On the main menu I have made a "Servers" section with several links to "World 1", "World 2", "World 3" and so on, they all reference "menuWorld_Click", I wan't to pass a value to this function from each item.

For example if you click "World 1" it will send the value "1" to "menuWorld_Click" and for "World 10" it will send "10", and so on.

Is there a way to pass this value?

Supporting Problem Code:

// Example call from menu
this.menuWorld2.Click += new System.EventHandler(this.menuWorld_Click);

// Example of "menuWorld_Click"
private void menuWorld_Click(object sender, System.EventArgs e)
{
      // Code Here
}
0
Comment
Question by:Sk8rRIMuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Assisted Solution

by:CrazyIvan007
CrazyIvan007 earned 200 total points
ID: 12154194
If you generated the code with the Designer, you can add

menuitem.Tag = "<Number";

to each MenuItem

Then in the Click Event

System.Windows.Forms.MenuItem _mi = (System.Windows.Forms.MenuItem)sender;
string strValue = _mi.Tag.ToString();

The selected Item is allway in the object "sender".
0
 

Author Comment

by:Sk8rRIMuk
ID: 12154242
//
// menuWorld2
//
this.menuWorld2.Index = 0;
this.menuWorld2.Text = "World 2";
this.menuWorld2.Click += new System.EventHandler(this.menuWorld_Click);

is what the designer generated so do I add:

this.menuWorld2.Tag = "2";

if I do I get an error saying it doesn't contain a defenition for .Tag?
0
 
LVL 1

Expert Comment

by:CrazyIvan007
ID: 12154392
Sorry, but i have a own MenuItem Class which supports that!

The MS MenuItem doesn't have a Tag Option, so remove that and get the number out of the Text in the Click Event with

int intNumber = -1;

Match m1 = Regex.Match(_mi.Text, @"(\d+)$");
if (m1.Success)
{
   intNumber = System.Convert.ToInt32(m1.Groups[1].Values.ToString());
}

Then you have the number in intNumber and you can check intNumber >= 0 ... if not the regex doesn't match
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:Sk8rRIMuk
ID: 12154434
The type or namespace name 'Match' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'm1' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'm1' could not be found (are you missing a using directive or an assembly reference?)

Errors... but I did this just:

System.Windows.Forms.MenuItem _mi = (System.Windows.Forms.MenuItem)sender;
string strValue = _mi.Text.ToString();

It returns the menu title for example "World 1", "World 10", etc. It's just what you suggested with "Text" not "Tag".

I can do regex in php but not in C# as I am only just starting out. So in C# can I extract the last 1 or 2 digits as the value from "strValue" via regex?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12154476
private void menuWorld_Click(object sender, System.EventArgs e)
{
      if ( sender == menuWorld1 )
      {
            // menuWorld1 is clicked
      }  
      else if ( sender == menuWorld2 )
      {
            // menuWorld2 is clicked
      }  
}
0
 

Author Comment

by:Sk8rRIMuk
ID: 12154533
private void menuWorld_Click(object sender, System.EventArgs e)
{
      if ( sender == menuWorld1 )
      {
            // menuWorld1 is clicked
      }  
      else if ( sender == menuWorld2 )
      {
            // menuWorld2 is clicked
      }  
}

Thanks, but...

I have 55 worlds that is not really too effeicent, I just need to know how to use C# regex I guess...
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 300 total points
ID: 12154571
           String s = ((System.Windows.Forms.MenuItem)sender).Text;

            string[] strings = s.Split(new char[]{' '});

            int n = Int32.Parse(strings[strings.GetLength(0) - 1]);
0
 

Author Comment

by:Sk8rRIMuk
ID: 12155001
Thanks!!!
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!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

695 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