我有一个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
的问题。
答案 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;
}
}
}