Is there a chance to imprive Python for .net to let "System.Windows.Forms.Form().Controls" be indexable?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Is there a chance to imprive Python for .net to let "System.Windows.Forms.Form().Controls" be indexable?

1989lzhh
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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a chance to imprive Python for .net to let "System.Windows.Forms.Form().Controls" be indexable?

Barton
Yes, this is well worth looking into. I may be able to get to this this weekend.
Thank you.

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet

_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a chance to imprive Python for .net to let "System.Windows.Forms.Form().Controls" be indexable?

Barton
In reply to this post by 1989lzhh
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 - [hidden email] http://mail.python.org/mailman/listinfo/pythondotnet

_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a chance to imprive Python for .net to let "System.Windows.Forms.Form().Controls" be indexable?

Barton
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 - [hidden email] http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________ Python.NET mailing list - [hidden email] http://mail.python.org/mailman/listinfo/pythondotnet

_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a chance to imprive Python for .net to let "System.Windows.Forms.Form().Controls" be indexable?

1989lzhh
In reply to this post by Barton
Hi Barton,
Thanks for your reply,I really appriciate!
Here is my thought:

在 2011年9月4日 上午3:35,Barton <[hidden email]>写道:
Yes, I agree that the Item Property is still there.
It seems to be exposed ONLY FROM IEnumerator.GetEnumerator() or the Findxxx() functions provided by the class due to its virtual nature:

public virtual Control this [ int index ] { get; }
    I don't think the Item Property isn't exposed due to its virtual nature. I did some test in C#,it turns out that the virtual modified Item property can be accessed by python .net with the index manner .I noticed there is a new function "get_Item"  being add to the instance object. The same new function "get_Item" can be find in the Form().Controls but Form().Controls can not be indexable. It is wield.
I check the source code of python for .net and find this from "classmanager.cs":
                   // Check for indexer
                    ParameterInfo[] args = pi.GetIndexParameters();
                    if (args.GetLength(0) > 0) {
                        Indexer idx = ci.indexer;
                        if (idx == null) {
                            ci.indexer = new Indexer();
                            idx = ci.indexer;
                        }
                        idx.AddProperty(pi);
                        continue;
                    }
Maybe the mechanism of checking for indexer is not perfect?
Thanks!


 

Hope that helps


On 9/3/2011 7:09 AM, 刘振海 wrote:
Hi Barton,
thanks for your reply.
I have check the MSDN documents. It seems the Item properties both exist in .net2.0 and .net4.0 
Public property Item[Int32] Indicates the Control at the specified indexed location in the collection.
Public property Item[String] Indicates a Control with the specified key in the collection.
and I get the blew code from C# 
"
public class ControlCollection : ArrangedElementCollection, IList, ICollection, IEnumerable, ICloneable  {
public virtual Control this[int index] { get; }
public virtual Control this[string key] { get; }
"
In C# the Controls support index,So I think It will be intuitive for Controls to support index. 



2011/9/3 Barton <[hidden email]>
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 - [hidden email]



_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a chance to imprive Python for .net to let "System.Windows.Forms.Form().Controls" be indexable?

Barton
OK - I'll look into this further. It turns out the .NET 3.5 DOES allow this:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Button btnCopy;
            Button btn = new Button();
            this.Controls.Add(btn);
            btnCopy = (Button)this.Controls[0];
            bool yes = (btn == btnCopy);
        }
    }
}

On 9/3/2011 8:31 PM, 刘振海 wrote:
Hi Barton,

    I don't think the Item Property isn't exposed due to its virtual nature. I did some test in C#,it turns out that the virtual modified Item property can be accessed by python .net with the index manner .I noticed there is a new function "get_Item"  being add to the instance object. The same new function "get_Item" can be find in the Form().Controls but Form().Controls can not be indexable. It is wield.
I check the source code of python for .net and find this from "classmanager.cs":
                   // Check for indexer
                    ParameterInfo[] args = pi.GetIndexParameters();
                    if (args.GetLength(0) > 0) {
                        Indexer idx = ci.indexer;
                        if (idx == null) {
                            ci.indexer = new Indexer();
                            idx = ci.indexer;
                        }
                        idx.AddProperty(pi);
                        continue;
                    }
Maybe the mechanism of checking for indexer is not perfect?
Thanks!


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Loading...