Solved

Menu variable passing...

Posted on 2004-09-26
8
268 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
  • 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now