473,378 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

What's a good way to "run all checked items in a checked listbox"?

Hi,

I've got a checked list box, and a "go' button on the form. Each item
in the checked list box is associated with a program (say notepad,
calc, etc.). When the user clicks "go", every item that is checked is
to be run.

I'm looking for an elegant and extensible way to do this. I'm currently
using an if block, which just seems silly to me. Structurally, it's the
equivalent of the below. Any ideas on alternatives would be much
appreciated. What I have looks asinine.

In particular, it would be nice to be able to "run all checked",
without having to specify at compile time just what the possibilities
are (ie, the user might be able to add items to the list).

thanks,

cdj

==========================
private void btn_Gather_Click(object sender, System.EventArgs e)
{
if (chkListBox_AvailProgs.CheckedItems.Count == 0)
{
MessageBox.Show("There are no programs checked.", "Program selector
Error");
}
else if (dateTimePickerStart.Value.DayOfWeek.ToString() == "Sunday"
|| dateTimePickerStart.Value.DayOfWeek.ToString() == "Saturday")
{
MessageBox.Show("Date must be Monday-Friday.", "Program selector
Error");
}
else
{
if (chkListBox_AvailProgs.CheckedItems.Contains("Note pad"))
{
RunNotepad();
}

if (chkListBox_AvailProgs.CheckedItems.Contains("Calc "))
{
RunCalc();
}

if (chkListBox_AvailProgs.CheckedItems.Contains("Pain t"))
{
RunPaint();
}
}
}

Nov 8 '06 #1
1 1990
Hi,

Define your own list item:

internal class ProgramListItem
{
public string Title { get { return title; } }

public string Path { get { return path; } }

private readonly string title, path;

public ProgramListItem(string title, string path)
{
this.title = title;
this.path = path;
}

/// <summary>
/// Provides the display text to the CheckedListBox control
///</summary>
public override string ToString()
{
return title;
}
}
When you populate the list, do so by constructing instances of the
ProgramListItem class:

chkListBox_AvailProgs.Items.Add(new ProgramListItem("Calculator",
@"calc.exe"));
chkListBox_AvailProgs.Items.Add(new ProgramListItem("Notepad",
@"notepad.exe"));
You can then run the checked programs as follows:

private void btn_Gather_Click(object sender, EventArgs e)
{
// iterate over each checked item
foreach (ProgramListItem item in checkedListBox1.CheckedItems)
{
RunProgram(item.Path);
}
}

--
Dave Sexton

"sherifffruitfly" <sh*************@gmail.comwrote in message
news:11*********************@f16g2000cwb.googlegro ups.com...
Hi,

I've got a checked list box, and a "go' button on the form. Each item
in the checked list box is associated with a program (say notepad,
calc, etc.). When the user clicks "go", every item that is checked is
to be run.

I'm looking for an elegant and extensible way to do this. I'm currently
using an if block, which just seems silly to me. Structurally, it's the
equivalent of the below. Any ideas on alternatives would be much
appreciated. What I have looks asinine.

In particular, it would be nice to be able to "run all checked",
without having to specify at compile time just what the possibilities
are (ie, the user might be able to add items to the list).

thanks,

cdj

==========================
private void btn_Gather_Click(object sender, System.EventArgs e)
{
if (chkListBox_AvailProgs.CheckedItems.Count == 0)
{
MessageBox.Show("There are no programs checked.", "Program selector
Error");
}
else if (dateTimePickerStart.Value.DayOfWeek.ToString() == "Sunday"
|| dateTimePickerStart.Value.DayOfWeek.ToString() == "Saturday")
{
MessageBox.Show("Date must be Monday-Friday.", "Program selector
Error");
}
else
{
if (chkListBox_AvailProgs.CheckedItems.Contains("Note pad"))
{
RunNotepad();
}

if (chkListBox_AvailProgs.CheckedItems.Contains("Calc "))
{
RunCalc();
}

if (chkListBox_AvailProgs.CheckedItems.Contains("Pain t"))
{
RunPaint();
}
}
}

Nov 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: VK | last post by:
Hello: I have a control listed as: <asp:ListBox id="ddlList" runat="server" SelectionMode="Multiple"></asp:ListBox> - this list is dynamic. We would like users to select 4 choices. Is there...
3
by: Giovanni Bassi | last post by:
Hello All, I have a class implementing IList and a global object of this class type. I have two different form objects. One has a ListBox and the other has ComboBox. Both of their datasources...
0
by: VB Newbie | last post by:
I am creating a user control containing a combobox using VB.NET(2003) I want to add 2 public properties "DataSource" and "Items" like the "System.Windows.Forms.ComboBox" here is my code, but it...
5
by: VB Newbie | last post by:
I am creating a user control containing a combobox using VB.NET(2003) I want to add 2 public properties "DataSource" and "Items" like the "System.Windows.Forms.ComboBox" here is my code, but it...
3
by: taurin423 | last post by:
I am working on integrating RSS reading into my ASP.NET web application. So far I have it working just fine with showing the full feed, but I'm not sure how to let it handle which items have been...
3
by: Zytan | last post by:
I can handle DoubleClick on a ListBox to respond, not when the selection changes, but when you double click one to invoke an action. This is the same as using the keys to move the selection, then...
2
by: kimiraikkonen | last post by:
Hi experts, I need to display some custom text ouptut in a Listbox and the first 4 lines of listbox (index items) will be removed. the problem is not to know how to display custom text "line by...
6
by: painkiller | last post by:
language: vb.net environment: windows forms .net : v1.1 i am having a checkedlistbox control that display document category such as text, image, video, audio etc. these values are coming from...
1
mikek12004
by: mikek12004 | last post by:
I have recently put https on a page with certificate and everything but when I type https://.. in IE I get the message "this page contains both secure and nonsecure items" why? What is the nonsecure...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.