|
I meant the Reflection namespace is huge but still I need to learn that. It's just that it's 5 am and I have a little time to finish what I'm writing. that's all.
Anyway, thanks for the help..
I think I should use MemberInfo class and tried smth like this:
<br />
System.Reflection.MemberInfo[] myMembers = ....GetType().GetMember("pictureBox1");<br />
this.Controls.Remove(myMembers[0]);<br />
on it..
Radgar
"Imagination is more important than knowledge." - Albert Einstein
|
|
|
|
|
No, I don't think that's the way to go. Sorry, I don't know a lot more, but definately reflection is the only way you'll get variables out of names, unless you build a hash table before you start and look it up.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
hmm.. if only there was an equivalent like java's eval function... but no..
thanx for your time
Radgar
"Imagination is more important than knowledge." - Albert Einstein
|
|
|
|
|
Well friend I don't have fancy solution like reflection but this should do the trick... I bet 90% of people are using it... hey -> not all of us are MVPs ... NHF? I'm just kidding
<br />
foreach (Control c in this.Controls)<br />
{<br />
if (c.Name == "control1")<br />
{<br />
}<br />
}<br />
Now if ur's event isn't in form which control u need to modify, than you'll need to pass that form, or it's collection Controls. Drop a line if u need more hints...
|
|
|
|
|
I thought about this method but I need to do it without loops.. It will slow down the app...
thanks anyway.
Radgar
"Imagination is more important than knowledge." - Albert Einstein
|
|
|
|
|
you may
use reflection to get the type by name
and then call InvokeMember
|
|
|
|
|
Not sure if this'll compile, but I've done something similar to this:
<br />
string controlname = "control1";<br />
string propertyname = "BackColor";<br />
object newvalue = Color.Black;<br />
Control control;<br />
<br />
control = form1.GetType().InvokeMember( controlname, BindingFlags.Instance|BindingFlags.GetField, null, form1, new object[] {} );<br />
control.GetType().InvokeMember( propertyname, BindingFlags.Instance|BindingFlags.SetProperty, null, control, new object[] { newvalue } );<br />
--
Joel Lucsy
|
|
|
|
|
Hi All,
Just wondering if there is a way to change code access permissions (is that the right phrase?) from within the actual code at runtime?
Is this possible? I am writing some network tools and the network I am on has restrictions on .NET applications running. I was need to temporarily give the code access to certain areas of the system and then once the job is done, remove them.
Thanks for the help, sorry if this is a really noob question.
|
|
|
|
|
No, there is no way to elevate permissions at runtime (huge security risk!)
You can, however, sign your code with a certificate, then trust your certificate. You could also add the site you're running the app from (either network share or off the web) to your Internet Explorer trusted sites list, which, I believe, should get your code full permissions.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
"huge security risk!"........ I thought as much, just wanted to make sure though. Thanks for the answer... unfortunately certificates are not going to work for this setup because the tool will be run remotely and will just e-mail out specific user information to the administrators mailbox... hmm perhaps there is another way to go about this. I will keep hunting. Thanks for all the help.
Timbobsteve
|
|
|
|
|
Hi,
I've been trying to get some 3D graphics into my C# application - I'm fairly familiar with using DirectX in C# and have created a few 3D applications in windowed and fullscreen modes etc... however...
I'm now trying to call DX9 (Summer 2004) from within a Custom Control - so instead of creating a full-sized WinForm device (or a fullscreen device), I create a control that can just be dragged onto any other WinForm application and hey-presto - there's my DX device ready to draw. (I'm kindof taking a cue here from the use of OpenGL and MFC within the same window, whereby you create the gl device from the HDC of an MFC picture control or similar...)
Anyway.
I create a simple DX app that simply creates a device, overrides the OnPaint and draws some objects etc - which works fine as a standalone WinForm application. I then created a Custom Control - override the OnCreateControl function to call some device initialisation code, override the OnPaint method as I've always done.. but the problem comes with then inserting the newly compiled control dll into a new winform app. Although the control inserts correctly, it then (of course) starts drawing whilst inside the IDE - this would be fine but then the whole lot becomes unresponsive (whilst the DX device contiunes to redraw), meaning that I can't resize, reposition my control, build, compile or do anything apart from giving the three fingered salute and neglecting to tell MS about my problem....
I'm not thrashing the processor (VS.NET 2003 only clocks %2 whilst in this unresponsive state), and Task Manager still thinks that VS is responding (until I try and do anything of use with it...)
Has anyone else tried doing this? Is it a reasonable thing to expect to be able to do? Thoughts or comments - I can post some code up if needed: not particularly exciting but gives an idea of what I'm trying to do.
Thanks,
benw
|
|
|
|
|
Why not just put some flag in that would only render if that flag is set to true. Have the flag set to false by default, that way you can begin rendering only when you explicitly start rendering.
I believe there are some design-time attributes you can set on your control to determine whether you're in design mode or not...I'm no expert there, but you might want to look into System.Windows.Forms.Design[er]
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
How does one pull a pattern matched keys from within a large Hashtable? I have a large System.Collections.Hashtable with over 2000+ entries within its mass. This table is constantly changing and can not be pushed outside of memory. Oh I wish I could push this to a SQL DB. My first thought was to foreach the Hashtable and build an array filled with keys each time the Hashtable changed. Then search the array. But there has to be a better way.
Any suggestions? I'll even take a laugh.
- - - -
Using this small example, I need to pull out a list of those entries within Hashtable listed below, all keys that start with “tom”, giving me just entries [0] and [3].
<br />
Entries within my Hashtable (short list) <br />
[0] tommy object<br />
[1] frang object<br />
[2] fratiti object<br />
[3] tom object<br />
[4] eddtom object<br />
[5] frankie object<br />
[6] blue object<br />
- - - -
|
|
|
|
|
Hi Dewclaws,
A Hastable is not the right collection for your problem.
How does a hashtable works
A hashtable build a array with N bags: bag #0 .. bag #N-1.
If you add a (key;value)-pair in the hashtable, the hashtable first compute the hashcode of you key.
For this it uses key.GetHashCode().
Than its arrange the hashcode in one of the bags. In most cases if it uses the bag #(hashcode%N).
In the bag, its store the pair of key and value. In a bag can be more than one pair.
If you search a value of a key, the hashtable compute the hashcode again and looks only in this one bag.
Example with 5 Bags:
Key Hashcode Bag
tommy 23 -> 3
frang 43 -> 3
fratiti 12 -> 2
tom 45 -> 0
eddtom 23 -> 3
frankie 67 -> 2
blue 56 -> 1
bag #0: (tom;object)
bag #1: (blue;object)
bag #2: (fratiti;object)
bag #3: (tommy;object) (frang;object) (eddtom;object)
bag #4:
If you select N large enough (about 1x..1.5x the number of keys) you have a mean search cost of O(n).
Solution 1
A solution is to use one sorted list of pairs or two sorted list of keys respectively values.
ArrayList keys = new ArrayList();
ArrayList values = new ArrayList();
public void Insert(string key, object value)
{
int index = keys.BinarySearch(key);
if (index >= 0)
{
values[index] = value;
}
else
{
keys.Insert(~index, key);
values.Insert(~index, value);
}
}
If you search all (key;value)-pairs in which the key starts with a special string use binary search again.
public struct Range { public int From; public To };
public Range Find(string keyStart)
{
Range range;
range.From = keys.BinarySearch(keyStart);
range.To = keys.BinarySearch(keyStart + char.MaxValue);
if (range.From < 0) range.From = ~range.From;
if (range.To < 0) range.To = ~range.To - 1;
return range;
}
Solution 2
Build a search tree.
The root node contains for each letter (symbol) at the first position a subnode. Each subnode for each letter (symbol) at the second position a subnode. And so on.
This solution is a bit faster than Solution 1, but a lot more complex to implement.
Example:
[ROOT]
|- b
| '-----------------> (blue;object)
|- e
| '-----------------> (eddtom;object)
|- f
| '-r
| '-a
| |-n
| | |-g
| | | '---------> (frang;object)
| | '-k
| | '---------> (frankie;object)
| '-t
| '-----------> (fratiti;object)
'- t
'-o
'-m
|-------------> (tom;object)
'-m
'-----------> (tommy;object)
If you search for the keys which starts with tom, you walk the path t-o-m and select all subnodes.
Solution 3
If you look for all keys, which contains a string in any position, you can walk throw the full list.
Hope, one solution helps you.
Niedzi
|
|
|
|
|
Hello,
I'm validating the contents of a text box control. It must contain 9 digits, with optional hyphens between them, like this: NNN-NN-N-NNN. I'm using this regular expression:
[0-9]{3}(-)?[0-9]{2}(-)?[0-9](-)?[0-9]{3}$
As I understand, the $ at the end indicates that no more characters should be accepted. However, I can type a correct string followed by some other characters, and it will still be valid under that regular expression.
Any ideas?
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Yes, the $ will match the end of the text. How are you using the expression to validate the input?
[0-9] can be written as \d. There is no need to use parenthesis around the hyphens. So this is the same:
\d{3}-?\d{2}-?\d-?\d{3}$
---
b { font-weight: normal; }
|
|
|
|
|
This is the code I have:
Regex re = new Regex("\d{3}-?\d{2}-?\d-?\d{3}$");
if(!re.IsMatch(txtAccount.Text))
{
e.Cancel = true;
} (I just changed the regular expression to what you suggested.)
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
BTW, I just found out that if I enter NNN-NN-N-NNNN (one extra digit at the end) the validation fails. But if I just enter the 9 and more digits without hyphens, the validations passes. For eight digits without hyphens, the validation fails.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Yes, of course. The input is still valid if you add more characters to the beginning of the string. If you have more than nine digits, and no hyphens, the pattern will match the last nine digits.
If you also want to match the beginning of the text, use ^.
---
b { font-weight: normal; }
|
|
|
|
|
That was it, thanks!!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
This is a quick question. Is it possible to do ADO.NET access from a static class? I'm guessing that it's not, because if I were to create a static class (say DBController) and then attempt to use it to access my database (eg with the method DBController.getNewClients) then the method (getNewClients) would have to create non-static instances of objects (ie the connection object).
From Java I know that if you try to reference non-static methods from static methods you get an error. Does this hold true for C# as well? Or would I be able to create a static DBController class that I could then use on any of my forms to access my database?
If that's not possible, then is it possible to pass a reference to my DBController to every form that's going to need access to it? How would I do that?
These may be dumb questions, but I'm the only tech guy in my company and my degree is in mathematics. Any help would be EXTREMELY appreciated.
It's lonely, oh so lonely, being the only tech guy in my company.
|
|
|
|
|
Yes, it's possible. As the connection has to be local to every thread that uses it, you would have to declare the reference to it as ThreadStatic.
---
b { font-weight: normal; }
|
|
|
|
|
Thanks for the response. I don't know exatly what a thread is. Could you give me a code snippet (just by example) so I can see what you're talking about? Or just a more specific explanation?
One is the loneliest number...
|
|
|
|
|
A thread is like a line of execution of code. You can have several threads running simultaneously in your process, for example one to manage the UI and respond to events, while other is processing data.
Multithreading is not that easy, since you can have problems when two threads try to access the same object at the same time. Then you have thread synchronization, so that while one thread is accessing certain object, other threads wait on it. But then if you have thread A holding object 1 and waiting for object 2, and thread B is holding object 2 and waiting for object 1, you have a deadlock. Debugging a deadlock can be very difficult, but using multiple threads can improve the performance of your application.
Search google for multithreading programming, I'm sure there are several articles on it.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
If you use the class in asp.net, or from more than one program running at the same time, there will be more than one thread using the same class.
If you have a static reference in the class that is not thread static, it will be shared by every thread that uses the class. That means that when one program connects to a database, and then another program connects to a second database, both programs will be using the second database. Also when one program closes the connection, the other program will stop working.
---
b { font-weight: normal; }
|
|
|
|