简体   繁体   English

C#菜单栏文本值

[英]C# Menu strip text value

I have a ToolStripMenu and a table where I have a column named qcategory char type. 我有一个ToolStripMenu和一个表,其中有一个名为qcategory char type的列。 I want to create a query that selects only rows where qcategory is equal with ToolStripMenuItem selected. 我想创建一个查询,该查询仅选择qcategory与选择的ToolStripMenuItem相等的行。 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 . 我在那里所做的就是创建一个名为categorie的字符串。 simulareExamenToolStripMenuItem is the button from the menu. simulareExamenToolStripMenuItem是菜单中的按钮。 Here I assigned to categorie the string value of item selected. 在这里,我分配给所选项目的字符串值进行categorie In categoriaBToolStripMenu1 I instantiated form Simulare where is the query. categoriaBToolStripMenu1我从Simulare实例化了查询的位置。 After that, I created a function that returns value of categorie . 之后,我创建了一个返回categorie值的函数。 Then, in Simulare form, I instantiated Elev form (where is menu). 然后,在Simulare形式,我实例化Elev形式(其中为菜单)。

Elev elev = new Elev();

After that, in constructor I assing to categorie the value of categorie from Elev form. 之后,在构造函数中,我尝试从Elev表单中对categorie的值进行categorie

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 . 所有的一切,我必须从形式传递Elev ,字符串值categorie和形式使用Simulare Can anybody help me with that? 有人可以帮我吗? Thanks! 谢谢!

UPDATE! 更新! Now, this is crap. 现在,这是废话。 This is what I have in Elev form: 这是我在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();
        }

This is what I have in Simulare form: 这是我在Simulare表格中得到的:

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 . 但是没有显示存在CategoriaB行,而是显示value是Categoria C行。 On Console.WriteLine(categorie) it shows Categoria C , not CategoriaB as it should. Console.WriteLine(categorie)它显示Categoria C ,不CategoriaB因为它应该。 ( Categoria C is a value like Categoria B from qcategory column, but at the other row.) OMG! Categoria C是类似于qcategory列中Categoria B的值,但在另一行。) OMG! Whatever subItem I would chose, it selects Categoria C ..why??? 无论我选择什么子项,它都会选择Categoria C ..为什么?

UPDATE 2 This is what I have in Elev form: 更新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();
        }

This is what I have in Simulare form: 这是我在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";
        }

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 . Categoria C )如果我单击Categoria B ,则会收到Categoria C问题。

The Selected property is a boolean, meaning categorie will always equal "True" or "False" . Selected属性是一个布尔值,表示categorie始终等于"True""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. 此解决方案将需要每个菜单项都具有与此事件类似的click事件处理程序。

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 编辑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 就是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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM