|
This will work for just the controls on the form:
foreach(Control subControl in myForm.Controls)
{
subControl.Text = "Howdy!";
}
However, if your controls have sub-controls (like a TabControl has it's own Control collection full of TabPages which then have their own controls), you'd have to use recursion.
SetSubControlText(Control ctrl, string text)
{
if(ctrl.Controls.Count > 0)
{
foreach(Control subControl in ctrl.Controls)
{
subControl.Text = text;
SetSubControlText(subControl);
}
}
}
If you call it like this: SetSubControlText(this, "Howdy!"); then you'll be passing the form to it and the method will loop through every control on the form, and their sub-controls, and set their text to Howdy!.
That should work...but I'm at school right now, and on their crappy computers without VS.NET, so this is untested. If it doesn't work, let me know and I'll look at it tonight.
Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...
-Anna-Jayne Metcalfe on Paintballing
|
|
|
|
|
It works! Thank you very much
|
|
|
|
|
With the Windows API, I used GetSystemMetrics(SM_CXSCREEN)to get the screen size. How do I get it in .NET?
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhi
|
|
|
|
|
That would be the Screen class in the System.Windows.Forms namespace in the .NET framework
Hey leppie! Your "proof" seems brilliant and absurd at the same time. - Vikram Punathambekar 28 Apr '03
|
|
|
|
|
Thanks, although that wasn't necessary.
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhi
|
|
|
|
|
Does anybody have a code snippet that shows how to find the namespace an object lives in?
Dim obj As Object<br />
Namespace = FindNamespace(Obj)
|
|
|
|
|
obj.GetType().Namespace or typeof(Object).Namespace
Hey leppie! Your "proof" seems brilliant and absurd at the same time. - Vikram Punathambekar 28 Apr '03
|
|
|
|
|
Thanks leppie
|
|
|
|
|
I have a Form that react to a delegate callback and create child controls according to the delegate event argument. But when I try to create the controls and set the parent to the Form at runtime, the subjected exception raised. Any solution on this?
e.g.
Class CA : System.Windows.Forms.Form
{
public CA()
{
CB b = new CB();
b.MyEvent += new OnMyEventHandler( this.OnMyEvent );
}
protected void OnMyEvent( int nCount )
{
for ( int i = 0; i < nCount; i ++ )
{
Button btn = new Button();
btn.Parent = this; <----- Exception
btn.Location = new Point( 100 + i * 100, 10 );
btn.Show();
}
}
}
public delegate void OnMyEventHandler( int nCount );
Class CB
{
public event OnMyEventHandler myEvent;
public CB
{
if ( myEvent != null )
myEvent( 5 );
}
}
Thanks!
|
|
|
|
|
if (control.InvokeRequired)
control.Invoke(...., .....);
else
Hey leppie! Your "proof" seems brilliant and absurd at the same time. - Vikram Punathambekar 28 Apr '03
|
|
|
|
|
Thanks for the hint. But I still have some further question.
The Invoke require a delegate as the arguement. What I want to ask is, if I dynamically create a number of buttons when a delegate callback triggered, can I set those buttons' parent to the original form?
|
|
|
|
|
The simplest solution is to marshal to call to the correct thread when the delegate is invoked:
class CA : System.Windows.Forms.Form
{
...
protected void OnMyEvent(int nCount)
{
if (this.InvokeRequired)
{
this.Invoke(new OnMyEventHandler(this.OnMyEvent));
}
else
{
for ( int i = 0; i < nCount; i ++ )
{
Button btn = new Button();
btn.Parent = this;
btn.Location = new Point( 100 + i * 100, 10 );
btn.Show();
}
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
Hi, I am new to C# and this message board, so I would appreciate any help I can get. I am writing and MDI application, where I have the parent screen split in two areas (with size of the area on the left about 20% of the MDI parent). The area on the left will be the navigation screen and on the right will display the window that results from user action taken on the left. I am trying to get the windows on the right to display the maximum size of its alloted area and I thought I can do this by adding it to a panel. Is it possible to add a form to a panel and if so can any provide some code on doing this. If not, is there some kind of container to restrict the child form size to its alloted area. Thanks.
|
|
|
|
|
Someone who wrote a book[^] about .NET interop has released a free tool[^] to monitor type marshaling inside the CLR. It takes advantage of .NET 1.1. It works on all machines, although Windows XP is required to show the balloon tooltips.
|
|
|
|
|
Clicking on the second link gives you a "Server error in / application."
Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...
-Anna-Jayne Metcalfe on Paintballing
|
|
|
|
|
The tool link works for me.
For others if you don't know about Adam Nathan, he is CLR Test Lead.
Don't and drive.
|
|
|
|
|
Kant wrote:
For others if you don't know about Adam Nathan, he is CLR Test Lead.
And a freaking god when it comes to Interop.
Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...
-Anna-Jayne Metcalfe on Paintballing
|
|
|
|
|
I bet he is Stephane's guru.
Don't and drive.
|
|
|
|
|
eh... who knows
|
|
|
|
|
Don't forget to read his blog[^] as well. Apparently, the new Application.EnableVisualStyles() method has a marshalling bug in it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
I am subscribed to it since day one. But quite honestly, I think I am gonna quit it altogether as I tend to think that as a "customer" I don't have to learn the intrinsics of an engine whose implementation is going to change anyway.
For any current problem, the work around is to write a dll and use a P/Invoke native call to bypass the CLR itself. It only takes an F-grade know-how to do so. I swallow a small raisin, leaving away all this crap.
|
|
|
|
|
I'm trying to educate myself on C# and .Net. I decided to try and use the techniques outlined by Marc Clifton ("Using XML to generate Menus") but to use it with a different UI tool kit along with extending the menus.xsd to support imagelists.
What I'm looking for is a method of taking an input name (of an imagelist or a method) passed in the xml definition or the menu and use that name to pass the actual object or method to the appropriate constructors etc.
I've found that I can find the appropriate MethodInfo information using reflection and can find the FieldInfo information for an imagelist but I can't seem to figure out how to use that information to accomplish my objective. I'm using the Crownwood Magic Library menus that have a MenuCommand constructor that will take an imagelist and index value for the menu image and will take an EventHandler for an OnClick event.
Is there a way to use the information retrieved form the reflection methods to set the event handler and to set the imagelist from the names passed in the xml.
Thanks for the help
Neil Lamka
neil@meetingworks.com
|
|
|
|
|
I beleave you are on the wright track you just need to use the
typeof( YourType ).GetMethod( "MethodName", BindingFlags.Public ).Invoke( objToCallInvokeOn, new object[](arg1, arg2, arg3} );
The BindingFlags is just that there flags so you can use | operator.
The args can be null new object[]{null} or null will work I beleave.
There are a few different ways and method overloads that is avaliable.
|
|
|
|
|
Thanks for the pointer.
If I use this method then I'm assuming that in my DLL that handles reading the XML definitions for menus and all that I'd have
1) Set an "OnClick" event handler internal to my DLL
and then
2) When processing the OnClick event figure out the real target menthod that should process the event and use the Invoke as you described.
Is there any way to set the event handler for the menu item so as to bypass having to handle the event and route it? I'd like to get out of the way and just wire the menu directly to the desired handler. I was hoping that once I found the MethodInfo for the target method I would be able to set the menu event handler directly. Or is that what your note allows me to do and I just did not understand.
Thanks
Neil
Neil Lamka
neil@meetingworks.com
|
|
|
|
|
Sorry it took so long to get back. I hate to say it but I dont fully
understand what it is you need to do. Just a little more info on what
you need to do and maybe I can help some more.
Bo Hunter
|
|
|
|