PC Review


Reply
Thread Tools Rate Thread

Adding to Array

 
 
=?Utf-8?B?amV6MTIzNDU2?=
Guest
Posts: n/a
 
      18th Mar 2005
Hi I have the following method that I'm having trouble with. The method
should check that the files exist. If it does, then dynamically create a
checkbox for that file. Not sure if the second array databasesActual is
required which is where I'm geeting mixed up.

private void AddCheckBoxes(string dbFolder)
{
string[] databases = {"C:\Master.mdb", "C:\Toolbox.mdb", "C:\Import.mdb",
"C:\Library.mde", "C:\User.mdb", "C:\ImpData.mdb"};
string[] databasesActual;
for ( int j = 0; j < databases.Length; j++ )
{
if ( File.Exists(dbFolder + databases[j] ))
databasesActual = databases[j].ToString();
this.checkBoxes = new CheckBox[databases.Length];
int x = 68, y = 8;
for(int i = 0; i < this.checkBoxes.Length; i++)
{
CheckBox checkBox = new CheckBox();
checkBox.Parent = this;
checkBox.Location = new Point(x,y);
y += 32;
checkBox.Size = new Size(161, 21);
checkBox.Text = databases[i].ToString();
this.checkBoxes[i] = checkBox;
}
}
 
Reply With Quote
 
 
 
 
Morten Wennevik
Guest
Posts: n/a
 
      18th Mar 2005
Hi Jez,

You need some way of knowing which of the databases exist, but since you
don't know how many an ArrayList is better than an Array, so your code
would be something like this:

private void AddCheckBoxes(string dbFolder)
{
string[] databases = {
"C:\Master.mdb",
"C:\Toolbox.mdb",
"C:\Import.mdb",
"C:\Library.mde",
"C:\User.mdb",
"C:\ImpData.mdb"};

ArrayList databasesFound = new ArrayList();

foreach(string s in databases)
if(File.Exists(dbFolder + s)
databasesFound.Add(s);

this.checkBoxes = new CheckBox[databasesFound.Count];

for(int i = 0, x = 68, y = 8; i < this.checkBoxes.Length; i++, y += 32
{
CheckBox checkBox = new CheckBox();
checkBox.Parent = this;
checkBox.Location = new Point(x,y);
checkBox.Size = new Size(161, 21);
checkBox.Text = databasesFound[i].ToString();
this.checkBoxes[i] = checkBox;
}
}

Ignore the other changes if you want, they are just to show you a
different way of doing the same.

--
Happy Coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      18th Mar 2005
I should add that the CheckedListBox is better suited for unknown amounts
of CheckBoxes.

private void AddCheckBoxes(string dbFolder)
{
string[] databases = {
"C:\Master.mdb",
"C:\Toolbox.mdb",
"C:\Import.mdb",
"C:\Library.mde",
"C:\User.mdb",
"C:\ImpData.mdb"};

foreach(string s in databases)
if(File.Exists(dbFolder + s)
checkedListBox1.Items.Add(s);
}

--
Happy Coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
=?Utf-8?B?amV6MTIzNDU2?=
Guest
Posts: n/a
 
      18th Mar 2005
That work great thanks, however, how to I decide if one of the checkboxes is
checked. I need to know this in order to enable a button. My original code
(with fixed checkboxes on the form) was

if ((chkMaster.Checked) || (chkToolbox.Checked) || (chkImport.Checked) ||
(chkLibrary.Checked) || (chkUser.Checked) || (chkImpData.Checked))
{
btnCompact.Enabled = true;
}

I can loop through the dynamic checkboxes with
for ( int i = 0; i < checkBoxes.Length; i++ )
{
if (checkBoxes[i].Checked
}
but I don't know if one or any of them is checked?


"Morten Wennevik" wrote:

> I should add that the CheckedListBox is better suited for unknown amounts
> of CheckBoxes.
>
> private void AddCheckBoxes(string dbFolder)
> {
> string[] databases = {
> "C:\Master.mdb",
> "C:\Toolbox.mdb",
> "C:\Import.mdb",
> "C:\Library.mde",
> "C:\User.mdb",
> "C:\ImpData.mdb"};
>
> foreach(string s in databases)
> if(File.Exists(dbFolder + s)
> checkedListBox1.Items.Add(s);
> }
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]
>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      18th Mar 2005
jez,

I see you have evoluated my sample. However you changed it in a way that you
make it difficult to set to use the tag. When you use more my original
sample than it can be somthing as bellow. And than use the Tag with

\\\
checkbox.Tag = databases[j];
///
Than you can add in that routine as well
\\\
checkbox.CheckedChanged += new System.EventHandler
(this.CheckBoxCheckedChange);
///
And than the event itself
private void CheckBoxCheckedChange(object sender, System.EventArgs e)
{
if (((Control) sender).Tag.ToString == "Import.mdb")
{}
}

And about the arraylist in another answer.

Although the arraylist is my favorite array, would I never use that for
this.

I hope this helps,

Cor


 
Reply With Quote
 
=?Utf-8?B?amV6MTIzNDU2?=
Guest
Posts: n/a
 
      18th Mar 2005
Thanks Cor, it sort of works, but how do I get the CheckState from the sender
as

sender.ToString()
"System.Windows.Forms.CheckBox, CheckState: 0"

but there is no property for CheckState from sender
?


"Cor Ligthert" wrote:

> jez,
>
> I see you have evoluated my sample. However you changed it in a way that you
> make it difficult to set to use the tag. When you use more my original
> sample than it can be somthing as bellow. And than use the Tag with
>
> \\\
> checkbox.Tag = databases[j];
> ///
> Than you can add in that routine as well
> \\\
> checkbox.CheckedChanged += new System.EventHandler
> (this.CheckBoxCheckedChange);
> ///
> And than the event itself
> private void CheckBoxCheckedChange(object sender, System.EventArgs e)
> {
> if (((Control) sender).Tag.ToString == "Import.mdb")
> {}
> }
>
> And about the arraylist in another answer.
>
> Although the arraylist is my favorite array, would I never use that for
> this.
>
> I hope this helps,
>
> Cor
>
>
>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      18th Mar 2005
Jez,

((CheckBox) sender).CheckState

I hope this helps,

Cor


 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      18th Mar 2005
The CheckedListBox has a CheckedItems/CheckedIndices property you can use,
or the CheckedListBox.GetItemCheckState(index).

There should be good examples on how to use either in the documentations.

foreach(string s in checkedListBox1.CheckedItems)
{
// create a button?
}

On Fri, 18 Mar 2005 04:09:02 -0800, jez123456
<(E-Mail Removed)> wrote:

> That work great thanks, however, how to I decide if one of the
> checkboxes is
> checked. I need to know this in order to enable a button. My original
> code
> (with fixed checkboxes on the form) was
>
> if ((chkMaster.Checked) || (chkToolbox.Checked) || (chkImport.Checked) ||
> (chkLibrary.Checked) || (chkUser.Checked) || (chkImpData.Checked))
> {
> btnCompact.Enabled = true;
> }
>
> I can loop through the dynamic checkboxes with
> for ( int i = 0; i < checkBoxes.Length; i++ )
> {
> if (checkBoxes[i].Checked
> }
> but I don't know if one or any of them is checked?
>
> "Morten Wennevik" wrote:
>
>> I should add that the CheckedListBox is better suited for unknown
>> amounts
>> of CheckBoxes.
>>
>> private void AddCheckBoxes(string dbFolder)
>> {
>> string[] databases = {
>> "C:\Master.mdb",
>> "C:\Toolbox.mdb",
>> "C:\Import.mdb",
>> "C:\Library.mde",
>> "C:\User.mdb",
>> "C:\ImpData.mdb"};
>>
>> foreach(string s in databases)
>> if(File.Exists(dbFolder + s)
>> checkedListBox1.Items.Add(s);
>> }
>>
>> --
>> Happy Coding!
>> Morten Wennevik [C# MVP]
>>




--
Happy Coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking array to see if value exists before adding to array Wullie Microsoft Excel Programming 4 19th Jun 2009 09:00 AM
Adding a NULL to an array then adding array to a data table =?Utf-8?B?RmlkZGVsbTM3NDI=?= Microsoft ADO .NET 4 18th May 2006 07:18 PM
Adding to an array Otto Moehrbach Microsoft Excel Programming 5 24th Oct 2004 08:02 PM
Adding an Array to an Array List Jack Addington Microsoft C# .NET 2 13th Sep 2004 04:06 PM
Adding an Array =?Utf-8?B?SmFtZXMgU3RlcGhlbnM=?= Microsoft Excel Programming 2 14th Jan 2004 12:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:17 AM.