c# winforms Child form load problem - c#

I have a dialog-based application.
I need to show a child window like this:
(1) First, the application's main dialog window will show up,
(2) then, a child dialog window will show up automatically on top of that.
You know, it is not enough to call the child window's ShowDialog() in the parent window's constructor or load event. Coz in those cases, the child window will appear first.
What should I do to achieve that?

Use can use the event Shown of your main dialog, to show the child in front of you main dialog. This event is only raised once, when the main dialog is shown the first time. Also you should use the Show() (not ShowDialog) method and then call BringToFront() of your child dialog.
private void OnShown(EventArgs e) {
ChildDialog child = new ChildDialog();
child.Show(this);
child.BringToFront();
}

Related

ShowDialog in mdi

I need to open with Showdialog() in mdi form because I need to stop code until the mdichild finish.
The structure of program is this, I open a mdichild in onCreate of this I instead the class and in the constructor I open de SearchForm
There's the code:
frm_bsq_persona busqueda_persona = new frm_bsq_persona();
busqueda_persona.MdiParent = this.MdiParent.MdiParent;
busqueda_persona.Show();
Thank you very much.
MDI child as dialog form (MDI modal workaround)
http://www.codeproject.com/Articles/48436/MDI-child-as-dialog-form-MDI-modal-workaround
It works by disabling all of the controls on the parent form, and providing you with an EventReceiver method to receive the DialogResult from the dialog.
There are some caveats. State of the parent form is not preserved perfectly, since all controls are re-enabled on return. If you want that, you'll have to save the state of the parent form (or maybe just those controls that are already disabled) before creating the dialog, and restore the state when the Event Receiver is called.

Returning a DialogResult from child Form referenced in parent Form project

In a multiple projects solution I had a startup WinForms project with 2 Forms, expecting a DialogResult whenever the child Form was shown to continue execution in my main Form.
var form2 = new ThisNameSpace.FormChild();
var dResult = form2.ShowDialog(this);
if(dResult == DialogResult.OK)
{
// Do some work
}
else
{
// Do other work
}
For some reason, I had to move the child Form in another project. I've referenced the new Project's dll in my main startup Form project but I'm now unable to return a dialog result. When my child Form closes the FormClosing event of the main Form is raised with CloseReason.None and the application exits. How can I work this around?
You should not raise FormClosing event yourself, and you should not write any custom code to just close the dialog.
Instead do this:
on your OK button in child form, set DialogResult property to OK
on your child form, set set Accept Button property to point to your OK button
that's all you need to close the window and correct DialogResult will be returned.
If you ever need to close it 'manually' (and this is rare for modal dialogs, i.e. those opened with ShowDialog instead of Show), use Close method.

How to call method of the main WPF window from the modal window?

How to call method of the main WPF window from the modal window?
(In fact I want to start some timer of the MainWindow.xaml.cs when I close some model window.)
Thank you!
What you can do is before opening your second window assign the main window as its owner, then from the modal window call the Owner property, cast it into a MainWindow object and you'll be able to execute the method.
// Code in main window
ModalWindow window = new ModalWindow();
window.Owner = this;
window.ShowDialog()
//Code on the modal window
var myObject = this.Owner as MainWindow;
myObject.MyMethod(); // Call your method here.
public class ModalWindow : Window
{
private MainWindow _parent;
public ModalWindow(MainWindow parent)
{
_parent = parent;
Owner = parent;
}
void CallParent()
{
_parent.Call();
}
}
I would suggest this kind of pattern is a code smell.
If your goal is to pass information from the modal to the parent, better to expose properties on the modal window and, after it closes, read those values and perform whatever actions are necessary in the parent.
If your goal is to pass information from the parent to the modal, then pass it into the constructor or public properties before calling ShowDialog().
There are a lot of ways to do this.
You could overload the constructor of the modal window such that you can pass in a reference to that function, or the main window. Or, add a property to that window.
You could also start the timer on the next line in the main window code that shows the modal window.
If this model window will open from the MainWindow, like this let's say
modalWindow.ShowDialog()
it's enough just to add the code after this call and it will be executed after the modal window closed.
If the modal window is opened form somewhere else but on closing should run the code on complitely unrelated part, can use, for example, Commands or RelayCommand (kind of direct delegate call).
Hope this helps.
You could attach an event handler to the "Closing" event of the modal dialog, which would be executed in the main program when the dialog fires this event.

Communication between parent and child window in wpf

I have a parent window which launches a child window, after doing some selection/operation in the child window is closed and I want to send some info back to the parent window (a custom class object), what's the best way to accomplish this in WPF using the features provided by WPF?
You have many options:
You could use a custom event in your child window that the parent window listens to
You could define a delegate in the child window that references a method in the parent window
You could change the constructor for the child window to take a reference to the parent window and call a method on the parent window using that reference
You could possibly use the VisualTreeHelper class to get the parent window and call a method on that reference
Extracted from this link:
The easiest way I have found to pass data from a child window to a
parent window is to use an application wide property. This property is
an object, and is not the most elegant form to pass data from a child
window to a parent, but it's the least amount of programming. The best
way to do this is using get and set accessor properties.
Create a main window (mainWindow) Create a child window (in this case,
Password)
In the main window, the child window must be shown, say, within a
button click. This window would have a button to do something, in this
case, it's to delete a record from the database.
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
Password passwordentry = new Password();
passwordentry.ShowDialog();
if (Application.Current.Properties["PassGate"].ToString() == "mypassword")
{
Code, or call to delete the record;
}
Application.Current.Properties["PassGate"] = "";
}
In the child window (Password), the property for the application is
set using a textbox. This is a simple window that has a textbox called
PasswordTextBox and a couple of buttons, like Accept and Cancel.
private void AcceptButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["PassGate"] = PasswordTextBox.Text;
this.Close();
}

Updating textbox on mainform with a variable on a child form that is launched from main form

I have a windows form app. The main form has a textbox, and a button to launch another form. When the other form is launched, both forms are on screen (but the launched form is modal). The child form has a textbox and button, and when the button is pressed, I want the textbox on the main form (the parent) to be updated with the value in the textbox on the child form.
How is this functionality achieved?
Ideally you want to keep both forms from being dependent on each other, this could be achieved with interfaces:
public interface IMainView
{
public void UpdateValue(string val);
}
public interface IChildView
{
public void Show(IMainView parent);
}
have your main form implement IMainView and the child implement IChildView, the parent calls child.show(this) and the child calls parent.UpdateValue(blah);
Hope this helps.
If the child form is closed when the button is clicked, you could put a public property which wraps the value of the textbox on the child form. Then the main form can read this property after calling ShowDialog.
If you want this to happen without closing the child form, you can create a function on the main form to change the textbox. Then the child form would call that function.
The best ways to achive this situation are clockWize's and Hans Passants's advices.
But what about that?
Write a property for your textbox at parent form, like this.
public string TextBoxText
{
get { return txtTextBox.Text;}
set { txtTextBox.Text = value;}
}
When you are opening the child form set the owner.
ChildForm f = new ChildForm();
f.Owner = this;
f.Show();
Create an event handler to child forms button click event.
public Button1_Click(object sender; EventArgs e)
{
ParentForm f = (ParentForm)this.Owner;
f.TextBoxText = txtChildTextBox.Text;
}
i didn't compile code; so may have errors :)
}
When a button is pressed to close the launched form, returning you to the main form- the launched form's text box is still in scope.
Closing a form is merely changing the object's state, not disposing of it. So in the button eventhandler that launches the form from the main form, the next line after launching your modal window, it can access the text from the object it launched as the textbox is a child of that form's object. Unless you're launching your modal window in another thread, which I wouldn't figure you are since it's modal, when it is closed, it should go to the next line in the buttons eventhandler that launched it.
your main form may have code something like this right now (haven't done winforms in a while so bear with me if I miss something):
public void Button1_Click(object sender, ClickEventArgs e)
{
SomeFormIWantToLaunch launchForm = new SomeFormIWantToLaunch();
launchForm.ShowDialog(this);
}
You need to just add after launchForm.ShowDialog(this); something like:
this.SomeTextBox.Text = launchForm.ATextBox.Text;

Categories

Resources