I am creating a multiple form windows application using C#, I have two form one Parent form and a child form.
From parent form I called the child form to add a tree node in the parent form treeview. After entering the details in the child form and pressing "Add" button I want to close the child form and want to add the tree node in the treeview dynamically inside the parent form.
The value is passing perfect, I am using properties for the transfer. Rest by using this.Show() another parent form opens up. I have already tried Invalidate() and Refresh() but the treeview does not get updated.
Override child form constructor to accept parent form as parameter
ChiildForm chilForm=new ChildForm(parentFormObject);
Now you can call method of parent form that would make the required change on the page.
parentFormObject.RefreshSection();
but the treeview does not get updated
To refresh the treeview you need to rebind it to your datasource after adding the newly added item of child form.
Example:
List<SomeClass> items = new List<SomeClass>();
if(childForm.ShowDialog() == DialogResult.Ok)
{
items.Add(childForm.newlyAddedItem); //you have mentioned that values are passing perfect
//your code for rebinding to the treeview
}
If you want to refresh after clicking Add buttons,
just try to call the load_ function by sending the parameters.
example,
button_click(Object sender,Event_args e)
{
Form_Load(sender,e);
}
Related
I am using an MDI form in which I have given option tool strip menu item as a call to a new child form. On the child form I have some buttons and DataGridView. But when I click the child form option the buttons and DataGridView are not displaying correctly. Provided I have already set the isMdiContainer property to true of MDI form.
The scenario runs successfully when I simply show a form, but the problem occurs when I use the form as a child form. Here is the code which I wrote in a tool strip menu item click:
frm_bank_master myfrm = new frm_bank_master();
myfrm.MdiParent =this;
myfrm.Show();
I'm working on winforms in c#. I have a form which loads other forms in its panel. Now my child forms have many textboxes.
I want to set focus on one of those textboxes by loading my children forms by setting tab index to zero.
But it isn't happening when i load my children forms. I have taken care of the tab stop properties & i also went through the tabbing order of the forms. But the problem is still there.
When i load children forms from startup it focuses zero indexed control. I guess my problem is that i am loading these forms in a panel of a parent form. Any solution to this problem?
The Windows Forms controls in the following list are not selectable. Controls derived from these controls are also not selectable.
Panel
GroupBox
PictureBox
ProgressBar
Splitter
Label
LinkLabel (when there is no link present in the control)
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
You can set focus to a control in a form which is added on a panel of its parent form by creating an instance of child form in the parent form and then setting focus to child form controls.
Make an instance of child form:
ChildForm formInstanceName = new ChildForm();
And then you can set the focus property for particular control as:
formInstanceName .controlname.Focus();
And before doing this make sure you add the child form to the respective panel.
Here is the snippet of the code I used:
Form1 childform1 = new Form1();//creating an instance of child form
private void btn_Click(object sender, EventArgs e)
{
Panel2.Controls.Clear();//To clear existing controls on panel
Panel2.Controls.Add(childform1); // To add child form controls on the panel.
childform1 .textbox1.Focus(); //To set focus to control of child form
}
I hope this helps you..
I have two Forms
Parent Form : properties : Total Value. Button to load child form.
Child Form: property : insert Value.
When I run application, Parent form loaded first. I can load second (Child) form by click button.
Problem is that when I insert value in child form parent form is not showing any changes even I pass value by delegate and other methods.
I don't want to reload Parent Window. Please help
What you should be doing is creating events on the child for (or using the existing events if they will do the job) and having the parent form subscribe to those events.
One common example is to have the parent hide itself, show the child, and then show itself again when the child is closed. Here is some code that does that:
//in parent form
private void someButtonClickHander(object sender, EventArgs args)
{
ChildForm child = new ChildForm();
this.Hide();
child.Closing += (sender2, args2) =>
{
var someResultFromChildForm = child.SomePropertyOnChildForm;
this.Show();
}
child.Show();
}
If the closing event doesn't work for you (maybe you want to do something when the child form presses a button) you may need to have the child create it's own event. There are lots of tutorials on MSDN or other sites on how to do this. If you have trouble with that (or any other aspect of this design) please ask for clarifications in comments.
If you want to keep data consistent between multiple forms, I recommend putting the data into an object that you can reach from both forms.
The most direct way is to implement INotifyPropertyChanged for the object, so you can bind to this object in both forms and any changes made will trigger the property change event.
For more complex scenarios I make an object that has custom events that I can subscribe to, as INotifyPropertyChanged can be lack the subtlety needed in complex scenarios. As an example, I have a "Helm" object for my primary navigation form that all of the UI elements subscribe to. If a navigation event occurs, the helm does all of the navigation and data loading, and then it triggers a series of events that the UI listens to.
For a simple parent child this can seem like overkill, but this model (having one truth for the current state that the UI simply subscribes to) allows your UI to evolve and for each element of the UI to only worry about its own needs.
Have a Property called Parent in your child form. When you create the instance of the Child,Pass the current object( parent form object) to the constructor where you will set it as the Parent property value of child. In Parent form Have a Public Method in your Parent form. P
parent form
Form1 : Form
{
decimal _totalPrice;
public void UpdateTotal(decimal val)
{
_totalPrice=_totalPrice+val;
lblTotal.Text=_totalPrice.ToString();
}
}
Child Form
Form2:Form
{
public Form1 Parent { set;get;}
public Form2(Parent parent)
{
this.Parent =parent;
}
}
When creating the object of Child form
Form2 objChjild=new Child(this);
//do whatever
Now from child form, if you want to update the total,
call like this
this.Parent.UpdateTotal(200);
I have a C# web application using master page...content page...usercontrol that contains a radgrid. To edit a record in that radgrid I launch an custom edit form into a radwindow using a custom url set in the itemcreated event. The edit form contains various controls but at the end of the edit form I will have 2 textboxes and a button. If the user needs to change the values of those 2 textboxes they must click the button to open another .aspx form with usercontrol in a radwindow to perform various database operations to retrieve the new values. I am saving the 2 values into Session so they will be available across the application. My question is how can I implement a delegate to reload the values of just those 2 textboxes when I close the child form. I am not as up on delegates as I need to be.
Thanks
How about having the specific controls listen for the child window closed event?
before you launch the child window add this code:
childwindow.FormClosed += new EventHandler(child_Closed)
then have a class function like this:
void Form117_Load(object sender, EventArgs e)
{
myControl.Text = Session["myControlText"];
myControl2.Text = Session["myControl2Text"];
}
I have a simple problem: I have a main form in win-forms/c#. It has a listbox bound to a database.
When I click a button a new form is created.
When I click a button on the child form, I want to call a method that exists in the main form, that updates the list box or alternatively when the child form closes, to call that function.
Is this possible??
There are many ways to achieve this, but here's a simple way. In your main form, when you create and show a child form, do it like this:
ChildForm child = new ChildForm();
child.Show(this); // this calls the override that takes Owner parameter
Then, when you need to call a method in the main form from the child form, use code like this (assumes your main form is of type MainForm):
MainForm parent = (MainForm)this.Owner;
parent.CallCustomMethod();
A more complex way would be to use a form of dependency injection, where you would pass in a reference to the parent form (or more properly, to its interface) in the constructor of the child form. But the above way is simple and probably effective enough for your purposes (and it actually is a form of dependency injection itself, sort of).
Scenario 1: Call a method in Parent Form on click of button in child form.
Create an Event in Child Form. Raise that event on some Button Click etc. Subscribe to that event in your Parent Form and call the parent's form method inside that.
Scenario 2: Call a method in Parent Form when Child Form is closed.
Handle the FormClosed or FormClosing event of Child Form in the Parent form and call the parent's form method inside that.
ChildForm frm = new ChildForm();
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
void frm_FormClosed(object sender, FormClosedEventArgs e)
{
//Call your method here.
}