C# 菜单条文本值

C# Menu strip text value

我有一个 ToolStripMenu 和一个 table,其中我有一个名为 qcategory 字符类型的列。我想创建一个查询,只选择 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 查询在哪里。之后,我创建了一个 returns 值为 categorie 的函数。然后,在 Simulare 表单中,我实例化了 Elev 表单(菜单在哪里)。

Elev elev = new Elev();

之后,在构造函数中,我从 Elev 表单中赋值给 categorie categorie 的值。

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; 

在构造函数中:

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 的值类似于 qcategory 列中的 Categoria B,但在另一行。) 天哪! 无论我选择什么子项,它都会选择 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";
        }

无论我选择什么子项,它都会从菜单中选择包含最后一个子项的字符串值的行。 (Categoria C) 如果我点击 Categoria B,我会收到来自 Categoria C 的问题。

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