0

我有一个 ToolStripMenu 和一个表,其中有一个名为qcategorychar 类型的列。我想创建一个查询,它只选择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();

之后,在构造函数中,我对from formcategorie的值进行赋值。categorieElev

String categorie = elev.getCategorie();

并进行查询:

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

我的问题是它没有正确读取菜单项值,我找不到问题。总而言之,我必须从 form 传递Elev字符串值categorie并在 form 中使用它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 CCategoria B来自qcategory列的值,但在另一行。) 天哪!无论我选择什么子项目,它都会选择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

4

1 回答 1

2

Selected属性是一个布尔值,意思categorie总是等于"True"or "False"。检查此链接

一个可能的解决方案:

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

我决定创建自己的项目来尝试展示我认为您正在努力实现的完整解决方案。就是 Windows 的样子

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

模拟器.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;
        }
    }
}
于 2015-03-17T16:22:11.613 回答