473,386 Members | 1,821 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,386 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 2590
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
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.