Here's a snippet that may help a little if you really need to get at a control that has been added to a parent control:

import clr
import System
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form, Button

myBtnName = "button1"

form1 = Form()

def AddButton(form, btnName):
    btn = Button()
    btn.Name = btnName
    btn.Text = "Click Me"
    form.Controls.Add(btn)

AddButton(form1, myBtnName)

ctrls = form1.Controls.Find(myBtnName, False)
btn = ctrls[0]
if isinstance(btn, Button):
    print repr(btn)
    print btn.Name

System.Windows.Forms.Application.EnableVisualStyles()
System.Windows.Forms.Application.Run(form1)



On 9/2/2011 8:17 PM, Barton wrote:
Very interesting;
Apparently, Microsoft doesn't think that it is a useful thing to index directly into a System.Windows.Forms.Control.ControlCollection Class. The collection is still iterable in versions above 2.0, but the class no longer supports indexing. So:

>>> print form.Controls[0] # works in pythondotnet built on .NET 2.0 on prior.
<System.Windows.Forms.Button object at 0xaa8bc6c>
>>>

for ctrl in form.Controls:
    print ctrl    # works in .NET 3.0 and above.


Compare
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection_properties.aspx
with
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection_properties%28VS.80%29.aspx
and you'll see that all ICollection and IList properties have been removed.

Reasons why are a good topic for future discussion.

On 8/31/2011 10:30 PM, 刘振海 wrote:
Hi,

here  is the code:

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form,Application,Button
form=Form()
form.Controls.Add(Button())
print form.Controls[0] #this code didnt work with an error say "not indexable object"

but this worked in ironpython,it seems in ironpython it treats the "form.Controls" as an list like object I can only get the form.Controls value use the "form.Controls.get_Item(0)" in the python for .net So is there a chance to imprive Python for .net to have that characteristic?

thanks


_________________________________________________
Python.NET mailing list -PythonDotNet@python.org
http://mail.python.org/mailman/listinfo/pythondotnet


_________________________________________________
Python.NET mailing list - PythonDotNet@python.org
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - PythonDotNet@python.org
http://mail.python.org/mailman/listinfo/pythondotnet

Reply via email to