Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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:
C#
// 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.");
}

with .Net 3.5 and the Any extension method you don't even need the loops to iterate over the collections:
C#
bool freqOk = allFreqItems.Any(item => item.Checked);
bool durOK = allDurItems.Any(item => item.Checked);


Warning: examples produced in a browser, may contain syntax errors :)
 
Share this answer
 
 
Share this answer
 
Comments
Sandeep Mewara 24-Jun-10 14:39pm    
Reason for my vote of 1
Did you read the question fully? He knows and is converting the code from VB to C#. He needs some technical ideas while doing it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900