[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Menu variable passing...

Posted on 2004-09-26
8
Medium Priority
?
311 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1200 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

Technology Partners: 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 Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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