簡體   English   中英

C#菜單欄文本值

[英]C# Menu strip text value

我有一個ToolStripMenu和一個表,其中有一個名為qcategory char type的列。 我想創建一個查詢,該查詢僅選擇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();

之后,在構造函數中,我嘗試從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行,而是顯示value是Categoria C行。 Console.WriteLine(categorie)它顯示Categoria C ,不CategoriaB因為它應該。 Categoria C是類似於qcategory列中Categoria B的值,但在另一行。) 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";
        }

無論我選擇什么子項,它都會從菜單中選擇包含最后一個子項的字符串值的行。 Categoria C )如果我單擊Categoria B ,則會收到Categoria C問題。

Selected屬性是一個布爾值,表示categorie始終等於"True""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";
}

此解決方案將需要每個菜單項都具有與此事件類似的click事件處理程序。

編輯

您需要確保可以選中下拉菜單項(通過屬性設置)。 單擊該按鈕后,通過下拉菜單並找到選定的菜單項。

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的樣子

高度

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

類似的

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