Adding to Array

G

Guest

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.ToString();
this.checkBoxes = checkBox;
}
}
 
M

Morten Wennevik

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.ToString();
this.checkBoxes = checkBox;
}
}

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

Morten Wennevik

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);
}
 
G

Guest

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.Checked
}
but I don't know if one or any of them is checked?
 
C

Cor Ligthert

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
 
G

Guest

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
?
 
M

Morten Wennevik

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?
}

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.Checked
}
but I don't know if one or any of them is checked?

Morten Wennevik said:
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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top