Solved

Menu variable passing...

Posted on 2004-09-26
8
278 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET HttpWebRequest 12 58
Visual Studio 2013 debugging 2 35
Connecting database in Xamarin? 6 53
Need help on C# Linq query on object 6 53
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

12 Experts available now in Live!

Get 1:1 Help Now