473,326 Members | 2,732 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,326 software developers and data experts.

Adding to Array

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;
}
}
Nov 16 '05 #1
7 6933
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]
Nov 16 '05 #2
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]
Nov 16 '05 #3
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]

Nov 16 '05 #4
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
Nov 16 '05 #5
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

Nov 16 '05 #6
Jez,

((CheckBox) sender).CheckState

I hope this helps,

Cor
Nov 16 '05 #7
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
<je*******@discussions.microsoft.com> 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]
Nov 16 '05 #8

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

Similar topics

7
by: David | last post by:
I have an array that contains numbers in string elements , i want to convert this to integers, i have made a for loop that uses the number of elements in the array and then adds 0, thereby...
4
by: Chris | last post by:
Dear all, I would like to figure out a way to add controls to a form using specs stored in a table. Any assistance in fleshing this idea out would be greatly appreciated. I'm using VB6. ...
5
by: Troy | last post by:
Hello, I have a dumb question. I have two array objects of type double with 12 elements in each object. I'd like to add the two objects but dont know how to. Any ideas? thanks
4
by: pauld | last post by:
$sql= sql query $i=0; while ($a2=mysql_fetch_array($a1)){array_push($temparray,$a2,$a2,$a2,$a2); { I want this array to be the value of an asssoc. array $results $results =$temparray doesnt...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
11
by: dennis.sprengers | last post by:
Consider the following multi-dimensional array: --------------------------- $arr = array( array(3, 5, 7, 9), array(2, 4, 6, 8), array(1, 3, 5, 7) ); function add_arrays($arr) { for ($row =...
10
by: Phat G5 (G3) | last post by:
I was toying around with adding a new method to the Array class. It works fine in FF but not in Safari. I have no idea in IE. I have not gotten that far yet. Array.prototype.find =...
2
by: AndyW2387 | last post by:
Hi I'm quite a newbie and have been struggling with sessions (!) What I'm trying to do is to add submitted values from a textfield (or ID number) to an array which is in a $_SESSION. ( It's for a...
6
by: santiago | last post by:
I guess one cannot do this: arraytot = arraytot + arraydet; So, what's the trick to adding arrays like this? Thanks.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.