I'm working with .NET CF framework in c#, and I want to know if I can access the controls somehow like this:
string field="txtName";
this.Controls[field];
or is this impossibile?
I think the method you're after is FindControl - you'll find that method on anything with a Controls collection.
What about using Linq?
var myControl = this.Controls.Cast<Control>().OfType<WhateverControlType>().FirstOrDefault(cont => cont.ID == "myControlId");
Something like that?
I don't see why it would be wrong, the indexer expects a string, and you're passing a string, so for me it's correct.
It is possible to reference a control in the control collection by name (stirng) or index (int). The only thing you will need to do additionally is cast the control into the type of object it is. Something like the following.
MyControl c (MyControl)this.Controls["ControlName"];
Enjoy!
Related
Basically I am using UIAutomationClient.Interop.dll for some UI work I am currently doing and I am facing the following issue:
I have a UI Element I would like to know its Control Type.
UIAutomationClient.Interop.dll exposes the following property: IUIAutomationElement::CurrentControlType property
Above property returns an Int that represents the Control Type ID but not a ControlType object.
Question:
How could I know what is the ControlType of an UI Element by just knowing its ID? I was not able to find out any other useful information.
NOTE:
I am using the UIAutomationTypes.dll to definte the ControlType object
Any idea?
I use GetCurrentPropertyValue(AutomationElement.ControlTypeProperty) in IronPython. Probably this is what you want. It should return ControlType object. Though I use its string representation .ProgrammaticName.lstrip('ControlType.').strip("'").
For those still looking how to do this in 2021 (with IUIAutomationElement).
You simply need to use the LookupById() function provided by the System.Windows.Automation.ControlType class.
//
IUIAutomationElement element;
// ...
// your code to retrieve the element you want
// ...
// the next line fixes a bug in ControlType class, as explained here: https://stackoverflow.com/a/13971182/991782
ControlType.Button.ToString();
var elementControlType = System.Windows.Automation.ControlType.LookupById(element.CurrentControlType);
I need to find out if a Type I'm working with is a Generic 'container', for example if I have a List<int> I need to check if I'm working with a List (I know how to get whether I'm working with an int), how would I do that? (I'm thinking Reflection). Another example, I have a class called StructContainer<T>, I need to find the word (name) 'StructContainer', I'm not too bothered with what 'T' is, using reflection I get StructContainer'1, would hate to have to do some string splitting etc /EDIT: Just to explain further, StructContainer<int> I need 'StructContainer', Tuple<int> I need 'Tuple', List<int> I need 'List' and so forth
Your first question can be achieved in multiple ways:
Check whether your object implements IEnumerable<T>: yourObject is IEnumerable<int>. This only works if you know the type of the object in the container (int in this case)
Use the same solution I described below, just change StructContainer to List.
As to your second question, you can do this:
var yourObject = new StructContainer<int>();
var yourType = yourObject.GetType();
if(yourType.IsGenericType &&
yourType.GetGenericTypeDefinition() == typeof(StructContainer<>))
// ...
string type = yourObject.GetType().Name.Split('`')[0];
i have a problem, the functionality I'm looking for exactly is:
I have a grid and datagrid, according to the line to select the datagrid there will be to introduce a user control or other user controls are different pictures I've made polylinesegments, bezier cuadratic ... to introduce the call will name, which build on a string, but I have no way to call it correctly.
This is what I do and it works by putting the full name:
d48.Children.Add(new tratsPintados.end148());
But put the string, tells me not find the path in the project, what I want is to find the path inside the string.
d48.Children.Add(new thestring());
Any ideas?
If you need to instantiate some class based on its name (without real reference), you will need to use Reflection.
Maybe you can do some lookup by name for the class you need, and then use Activator.CreateInstance to call its default constructor.
I hope this is what you want, the question text is quite confusing to me.
using System.Reflection;
public object GetObjectFromString()
{
string objectName = "WpfApplication1.uc1";
Type newType = Type.GetType(objectName, true, true);
object o = Activator.CreateInstance(newType);
// do what you want with the 'o' variable, maybe cast it to the type you want.
}
How Can I Evaluate a String in C# Windows Application because I need to Dynamically select object in a form based on the Combination of 2 String that give me the name of the needed object
You can tryControlCollection.Find method to find control by name.
For example:
MyForm.Controls.Find("FooButton", true);
Method returns an array of Control element with the Name property set to "FooButton".
There is no C# eval equivalent. But by the link you can find some useful answers. Ofc, if you want to find or evaluate something than winform controls
UPDATE: I think sometimes it is better get control by key directly. For example:
Control control = this.Controls["FooTxtBox"];
if(control==null)
{
MessageBox.Show("Control not found");
}
control.Text = "something";
This is a feature (compiler as a service) that should be available in the next version of the .NET Framework, version 5.
Perhaps reflection could be your solution for this?
Just use the string as the lookup for the Form.Controls collection. Then when you've got the instance of the control, just call whatever method you need on it to select it.
Have a look at this:
http://www.logiclabz.com/c/evaluate-function-in-c-net-as-eval-function-in-javascript.aspx (Link is dead, please provide an updated source)
I'd like to implement MVC while using LINQ (specifically, LINQ-to-entities). The way I would do this is have the Controller generate (or call something which generates) the result-set using LINQ, then return that to the View to display the data. The problem is, if I do:
return (from o in myTable select o);
All the columns are read from the database, even the ones (potentially dozens) I don't want. And - more importantly - I can't do something like this:
return (from o in myTable select new { o.column });
because there is no way to make anonymous types type-safe! I know for sure there is no nice, clean way of doing this in 3.5 (this is not clean...), but what about 4.0? Is there anything planned, or even proposed? Without something like duck-typing-for-LINQ, or type-safe anonymous return values (it seems to me the compiler should certainly be capable of that), it appears to be nearly impossible to cleanly separate the Controller from the View.
Use a view model layer. Your view has to know what it is going to display. I guess its possible to create a view that just formats a multi-dimensional array of data, but that isn't exactly the best reason to go with an MVC solution. You can however populate a view model with an anonymous object for consumption in your view.
Anonymous types are primarily designed to be used within a method. They are not suitable for communication between methods.
If you need to pass a set of data between two functions the best way is to create a new type wrapping the data or use a loser grouping like Tuple<T1,T2> or KeyValuePair<TKey,TValue>
How about this?
I assume that you have an entity class for your table 'myTable' (let's call it 'MyTableEntity'), so why don't you instantiate a new MyTableEntity object and use object initializer to fill only those columns you want?
return (from o in myTable select new MyTableEntity { AColumn = o.column });
This will not translate to a SELECT * as you asked, but you'll still have a way to pass a strongly-typed object to a view.
You have to be careful to just make use of the initialized properties inside the view and that's it.
Does this makes sense for you?
Since no one even attempted to answer my question, I will answer it myself..
It turns out, C# 4.0 supports duck-typing - they call it dynamic typing. However, in using dynamic types to return anonymous types, we lose the benefits of strong types:
Compile-time type-checking
Performance
Intellisense
I've opened a feature request to have strongly-typed anonymous return types here - if you think this would be a useful addition to C# 5, follow the link and let the .Net team know!
On .NET 4.0 Anonymous types can easily be converted to ExpandoObjects and thus all the problems is fixed with the overhead of the conversion itself.
Check out here
You can easily convert anonymous types into dynamic objects, here is the simple implementation of Donymous objects (Dynamic anonymous objects) that can populate from Anonymous object or DataReader.