Save Form with UserControl state with all the events - c#

I have a Form with variable number of UserControls. This UserControl allow adding/removing labels and has an event of changing the text of each label by additional small form. How to save the state of the Main Form with number of UserControls(with labels) from previous Form loading and editing and event of label name changing?

See this how to save and load the properties to xml. As addition to save all the control you had on the form, loop through the controls and use a propertie in your settings class to save the control name. This can be used when you're instantiate / load the controls back up.

Related

how we can go and go back into a user controller docked fill on the form on keeping the state?

I have form were I display menu on top and a panel docked fill on the rest of body.
And for each item of menu I display a user control in the panel and move the precedent user control.
My question is how can I switch between the user control and keep the same state as when I switch in other user controls.
This might be better as a comment, but having to post as an answer until I get more rep. If the progress value is stored in the control object, you need to save that control's instance in a variable in your form (or somewhere outside the control itself so that specific control instance is saved). When updating the value, access the control through your saved instance.
Or you could save the progress value in your form (to retain state), and the control would use that variable for displaying the progress.
Edit: Without seeing your current code, it's hard to know what's going on. But generally you're better off keeping your worker thread(s) isolated from form controls, so it doesn't block the UI, and using events to update a control when needed. If the control is created in the same form as the thread that updates the progress value variable, it can still access it using 'this' form instance.

How can I copy the databinding from one control to another?

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]);

Assigning events dynamically with a form that has no reference

I've got a class where I pass in a panel reference and in that panel I'm then required to draw multiple other panels that represent my objects. The issue is that when I go to bind dynamically click events to these panels I don't have reference to the Form of which I want to popup on the click event.
The reason for this structure is so that I can redraw my class dynamically and reattach certain events to each object.
Is there any way to do this without moving my classes logic outside of the Assembly or is there any way to attach my click event to a piece of code logic to a sub in my WinForm?
If you have a reference to any control on a Form you can recursively navigate up the Parent property until you hit the Form. Just test that the parent property's type is assignable to a variable of type Form.

Repeat a user control as user clicks next?

I have a user control that consists of some textboxes and checkboxes. Once the user is finished filling the first one, they should be able to add one more form by clicking an "add another record" link button.
How can I repeat this usercontrol as the user clicks?
I am supposed to use C# only.
So, we are talking of UserControl where your fields are located (I mean, that this is a one class inherited from UserControl or Control. )
There are a lot of ways to do it. But, I think, to be mode 'code concise' is to use FlowLayoutPanel
a) Create this panel. (through visual designer i.e.)
b) When user clicks, create your control
c) add your control to layout panel.
var myControl = new MyControlWithForm();
flowLayoutPanel1.Controls.Add(myControl);
One could use flowLayoutPanel1.Controls array to process all filled forms afterwards.

Accessing labels and combo boxes from another form C#

I'm being asked to access labels, text boxes, and combo boxes from another form.
From the input form to the main form using this:
frmInput input = new frmInput();
This ^^ is in my main form.
When i go to use "intelesense" it doesn't show any of the labels or anything that i need.
Suggestions?
Because by default GUI elements are defined with private access. If you want to expose them then define your own properties for these elements. You also needs to pass the reference to your main form to the input form... However not recommended.
Instead you can use Events to communicate data between form and keep the rendering to the control's parent form.
That controls can be declared private or protected and that's why you can't access them. However you can either make them public or access by name:
input.Controls["someButtonName"]

Categories

Resources