C#菜单条文本值

时间:2015-03-17 16:04:15

标签: c# toolstripmenu

我有一个ToolStripMenu和一个表,其中我有一个名为qcategory char类型的列。我想创建一个查询,只选择qcategory等于选中ToolStripMenuItem的行。所以我试着用这种方式:

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;
        }

我所做的是创建一个名为categorie的字符串。 simulareExamenToolStripMenuItem是菜单中的按钮。在这里,我为categorie分配了所选项目的字符串值。在categoriaBToolStripMenu1我实例化了表单Simulare,其中是查询。之后,我创建了一个返回值categorie的函数。然后,以Simulare形式,我实例化Elev形式(其中是菜单)。

Elev elev = new Elev();

之后,在构造函数中,我从categorie形式向categorie Elev分配值。

String categorie = elev.getCategorie();

并执行查询:

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

我的问题是它没有正确读取菜单项值,我找不到问题。总而言之,我必须从表单Elev传递categorie的字符串值,并在表单Simulare中使用它。任何人都可以帮助我吗?谢谢!

更新<!/强> 现在,这是废话。这就是我在Elev形式中所拥有的:

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();
        }

这就是我Simulare形式的内容:

String categorie = SimulatorManager.Categorie; 

并在contructor中:

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

但是没有显示存在CategoriaB的行,它显示值为Categoria C的行。在Console.WriteLine(categorie),它会显示Categoria C,而不是CategoriaB。 (Categoria C的值类似于Categoria B列中的qcategory,但位于另一行。) OMG!无论我选择哪个子项,它都会选择Categoria C ..为什么???

更新2 这就是我在Elev形式中所拥有的:

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();
        }

这就是我Simulare形式的内容:

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

无论我选择哪个subItem,它都会从菜单中选择包含last subItem的字符串值的行。 (Categoria C)如果我点击Categoria B,我会收到Categoria C的问题。

1 个答案:

答案 0 :(得分:2)

Selected属性是布尔值,表示categorie始终等于"True""False"。请检查此link

可能的解决方案:

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

这适用于所有菜单项,您只需将其添加为每个菜单项的点击处理程序。

或者:

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

此解决方案需要每个菜单项都有一个与此类似的点击事件处理程序。

修改

您需要确保可以检查下拉菜单项(这是通过属性设置的)。单击该按钮时,请通过下拉菜单找到所选菜单项。

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;
      }
   }
}

你应该考虑发布你的完整代码,因为你的问题不清楚你想要完成什么。

修改2

我决定创建自己的项目,尝试展示我认为你想要实现的目标的完整解决方案。 This就是Windows的样子

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;
        }
    }
}