配置一系列工具条菜单以查看是否检查了它们 [英] configuring an array of toolstripmenuitems to see if they are checked or not

查看:54
本文介绍了配置一系列工具条菜单以查看是否检查了它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是C#编程的新手,我基本上是在自学一些基本的语言,并且正在尝试将我的VB程序之一转换为C#以帮助实现此目的.但是,我试图减少我在VB代码中拥有的某些if命令来简化代码,但是我很难将其切换成Do循环或While循环.基本上,它的作用是检查持续时间"菜单中的项目是否已被选中,如果已选中,则启动计时器,更改按钮的文本并更改颜色.在VB中,我有8个If和ElseIf命令来检查每个选择,但我相信循环运行会更好.我已经为btnStart_Click包含了我的VB代码,该代码将在其中运行以供参考.我想知道您的想法怎么办?我认为数组可以将菜单项加载到该数组中,然后检查它们是否已被选中并以这种方式运行.

Hey Guys,



I am very new to C# programming and I am basically teaching myself the language I have some of the basics down and I am trying to convert one of my VB programs to C# to help do this. However I am trying to reduce some of my if commands that I have in the VB code to streamline the code but I am having a hard time switching it over to do say a Do Loop or While loop. Basically what it does is check to see if my items in my Menu of Duration have been selected and if so then start the timer, change the text of the button and change the color. Within VB i have 8 If and ElseIf commands to check each selection but I believe that running it in a loop will be better. I have included my VB code for the btnStart_Click that this will run in for reference. I would like to get your ideas of what to do? I am thinking that an array would work to load the menu items into that then check them to see if they have been checked and run it that way.

''start timer for beep and check for Frequency and Duration
  Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    
    ''Check to see if both the Frequency and Duration are selected from the Context Menu, 
    ''and if nothing select prompt for selection.
    If cms1MinFreq.Checked = True Then
      tmr.Start()
      btnStart.BackColor = Color.Red
      btnStart.Text = "Started"
    ElseIf cms2MinFreq.Checked = True Then
      tmr.Start()
      btnStart.BackColor = Color.Red
      btnStart.Text = "Started"
    ElseIf cms5MinFreq.Checked = True Then
      tmr.Start()
      btnStart.BackColor = Color.Red
      btnStart.Text = "Started"
    ElseIf cms10MinFreq.Checked = True Then
      tmr.Start()
      btnStart.BackColor = Color.Red
      btnStart.Text = "Started"
    Else
      MsgBox("Please select Frequency and Duration by right clicking the window.")
    End If

    If cms10MinDur.Checked = True Then
      tmrMain.Start()
      btnStart.BackColor = Color.Red
      btnStart.Text = "Started"
    ElseIf cms20MinDur.Checked = True Then
      tmrMain.Start()
      btnStart.BackColor = Color.Red
      btnStart.Text = "Started"
    ElseIf cms30MinDur.Checked = True Then
      tmrMain.Start()
      btnStart.BackColor = Color.Red
      btnStart.Text = "Started"
    ElseIf cms1HourDur.Checked = True Then
      tmrMain.Start()
      btnStart.BackColor = Color.Red
      btnStart.Text = "Started"
    Else
      MsgBox("Please select Frequency and Duration by right clicking the window.")
    End If

  End Sub

推荐答案

DragonNightz写道:
DragonNightz wrote:

我认为数组会将菜单项加载到该菜单项中,然后检查它们是否已被选中并以这种方式运行.

I am thinking that an array would work to load the menu items into that then check them to see if they have been checked and run it that way.


好主意.

这是为您提供循环的示例:


Good idea.

Here''s an example with loops for you:

// class variables
private List<ToolStripMenuItem> allFreqItems;
private List<ToolStripMenuItem> allDurItems;

// in the constructor
allFreqItems = new List<ToolStripMenuItem>();
allFreqItems.Add(cms1MinFreq);	
allFreqItems.Add(cms2MinFreq);	
allFreqItems.Add(cms5MinFreq);	
allFreqItems.Add(cms10MinFreq);

allDurItems = new List<ToolStripMenuItem>();	
allDurItems.Add(cms10MinDur);
allDurItems.Add(cms20MinDur);	
allDurItems.Add(cms30MinDur);	
allDurItems.Add(cms1HourDur);	


// then later when it's time to validate inputs
bool freqOK = false;
bool durOK = false;

foreach(ToolStripMenuItem item in allFreqItems)
{
	if (item.Checked)
	{
		freqOK = true;
		break;
	}
}

foreach(ToolStripMenuItem item in allDurItems)
{
	if (item.Checked)
	{
		durOK = true;
		break;
	}
}

if (freqOK && durOK)
{
	tmrMain.Start();
	btnStart.BackColor = Color.Red;
	btnStart.Text = "Started";
}
else
{
	MsgBox("Please select Frequency and Duration by right clicking the window.");
}


使用.Net 3.5和Any扩展方法,您甚至不需要循环来遍历集合:


with .Net 3.5 and the Any extension method you don''t even need the loops to iterate over the collections:

bool freqOk = allFreqItems.Any(item => item.Checked);
bool durOK = allDurItems.Any(item => item.Checked);



警告:在浏览器中生成的示例可能包含语法错误:)



Warning: examples produced in a browser, may contain syntax errors :)


您可以在此处转换代码
you can convert your code here http://www.developerfusion.com/tools/convert/vb-to-csharp/[^]


这篇关于配置一系列工具条菜单以查看是否检查了它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆