MessageBox Location

1 Answer 552 Views
MessageBox
Jure
Top achievements
Rank 2
Iron
Iron
Iron
Jure asked on 03 Nov 2021, 12:40 PM | edited on 03 Nov 2021, 12:42 PM

Hi,

I'd like to customize the location of a MessageBox. Ideally, I would just set a parent control and the MessageBox would center itself to the parent.

I tried this ((0,0) to make it obvious) but it doesn't work:  


  'Dim pt As Point = New Point(Me.Parent.Location.X + Me.Location.X, Me.Parent.Location.Y + Me.Location.Y)
  'Telerik.WinControls.RadMessageBox.Instance.Location = New Point((pt.X + Me.Width / 2) - (Telerik.WinControls.RadMessageBox.Instance.Width / 2), (pt.Y + Me.Height / 2))
  Telerik.WinControls.RadMessageBox.Instance.Location = New Point(0, 0)
  Dim ds As DialogResult = Telerik.WinControls.RadMessageBox.Show(Me, "Are you sure?", "Clear form",
                                                                        MessageBoxButtons.YesNo, RadMessageIcon.Question,
                                                                        MessageBoxDefaultButton.Button1)

I've also found this bit of code (modified it according to my needs) from an older answer to someone else's similar question:

Private Sub Instance_LocationChanged(sender As Object, e As EventArgs)
        RemoveHandler RadMessageBox.Instance.LocationChanged, AddressOf Instance_LocationChanged

        Dim box As RadMessageBoxForm = sender
        Dim pt As Point = New Point(Me.Parent.Location.X + Me.Location.X, Me.Parent.Location.Y + Me.Location.Y)
        box.Location = New Point((pt.X + Me.Width / 2) - (box.Width / 2), (pt.Y + Me.Height / 2))

        AddHandler RadMessageBox.Instance.LocationChanged, AddressOf Instance_LocationChanged
    End Sub

But the problem here is that the MessageBox is then static and can't be moved.

Suggestions?

Using version 2021.1.326.40.

Jure

1 Answer, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 05 Nov 2021, 08:53 AM

Hello Jure,

Thank you for the provided code snippet.

I have double check your scenario on my side but wasn't able to observe the described behavior. On my side, the RadMessageBox is positing in the center of its parent when shown. I think I am missing something from your setup. You can share what type of form, window, usercontrol are you using as a parent. Could it be possible to isolate this behavior in a standalone project so that I can take a closer look and provide a suitable solution?

I am looking forward to your reply.

Regards,
Dinko
Progress Telerik

Remote troubleshooting is now easier with Telerik Fiddler Jam. Get the full context to end-users' issues in just three steps! Start your trial here - https://www.telerik.com/fiddler-jam.
Jure
Top achievements
Rank 2
Iron
Iron
Iron
commented on 05 Nov 2021, 09:01 AM | edited

Thanks for replying Dinko. I think you're probably missing the point ;) - it's not a problem of MessageBox not being in the center of a form. Maybe I explained it poorly. I have a child user control, let's say a panel on the right side. And I want the MessageBox be centered on that panel, not on the form itself. Basically I want to position it anywhere on the screen. Does that work in your situation? I can prepare a standalone project if it's still not clear enough.
Dinko | Tech Support Engineer
Telerik team
commented on 10 Nov 2021, 09:00 AM

Thank you for the provided details. Now I see what you have in mind. Upon checking the source code, when the RadMessageBox Show method is called, internally we are looking for a parent of type Form. If we could not find such, the control will be centered on the main screen. Now in your case, to set custom location, you can use the Location property as you have already done. The control is not draggable because the LocationChanged event is called every time. You just need to move the logic from the LocationChanged to the Shown method, which will be called one time.

private void ShowMessageBox_Click(object sender, EventArgs e)
{	
	Telerik.WinControls.RadMessageBox.Instance.LocationChanged -= Instance_LocationChanged;
    Telerik.WinControls.RadMessageBox.Instance.LocationChanged += Instance_LocationChanged;

    Telerik.WinControls.RadMessageBox.Instance.Shown -= Instance_Shown;
    Telerik.WinControls.RadMessageBox.Instance.Shown += Instance_Shown;

     Telerik.WinControls.RadMessageBox.Instance.Opacity = 0;
    DialogResult ds = RadMessageBox.Show(this.panel1, "Are you sure?", "Clear form", MessageBoxButtons.YesNo, RadMessageIcon.Question, MessageBoxDefaultButton.Button1);
}

private void Instance_Shown(object sender, EventArgs e)
{
    Telerik.WinControls.RadMessageBox.Instance.Location = new Point(50,50);
    Telerik.WinControls.RadMessageBox.Instance.Opacity = 1;
}

As a side note, when you use the above approach you could observe some flickering behavior. To remove it you can hide the control, change its Location and show it again.

I think that this approach is what you are looking for.

Jure
Top achievements
Rank 2
Iron
Iron
Iron
commented on 10 Nov 2021, 01:38 PM

Perfect! Thank you Dinko. To be clear(er): the code dealing with the location in the Instance_LocationChanged can (must) be completely removed for the desired behavior. Meaning if no other logic is there just delete it.
Dinko | Tech Support Engineer
Telerik team
commented on 11 Nov 2021, 12:19 PM

The logic inside the LoctionChanged event needs to be removed or moved to the Shown method event handler. In the Shown event handler you can set the initial location of the MessageBox.
Tags
MessageBox
Asked by
Jure
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or