|
{"Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))"}
3. crParameterFieldDefinition = crParameterFieldDefinitions["recpt"];
Maybe there is no parameter named "recpt" ?
If it' stuck, DO NOT pull harder!
|
|
|
|
|
Hello!
I'm working on an application wich will alow the user to add and remove controls. I want to apply different rules to textbox and this rules will be different for every textbox. For example i want to check wich control has been clicked by using a click event. Because i don't know how many textboxes will the user create i've decided to go along with the code below:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control ctrl in Controls)
{
ctrl.Click += new EventHandler(ctrl_Click);
}
}
void ctrl_Click(object sender, EventArgs e)
{
}
Is there anyway that i can get the name of the control that has been clicked? The sender.Tostring() only returns the type and the text of the control.
Thanks!
|
|
|
|
|
Yes, what you want to do is cast the sender to a Control such:
void ctrl_Click(object sender, EventArgs e)
{
Control contol = sender as Control;
if (control != null)
{
string name = control.Name;
}
}
Regards,
Rob Philpott.
|
|
|
|
|
Hi.
void ctrl_Click(object sender, EventArgs e)
{
if (sender.GetType() == typeof(TextBox))
{
string name = ((TextBox)sender).Name;
}
}
should work. Also if you are only interested in handling clicks for textboxes you could change the foreach loop to:
foreach (Control ctrl in Controls)
{
if (ctrl.GetType() == typeof(TextBox))
{
ctrl.Click += new EventHandler(ctrl_Click);
}
}
If it' stuck, DO NOT pull harder!
|
|
|
|
|
do NOT compare strings to check for a type, instead use the appropriate keywords:
do NOT compare types, nor strings representing types, instead use the appropriate keywords:
if (sender is TextBox) log("this is a TextBox");
TextBox tb=sender as TextBox;
if (tb!=null) log("this is a TextBox");
it is both faster and more correct as it also matches derived types.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
modified on Saturday, November 28, 2009 7:37 AM
|
|
|
|
|
According to MSDN help -GetType and typeof returns System.Type, i totally agree with "if (sender is TextBox)".
A humble question, do you mean that GetType()==typeof compiles as a string comparizon?
else i dont understand the "do NOT compare strings to check for a type, instead use the appropriate keywords"
If it' stuck, DO NOT pull harder!
|
|
|
|
|
Sorry, I should have said "do NOT compare types, nor strings representing types, to check for a type, ..."
is/as are more efficient than GetType constructs
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
ok! Thanks Luc, for a split second i thought you were wrong, i should have known better
If it' stuck, DO NOT pull harder!
|
|
|
|
|
Ah, now that's signature material!
|
|
|
|
|
|
Some ideas that may (I hope) bear on your solution :
Is it the case that any TextBoxes are inside containers within the Form (nested) : in that case you are going to need to recurse to find them and attach an Event Handler.
Unfortunately to get the Form.ControlCollection into a "flattened" IEnumerable where you can do cool filtering with Linq, and just, for example, pull out all the TextBoxes into a nice List<>, takes some voodoo which I am just now exploring myself, so am reluctant to comment on that so far.
If TextBoxes are the only controls you want to put a special Event handler on, it seems like overkill to add a subscriber to the 'Click event of every control on your Form or whatever (unless, of course, they are all TextBoxes).
foreach (Control candidate in Controls)
{
if(candidate is TextBox)
}
The TextBox does expose an 'Enter event, and when that event is fired, you can be sure that the 'sender parameter inside that event is also the same as your MainForm.ActiveControl. That might be something you could exploit.
If you are maintaining an arbitrary collection of TextBox controls which the end-user has the power to create and/or remove, you might consider keeping a List<TextBox> up to date, and, possibly a TextBox currentTextBox variable: these may come in handy.
Assuming by "apply different rules to TextBoxes" you mean execute some code for one class of TextBoxes, and execute different code another class of TextBox you could consider a Dictionary of <TextBox, Action<Control>> as found in the very interesting answer by Nathan W. here : [^] which uses recursion in a very clever way.
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|
Hi guys im creating a folder then adding a user with permissions to that folder but how
do I then set this security permission to apply to this folder, sub folders and files within this folder?
currently it just applys to this folder only.(so when the app creates a file within this folder the file does not inherit the permission's from the folder)
DirectorySecurity dirSec = Directory.GetAccessControl(_dbPath);
dirSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.Write, AccessControlType.Allow));
dirSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.ReadAndExecute, AccessControlType.Allow));
dirSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.CreateFiles, AccessControlType.Allow));
dirSec.AddAccessRule(new FileSystemAccessRule(IdentityReference, FileSystemRights.WriteData,InheritanceLevel,PropagationFlags., AccessControlType.Allow));
Directory.SetAccessControl(_dbPath, dirSec);
|
|
|
|
|
try this:
dirSec.AddAccessRule(new FileSystemAccessRule(IdentityReference, FileSystemRights.WriteData,
InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
Edit: Splitted message, because it isn't rendered correctly
modified on Saturday, November 28, 2009 5:23 AM
|
|
|
|
|
Hi
I used reflection many times before on public methods but never realized that private methods can be invoked too. See the link : link
I am still thinking why is that allowed in the first place?
Isn't that going to break the rule of "private" being "private"?
puzzled
AJ
Follow your goals, Means will follow you ---Gandhi---
|
|
|
|
|
It's reflection, the very concept of it breaks all rules - much like arbitrary pointer arithmetic and type punning
That doesn't matter though, these things are useful in practice, The Rules are mostly of theoretical value (for example to be able to prove things about programs) and are only guidelines in the Real World.
|
|
|
|
|
Hi -
After some nice help from this group, my C# app is working well in debug mode.
However, when I publish it and then run setup.exe, the installation process crashes and makesan error report to sent to Microsoft. The error message (which I understand only parts of) says: "P1 <my app name> P4 mscorlib.dll P9 system.io.file not found exception". I don't know whether this means mscorlib.dll was not found or whether mscorlib.dll could not find a file. In any case, my machine has mscorlib.dll in the .net 2.0 folder. I built the application prerequisites to include .net 3.5 SP1 and also tried additinally listing 2.0. No difference: both crash.
I previously published a small app with no problems, so there must be something that this app needs that is not being found.
Can someone put me on the right path? I've Googled "visual studio 2008 express setup crash" and found nothing helplful.
Thanks -
Chuck
|
|
|
|
|
|
Hi Saksida:
.net 2.0 SP2 is already installed so the problem is something else.
Chuck
|
|
|
|
|
string str2= "";
foreach ( str2 in this.String_Array)
{
if (str2 != "")
{
flag2 = true;
break;
}
}
Error is
Cannot assign to 'str' because it is a 'foreach iteration variable'
Error
Type and identifier are both required in a foreach statement
If you can think then I Can.
|
|
|
|
|
A foreach iteration variable should be declared IN the foreach statement itself. You cannot reuse a previously declared variable, as in the case of 'for' loop.
This should work:
foreach (String str2 in this.String_Array)
Delete the first line that declares str2.
|
|
|
|
|
But the Error Getting Similer. I already use it.
If you can think then I Can.
|
|
|
|
|
Please be more descriptive. Post your code and the error message.
|
|
|
|
|
As Shamel said it, you need to have it like this:
foreach ( String str2 in this.String_Array)
{
if (str2 != "")
{
flag2 = true;
break;
}
}
eg_Anubhava wrote: Cannot assign to 'str' because it is a 'foreach iteration variable'
I frequently got this error, when I tried to change str2 and this.String_Array inside ForEach loop. My workaround was to create temp List, then I changed String_Array outside foreach loop
|
|
|
|
|
thank you sir my meaning is same
If you can think then I Can.
|
|
|
|
|
I Did create empty windows Form, and I Put your sample, and it worked without any error.
Have you posted your actual code? If Not, please post actual foreach loop.
Where and How do you use and initialize String_Array and Flag?
What Exception do you get and at witch line?
|
|
|
|