Search code examples
c#toolstripmenu

C# Menu strip text value


I have a ToolStripMenu and a table where I have a column named qcategory char type. I want to create a query that selects only rows where qcategory is equal with ToolStripMenuItem selected. So I tried in this way:

String categorie;
        private void categoriaBToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Simulare sim = new Simulare();
            sim.Show();
        }

        public void simulareExamenToolStripMenuItem_Click(object sender, EventArgs e)
        {
           categorie = simulareExamenToolStripMenuItem.Selected.ToString();
        }

        public string getCategorie()
        {
            return categorie;
        }

What I did there was to create a string named categorie. simulareExamenToolStripMenuItem is the button from the menu. Here I assigned to categorie the string value of item selected. In categoriaBToolStripMenu1 I instantiated form Simulare where is the query. After that, I created a function that returns value of categorie. Then, in Simulare form, I instantiated Elev form (where is menu).

Elev elev = new Elev();

After that, in constructor I assing to categorie the value of categorie from Elev form.

String categorie = elev.getCategorie();

and do the query:

String dataA = "SELECT DISTINCT * FROM questions where `qcategory`  = '" + categorie + "' order by rand() limit 1";

My problem is that it doesn't read the menu item value right and I can't find the problem. All in all, I have to pass from form Elev, the string value of categorie and use it in form Simulare. Can anybody help me with that? Thanks!

UPDATE! Now, this is crap. This is what I have in Elev form:

public void simulareExamenToolStripMenuItem_Click(object sender, EventArgs e)
        {
           //categorie = simulareExamenToolStripMenuItem.Selected.ToString();
            //SimulatorManager.Categorie = simulareExamenToolStripMenuItem.DropDownItems.ToString();
            foreach (ToolStripMenuItem subItem in simulareExamenToolStripMenuItem.DropDownItems) //dropdown is the name of the **parent** of the dropdown. Without your full code I can't tell you what to put there
                {
                    if(subItem.Checked)
                        {
                            SimulatorManager.Categorie = subItem.Text;
                        }
                }
        }

        private void categoriaBToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Simulare sim = new Simulare();
            sim.Show();
        }

This is what I have in Simulare form:

String categorie = SimulatorManager.Categorie; 

and in contructor:

String dataA = "SELECT DISTINCT * FROM questions where `qcategory`='" + categorie + "' order by rand() limit 1";

But is not showing rows where exist CategoriaB, it shows rows where value is Categoria C. On Console.WriteLine(categorie) it shows Categoria C, not CategoriaB as it should. (Categoria C is a value like Categoria B from qcategory column, but at the other row.) OMG! Whatever subItem I would chose, it selects Categoria C..why???

UPDATE 2 This is what I have in Elev form:

public void simulareExamenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem subItem in simulareExamenToolStripMenuItem.DropDownItems) 
                {
                    if(subItem.Checked)
                        {
                            SimulatorManager.Categorie = subItem.Text;
                        }
                }
        }

        private void categoriaBToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Simulare sim = new Simulare();
            sim.Show();
        }

        private void categoriaCToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Simulare sim = new Simulare();
            sim.Show();
        }

This is what I have in Simulare form:

public Simulare() // maine constructor
        {
            String categorie = SimulatorManager.Categorie;
            Console.WriteLine(categorie);
            dataA = "SELECT DISTINCT * FROM questions where `qcategory`='" + categorie + "' order by rand() limit 1";
        }

Whatever subItem I would choose, it selects rows which contains string value of last subItem from menu. (Categoria C) If I click on Categoria B, I receive questions from Categoria C.


Solution

  • The Selected property is a boolean, meaning categorie will always equal "True" or "False". Check this link

    A possible solution:

    public void simulareExamenToolStripMenuItem_Click(object sender, EventArgs e)
    {
       ToolStripMenuItem senderMenuItem = sender as ToolStripMenuItem;
       if(senderMenuItem != null)
       {
          categorie = senderMenuItem.Text;
       }
    }
    

    This would work for all menu items, you just need to add it as the click handler for each one.

    Or:

    public void simulareExamenToolStripMenuItem_Click(object sender, EventArgs e)
    {
       categorie = "Something";
    }
    

    This solution would need each menu item to have a click event handler that is similar to this one.

    Edit

    You'll need to make sure the dropdown menu items can be checked(this is set via properties). When the button is clicked go through the dropdown menu and find the selected menu item.

    public void simulareExamenToolStripMenuItem_Click(object sender, EventArgs e)
    {
       foreach(ToolStripMenuItem subItem in dropdown.DropDownItems) //dropdown is the name of the **parent** of the dropdown. Without your full code I can't tell you what to put there
       {
          if(subItem.Checked)
          {
             categorie = subItem.Text;
          }
       }
    }
    

    You should consider posting your full code as your question is not clear what you are trying to accomplish.

    Edit 2

    I decided to create my own project to try and show a full solution of what I think you're trying to achieve. This is what the Windows look like

    Elev.cs

    public partial class Elev : Form
    {
        private string category = null;
    
        public Elev()
        {
            InitializeComponent();
        }
    
        //Attached to **each** sub item in the dropdown
        private void OnCategoryChecked(object sender, EventArgs e)
        {
            ToolStripMenuItem senderItem = sender as ToolStripMenuItem;
    
            //If the sender isn't a tool strip item OR it's alreadt checked do nothing
            if (senderItem != null && !senderItem.Checked)
            {
                //Un check the previously checked item
                foreach (ToolStripMenuItem item in this.toolStripDropDownButton1.DropDownItems)
                {
                    item.Checked = false;
                }
    
                //Set it to checked so the user knows which is selected
                senderItem.Checked = true;
    
                //Set the category
                this.category = senderItem.Text;
            }
        }
    
        private void ExamineButtonClicked(object sender, EventArgs e)
        {
            if (category == null)
            {
                MessageBox.Show("Select a category first!");
            }
            else
            {
                Simulare sim = new Simulare(this.category);
                sim.Show();
            }
        }
    }
    

    Simulare.cs

    public partial class Simulare : Form
    {
        public Simulare()
        {
            InitializeComponent();
            this.categoryTextBox.Text = "None";
        }
    
        public Simulare(string category)
        {
            InitializeComponent();
            this.categoryTextBox.Text = category;
        }
    
        public string Category
        {
            get
            {
                return this.categoryTextBox.Text;
            }
            set
            {
                this.categoryTextBox.Text = value;
            }
        }
    }