Solved

Menu variable passing...

Posted on 2004-09-26
8
297 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
transaction in asp.net, sql server 6 47
Need a simple RegEx to search for two words 27 85
asp web application 3 42
I need help adding validation to my MVC.Net view 6 23
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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