I am creating an application where the controls on the form will be created at run-time using web-service. Web-Service return a class object using that information I create control on the UI.
The problem that I am facing is when the controls are rendered on the UI and I want access any one of the control I need to specify name which is hard-coded , as below.
TextBox txtbx = (TextBox)Controls["txtbx1"];
Since the names of the control are also dynamic I don't want to hard-code them.
What is the best solution to solve this problem
Use the FindName(string) method of the containing FrameworkElement (e.g. Window or other container control): TextBox txtbx = (TextBox)mainWindow.FindName("txtbx1");
Related
I have to disable a link on dynamically created user control depending on condition.
How to find type of the control?
I am using a placeholder, hence trying to find the control like below
this.placeholder1.FindControl("usercontrol1").FindControl("div1");
How to find link on usercontrol1 of type UserControl1?
I can't use the name UserControl1 of the .ascx file.
You must recreate dynamic controls on each postback.
Then only you can access those control using
var yourControll= (UserControlType)
this.placeholder1.FindControl("usercontrol1");
I have a form with a lot of user input controls; most of them are optional, and for reasons beyond my control the required elements are scattered around the form. I've been asked to add a button that opens a second form (henceforth called ChildForm) which is linked to the original form (henceforth called ParentForm) and has only the required controls from ParentForm.
I want the controls in ChildForm to be linked to the same datasource as their corresponding controls in ParentForm. I'd like to create this linkage programmatically in a loop so that later changes to ParentForm don't require manually editing the databindings of ChildForm controls.
I tried ChildControl.DataBindings.Add(ParentControl.DataBindings[0]); but I get a dataBinding belongs to another BindingsCollection ArgumentException at runtime.
How can I bind a new control to the same column of a DataTable as an existing control without manually doing it for each control?
If your Binding is simple (doesn't have any Format and Parse event handler registered), you can do a shallow clone like this:
public void CloneBinding(Control control, Binding bind){
Binding bind = new Binding(bind.PropertyName, bind.DataSource, bind.BindingMemberInfo.BindingMember);
control.DataBindings.Add(bind);
}
//Use it
CloneBinding(ChildControl, ParentControl.DataBindings[0]);
In my winforms application I create form elements(Radiobutton, Label etc.) dynamically. I need to access specific form element with its name property. A simple solution is iterate each form element using Form1.Controls and check its name property. But in my opinion this solution is not effective. Is it possible to access specific form element using reflection?
If you have access to the controls collection you can use the Find() method provided
myForm.Controls.Find("ControlName",true);
If you know its type - you can cast it as well
TextBox t = (TextBox)myForm.controls.Find("txtFirstName",false);
The true or false will instruct the function to search for any child controls.
I'm making a settings screen at the moment that will have a radiobutton group which will determine what controls are displayed. The different settings are contained within a UserControl.
I am dynamically creating this UserControl like so:
panel = new btSettings();
this.Controls.Add(panel);
panel.Location = new Point(15, 49);
Just wondering how I can access the fields within this control and design time when the object will only be created during run time?
Thanks.
Answering your question...
Just wondering how I can access the
fields within this control and design
time when the object will only be
created during run time?
If you need to work with the control at design time, I think the only way is to create a custom control and drag it into your form.
You will need to use indirect references to do what you are wanting to do. Its been a while since I worked with user controls, but container controls should support a Controls (or Children) collection which will return the controls that exist in it. You will then need to iterate over them, cast them to the types you care about and work with them. For example:
Foreach(Control target in panel.Controls) {
if (target.GetType() == typeof(RadioButton) {
((RadioButton)target).Checked = true;
//etc...
}
}
You can't get not existing object on design time.
You'll have to programatically set up the control at run time. You could use design time to figure out what you want by statically adding the control, but youll have to programatically set up the control at runtime
I have an interface which return a Control based on File Name
for example if there's an html file I am returing an Instance of WebBrowser Componet.
This WebBrowser Component inherits from WebBrowser Class.
I need to update this component so that it can accomodate a grid as well.
What Should I do ?
I am working on Windows Application C# 2.0
Instead of having your user control inherit directly from WebBrowser, you can instead have it inherit from UserControl - this will make your control something like an ordinary panel, on which you can place one or more other controls. Then have code that either creates a WebBrowser control and adds it to your control's Controls collection, or else creates a grid and adds it to the Controls collection, based on your needs.