views:

55

answers:

2

I'm still stuck.

Assume that I've got a user control with a button. And an event called damnIt_ButtonClicked. In the main window I want to emulate the control's lifetime like it is a modal dialog, although it's not.

I want to wrap everything into one method, it returns true if the Button on the control clicked.

  public bool Show() {

      var control = new ControlWithSingleButton();
      bool result;

      control.damnIt_ButtonClicked += (object sender, EventArgs args) =>
      {
            result = true;
      };

      MainWindowGrid.Children.Add(control);
      MainWindowGrid.Visibility = Visibility.Visible;


      return result;
  }

Now. As you see the problem is this method will return always false; But I need to return a result only when damnIt_ButtonClicked event fires. It means I have to put the thread on wait, till the user clicks button. Right? Or how it should be done. Help me please....

A: 

You're going to need to re-architect your solution. Without knowing a broader scope of what you're trying to do, here's a possible solution.

private bool buttonResult;

public void Show() {

  var control = new ControlWithSingleButton();
  bool result;

  control.damnIt_ButtonClicked += (object sender, EventArgs args) =>
  {
        this.ProcessButtonClick();
  };

  MainWindowGrid.Children.Add(control);
  MainWindowGrid.Visibility = Visibility.Visible;

  }

private void ProcessButtonClick()
{
   this.buttonResult = true;
   //do whatever you would have before if Show had returned true
}
Steve Danner
But I need to return the result in the Show() method.
Ike
#1 need to create a control and inject it into the grid#2 wait till user clicks the button#3 return a result.I have a problem with #2
Ike
I am actually trying to create "flat modal dialog". Although technically It's not gonna be a dialog at all. It's just a user control with a button that I'm gonna place into MainWindow's grid.I just need some method that I'm gonna able to call and it will return the result - either user clicked the button or not...
Ike
I just want to avoid of exposing events I want to wrap that up into one method.
Ike
@Ike You could still use Steve's solution, but inside the Show() method disable all of the other controls on the Form, and inside the ProcessButtonClick() method re-enable them.
Justin
If you do everything synchronously (waiting on the user to do an action before you allow code execution to continue), the way you want to, the UI is going to freeze up and the user won't be able to do anything.
Steve Danner
@Justin but how am I suppose to return the value? I need to wrap it up into one static method, so I could call it from any other place
Ike
@Steve Danner I understand that, so what do you suggest?
Ike
@Steve Danner What if I create a control in one Thread(thread#2) and pass that object to the UIThread, add it to the grid in the UIThread but listen to the events in the thread#2.. or something like that...
Ike
You could certainly do that, but doesn't the solution I provided above seem a bit more straight forward? The solution above should provide everything you need to avoid thread freezing and such.
Steve Danner
A: 

You know what? I give up!

I decided to make the control a window, although it was strictly prohibited in given specifications to use any other windows but the Main. Anyway it's gonna be a chromeless, borderless transparent window, so nobody can see the difference.

Thank you so much.

Ike
Nice -- much better than whatever hacks would have been needed to get it working the other way.
Justin