473,326 Members | 2,111 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.

Dynamically create controls in a service

Hello all,

I may just not be searching for the right thing, but I've been looking for a
way to dynamically create controls (and name them) in my code so that I can
create only the controls I need at run time. For example, I'm writing a
filewatcher service that will eventually write back to a database, but I may
need the service to watch multiple directories (thus, multiple
FileSystemWatcher components). The rub is this...how do I create these
dynamically. Here's what I've tried, and obviously had no luck.

code snippet------------------------------------------

string SQL = "SELECT something from Something else where X=1 and Y=2";

SqlCommand cmd = new SqlCommand(SQL, Conn1);
SqlDatareader dr = cmd.ExecuteReader();

if(dr.hasrows)
{
int x = 0;
while(dr.read())
{
//This is where I need to create a dynamic FileSystemWatcher
component.
FileSystemWatcher fw(x) = new FileSystemWatcher(); //obviously, this
doesn't work but hopefully you can see what I'm aiming for.
fw(x).Path = @"C:\Somepath"; //this will actually be pulled from the
datareader (dr).
fw(x).Filter = "*.*";
fw(x).IncludeSubdirectories = true;
fw(x).Changed += new System.IO.FileSystemEventHandler(OnChanged);
}
}

I know the above is not complete, but I'm hoping that someone gets the idea
of what I'm trying to do. There could be multiple paths that need to be
watched and I will not know that until runtime. I'll be pulling the info
from a database.

Thanks for your help!

Nov 16 '05 #1
2 2584
Sean,

Your FileSystemWatcher instances will just be objects, not controls, and
there's no need to name them. However, you do need to keep them in some
higher-level scope if you want them to be available after their population.
A collection object that is a field in your service class is a reasonable
candidate for this. e.g.:

private ArrayList watchers = new ArrayList();
//...
private void AddWatcher(string path, string filter, bool
includeSubdirectories)
{
FileSystemWatcher newWatcher = new FileSystemWatcher(path, filter);
newWatcher.IncludeSubdirectories = includeSubdirectories;
newWatcher.Changed += new FileSystemEventHandler(OnChanged);

this.watchers.Add(newWatcher);
}

With this setup, you can run through your datareader, calling AddWatcher for
each row with parameter values read from the datareader.

HTH,
Nicole
"Sean" <se**@thedeegans.com> wrote in message
news:41******************@newsread2.news.atl.earth link.net...
Hello all,

I may just not be searching for the right thing, but I've been looking for
a
way to dynamically create controls (and name them) in my code so that I
can
create only the controls I need at run time. For example, I'm writing a
filewatcher service that will eventually write back to a database, but I
may
need the service to watch multiple directories (thus, multiple
FileSystemWatcher components). The rub is this...how do I create these
dynamically. Here's what I've tried, and obviously had no luck.

code snippet------------------------------------------

string SQL = "SELECT something from Something else where X=1 and Y=2";

SqlCommand cmd = new SqlCommand(SQL, Conn1);
SqlDatareader dr = cmd.ExecuteReader();

if(dr.hasrows)
{
int x = 0;
while(dr.read())
{
//This is where I need to create a dynamic FileSystemWatcher
component.
FileSystemWatcher fw(x) = new FileSystemWatcher(); //obviously,
this
doesn't work but hopefully you can see what I'm aiming for.
fw(x).Path = @"C:\Somepath"; //this will actually be pulled from
the
datareader (dr).
fw(x).Filter = "*.*";
fw(x).IncludeSubdirectories = true;
fw(x).Changed += new System.IO.FileSystemEventHandler(OnChanged);
}
}

I know the above is not complete, but I'm hoping that someone gets the
idea
of what I'm trying to do. There could be multiple paths that need to be
watched and I will not know that until runtime. I'll be pulling the info
from a database.

Thanks for your help!

Nov 16 '05 #2
Nicole, I figured it was going to be something very simple. I had the old
"Forest through the trees" syndrome.

Thanks again!

Sean

"Nicole Calinoiu" <ni*****@somewhere.net> wrote in message
news:%2*****************@tk2msftngp13.phx.gbl...
Sean,

Your FileSystemWatcher instances will just be objects, not controls, and
there's no need to name them. However, you do need to keep them in some
higher-level scope if you want them to be available after their population. A collection object that is a field in your service class is a reasonable
candidate for this. e.g.:

private ArrayList watchers = new ArrayList();
//...
private void AddWatcher(string path, string filter, bool
includeSubdirectories)
{
FileSystemWatcher newWatcher = new FileSystemWatcher(path, filter);
newWatcher.IncludeSubdirectories = includeSubdirectories;
newWatcher.Changed += new FileSystemEventHandler(OnChanged);

this.watchers.Add(newWatcher);
}

With this setup, you can run through your datareader, calling AddWatcher for each row with parameter values read from the datareader.

HTH,
Nicole
"Sean" <se**@thedeegans.com> wrote in message
news:41******************@newsread2.news.atl.earth link.net...
Hello all,

I may just not be searching for the right thing, but I've been looking for a
way to dynamically create controls (and name them) in my code so that I
can
create only the controls I need at run time. For example, I'm writing a
filewatcher service that will eventually write back to a database, but I
may
need the service to watch multiple directories (thus, multiple
FileSystemWatcher components). The rub is this...how do I create these
dynamically. Here's what I've tried, and obviously had no luck.

code snippet------------------------------------------

string SQL = "SELECT something from Something else where X=1 and Y=2";

SqlCommand cmd = new SqlCommand(SQL, Conn1);
SqlDatareader dr = cmd.ExecuteReader();

if(dr.hasrows)
{
int x = 0;
while(dr.read())
{
//This is where I need to create a dynamic FileSystemWatcher
component.
FileSystemWatcher fw(x) = new FileSystemWatcher(); //obviously,
this
doesn't work but hopefully you can see what I'm aiming for.
fw(x).Path = @"C:\Somepath"; //this will actually be pulled from
the
datareader (dr).
fw(x).Filter = "*.*";
fw(x).IncludeSubdirectories = true;
fw(x).Changed += new System.IO.FileSystemEventHandler(OnChanged);
}
}

I know the above is not complete, but I'm hoping that someone gets the
idea
of what I'm trying to do. There could be multiple paths that need to be
watched and I will not know that until runtime. I'll be pulling the info from a database.

Thanks for your help!


Nov 16 '05 #3

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

Similar topics

2
by: Sean | last post by:
Hello all, I may just not be searching for the right thing, but I've been looking for a way to dynamically create controls (and name them) in my code so that I can create only the controls I...
3
by: Steven Caliendo | last post by:
Hi, What I would like to do is have a different number of controls on a web form based on a users configuration. So, some users will need 5 textboxes, and some will only need 1. I don't want...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
3
by: Grey | last post by:
I know i can use placeholder control for dynamic create controls. but my requirement is i need to create multiple controls. when the user click the button, one textbox will be created. If user...
0
by: tommy | last post by:
hello everybody, i haved read a few articles about, how to create controls in asp.net dynamically! i have tested it -- all works fine.... but....now it comes--> HOW create controls...
3
by: sck10 | last post by:
Hello, When a person subits my web page, I do a simple check (validation) and if there are any errors, I create a variable (strValidateText) and set that value to a label on the form. What I...
2
by: Julia | last post by:
Hi, I have a custom control which render drop down boxes according to XML configuration file I would like to know how do I get the selected items of the drop down boxes should I override...
1
by: lanmou | last post by:
hi, Iam working on an application where the user gives the fields they want in a particaular clients records.(this is a form where there are textboxes where they enter the field names and data type...
8
by: William Foster | last post by:
Good evening all, I am creating a program with the ability to select an option from a ComboBox which will then create a secondary list of options in a CheckedListBox. Previously when I have...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.