Sounds a simple question but haven't found a way to do, so would solicit any responses I get.
I have a winform which in turn contains a user control object. based on some condition in the user control, i have to set a value in the winform.
One way could be to pass the winform object as parameter to user control but that would give cyclic dependency. Is there a easy way out?
You can always use events from the user control to the form.
User Control Events in VB and C#
Writing C# Custom Events
Usually I would expose an appropriate event from the usercontrol. The form can subscribe to the event and react accordingly.
Related
I have a custom user control, onto which I place a button control. I set the access modifier of the button to Public. When I drop the user control onto a form, I see the button, but am not able to select it or edit its properties in the form designer.
Ultimately, I want to create a far more complex custom wizard control, with a content panel, "Back" and "Next" buttons, etc. I have successfully created a content panel to which controls can be dropped into at design time on the main form.
However, I am bulked at not being able to edit nested controls on the user control itself.
When inheriting from a user control, or inheriting from a form, one can typically edit properties of controls whose access modifier is set to "Protected".
What do I need to do to be able to access controls of the custom user control from the designer of the form?
I think you need go to the user control designer view to modify the properties of the button (instead of the form where the user control is placed), since it is nested in the user control.
You can make basic changes to a child control of a user control on a form to the limited extent that you can expose the properties of interest via the parent user control designer and support the property changes at design time. You can make much more complex design time behaviors by writing your own custom designers but that is a potentially difficult to very difficult undertaking.
There is a good reason why it does not work the way you probably think it could and should and if you think about it carefully enough for a while, you will understand why. When you inherit from an object, yes you can change properties etc, but in this case you are creating a new type, so you can modify, add, redefine properties.
But when you drop a user control on a form, you are not creating a new type. You are creating an instance of a type, the user control, that is already defined. To the extent you can modify properties, you are modifying state that must be preserved for that instance. State must not only be persisted, but user controls also often involve painting etc so state changes can also have complex behavioral effects and this is a potentially complicated requirement that cannot be completely generalized.
So there are some modifications that are not possible at all, because they imply modifying the type, and you already have a type, you are only creating an instance. For the rest of the possible range of modifications that are ultimately state based, the platform only supports so much of the total possible state management.
I'm new to c# and I'm looking for a way to bind a property of an object of my own to the value of a textbox in a regular form (reset the property of the object everytime the value of the input changes).
I have read some information and it seems that this can be done only for database objects. Can you give me additional information.
Assuming you mean Windows Forms textbox,
say
textBox.DataBindings.Add("Text", obj, "SomeProperty");
whenever you feel like binding it. Bindings are usually done in Form_Load event handler, if the object can be obtained at that time of course, and if there's no complex logic with different data sources.
Note that this will only work in one direction (changing TextBox will yield object property changes). To sync the other way round, the object must implement INotifyPropertyChanged interface.
If you want to persist the information between runs of the application (i.e. have it be saved when you close the app and re-appear when it opens), it's easiest to use the Windows Forms designer (I assume you are coding a WinForms app) to bind the value of the TextBox to an application setting. (This article on validation provides a screenshot similar to what you want.) (EDIT: Here is the exceptional article on the subject: Exploring Secrets of Persistent Application Settings. And here is a snippet page that I put together to discuss binding.)
This binding is automatically two-way, unlike the binding that #gaearon mentions. You just need to make sure that you save the settings (i.e. Properties.Settings.Default.Save()) before closing the application (e.g. as the event handler for the Form.Closing event).
If you need more clarification, leave a comment.
In my project I have a settings form. Where if any changes happen I have to notify user about it if he wants to leave that page without saving his changes. At this time I am doing this by catching every control change event. I am sure there is a better way - like catching the change event from its container. Is it possible?
Rather than worrying about the controls directly, how about creating a Settings class that implements interfaces from System.ComponentModel like INotifyPropertyChanged and IDataErrorInfo and use data binding to get the values in and out of the controls.
Your Settings class can then not only record whether anything has changed but also make validation of the user input easier.
A good place to start is MSDN.
You have the right solution, but you may want to be very generic about catching the change events. For example, you could try something like this right after the InitializeComponent(); line in the constructor:
foreach(Control c in Controls) {
c.TextChanged += new EventHandler(genericTextBox_TextChanged);
}
genericTextBox_TextChanged would set a form-wide hasChanged flag to true, or something really basic like that. You may need to make this into a recursive function that loops through all of the children of c if it has child controls.
Let me offer you some kind of a workaround. My offer is to create a custom DataSet. Then add tables corresponding to the form controls. After this you can bind each form control to this dataset.Pros: You keep all the controls data-bound. So you don't need to care about the changing of particular control. You have just to control dataset changes. Cons (maybe): after this you should rewrite settings preview mechanism. Instead of changing controls you have to change data. IMO, it's not so hard, but I have no idea about this approach in your applicationI think this approach will be, at least, easy to debug.
If it's web, look at the unload event for javascript
I'm looking for an efficient manner in which to handle (methods and events of controls) dynamically created tab pages with dynamically created controls in them (same interface per tab page).
I want to be able to identify what tab page, a button's Click event came from and such behavior.
Would I be set by using a generic collection list in which to store the tab pages in? Where and in what am I looking to hold the references for each individual control, along with their methods and events? Am I also looking to hold the references for each individual control in generic collections lists? I'm hoping to avoid the User Control TabPage inheritance method. Is that going to end up being the most efficient route I'm going to have to take?
That's certainly possible, merely awkward. You can always get a reference to a control back from the Controls collection or the sender argument of an event. It's Parent property gives you the tab page. Etcetera.
Yes, a List<TabPage> would work. But also the TabControl.TabPages property.
Perhaps I'm missing something, but assuming you know the hierarchy of the controls, on click you can cast the sender as a button (as Hans suggested), and get it's parent
if(MyTabPage == (TabPage)((Button)sender).Parent)
OR get the parent's parent
if(MyTabPage == (TabPage)((Button)sender).Parent.Parent)
or however far up the hierarchy you need to go to get to the TabPage to figure out which tab you're on.
Every time when I change some values in form controls, I need to set a property of an object or vice versa. Instead of writing some methods for each control, I want a general solution for GUI elements. I do not use WPF but only C# 3.0.
Any suggestions?
Thanks.
All controls expose events that signal that their value changed. You can subscribe to it and change another control's value.
You could make the object a field in your form. When the relevant event fires from a control, then call the appropriate operation on the object.
Alternatively, have a Presenter/Controller object that stores your form as a field. It could take it as a parameter in it's constructor. This presenter can then subscribe to your form's relevant events and act appropriately. You could go further with this and extract an interface from your form and program to that in the presenter instead which would help testing. Have a look at the MVP pattern.