Hello,
I have a number of tabs within a radpageview control in tabstrip mode, and I would like to allow the user to close them by pressing the middle mouse button (like a regular web browser).
This is what I've done so far:
and the function:
I've tried calling the Pages.Remove function using the sender object as well as by the tab's index, but the result is always the same:
"nullreference exception" right after the page is removed.
Is there another way I can execute the 'close tab' functionality (just like clicking the 'x' button), without getting a null reference exception?
Thanks in advance!
I have a number of tabs within a radpageview control in tabstrip mode, and I would like to allow the user to close them by pressing the middle mouse button (like a regular web browser).
This is what I've done so far:
//add event handler
newPage.Item.MouseDown +=
new
MouseEventHandler(radButtonItem_MouseDown);
and the function:
private
void
radButtonItem_MouseDown(
object
sender, MouseEventArgs e)
{
//button was clicked by the mouse
if
(e.Button == MouseButtons.Middle)
{
MessageBox.Show(
"Middle button"
);
rpvApplication.Pages.Remove(((RadPageViewStripItem)sender).Page);
// crashes!
//rpvApplication.Pages.RemoveAt(3); // crashes!
}
}
I've tried calling the Pages.Remove function using the sender object as well as by the tab's index, but the result is always the same:
"nullreference exception" right after the page is removed.
Is there another way I can execute the 'close tab' functionality (just like clicking the 'x' button), without getting a null reference exception?
Thanks in advance!
6 Answers, 1 is accepted
0
Accepted
Hi Themos,
Thank you for writing.
The desired functionality can be easily achieved by using the MouseDown event of RadPageView. Here is a sample:
I hope this helps.
Kind regards,
Stefan
the Telerik team
Thank you for writing.
The desired functionality can be easily achieved by using the MouseDown event of RadPageView. Here is a sample:
void
radPageView1_MouseDown(
object
sender, MouseEventArgs e)
{
if
(e.Button == System.Windows.Forms.MouseButtons.Middle)
{
RadPageViewStripItem item = radPageView1.ElementTree.GetElementAtPoint(e.Location)
as
RadPageViewStripItem;
if
(item !=
null
)
{
radPageView1.Pages.Remove(item.Page);
}
}
}
I hope this helps.
Kind regards,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Themos
Top achievements
Rank 1
answered on 26 Jul 2012, 09:03 AM
Thanks for your reply, this was just what I needed!
The problem was that I was setting the event handler on the tabitem itself, instead of the radpageview!
I simply modified your code to prevent the user from closing some specific tabs, by using the following code:
if (item != null && item.ButtonsPanel.CloseButton.Visibility != Telerik.WinControls.ElementVisibility.Collapsed)
{
rpvApplication.Pages.Remove(item.Page);
}
On a similar note, is there a way to override the default behavior of the Radpageview strip buttons, and specifically the Close Button, so as to prevent the user from closing those tabs that have their closebutton visibility set to collapsed?
Thanks again for your time!
The problem was that I was setting the event handler on the tabitem itself, instead of the radpageview!
I simply modified your code to prevent the user from closing some specific tabs, by using the following code:
RadPageViewStripItem item = rpvApplication.ElementTree.GetElementAtPoint(e.Location) as RadPageViewStripItem;if (item != null && item.ButtonsPanel.CloseButton.Visibility != Telerik.WinControls.ElementVisibility.Collapsed)
{
rpvApplication.Pages.Remove(item.Page);
}
On a similar note, is there a way to override the default behavior of the Radpageview strip buttons, and specifically the Close Button, so as to prevent the user from closing those tabs that have their closebutton visibility set to collapsed?
Thanks again for your time!
0
Themos
Top achievements
Rank 1
answered on 30 Jul 2012, 06:25 AM
Is there no way to prevent specific tabs from closing? i.e. disabling the radpageview's global close button when those pages are selected?
0
Accepted
Hi Themos,
Thank you for writing.
You can prevent a page from being removed by subscribing to the PageRemoving event of the control:
In order to disable the main close button upon some condition, you can use the SelectedPageChanged event:
I hope this helps.
Regards,
Stefan
the Telerik team
Thank you for writing.
You can prevent a page from being removed by subscribing to the PageRemoving event of the control:
void
radPageView1_PageRemoving(
object
sender, RadPageViewCancelEventArgs e)
{
if
(e.Page.Item.ButtonsPanel.CloseButton.Visibility == ElementVisibility.Collapsed)
{
e.Cancel =
true
;
}
}
In order to disable the main close button upon some condition, you can use the SelectedPageChanged event:
void
radPageView1_SelectedPageChanged(
object
sender, EventArgs e)
{
if
(radPageView1.SelectedPage.Item.ButtonsPanel.CloseButton.Visibility == ElementVisibility.Collapsed)
{
((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.Enabled =
false
;
((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.UseDefaultDisabledPaint =
true
;
}
else
{
((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.Enabled =
true
;
}
}
I hope this helps.
Regards,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Themos
Top achievements
Rank 1
answered on 31 Jul 2012, 07:50 AM
The second part of the code didn't seem to make a difference, but the first one worked like a charm! Thanks a lot!
PS: I now changed the second part of the code to hide the 'X' button instead of disabling it, and it works even better!
PS: I now changed the second part of the code to hide the 'X' button instead of disabling it, and it works even better!
0
Hello Themos,
I am glad that I could help. You are right, enabling does not work out of the box and collapsing would be easier in this case. However here is the whole sample with disabling if someone is interested:
Regards,
Stefan
the Telerik team
I am glad that I could help. You are right, enabling does not work out of the box and collapsing would be easier in this case. However here is the whole sample with disabling if someone is interested:
public
Form1()
{
InitializeComponent();
}
protected
override
void
OnLoad(EventArgs e)
{
base
.OnLoad(e);
radPageView1.PageRemoving +=
new
EventHandler<Telerik.WinControls.UI.RadPageViewCancelEventArgs>(radPageView1_PageRemoving);
radPageView1.SelectedPageChanged +=
new
EventHandler(radPageView1_SelectedPageChanged);
UpdateCloseButtonVisivility();
((RadPageViewStripElement)radPageView1.ViewElement).ShowItemCloseButton =
true
;
radPageViewPage2.Item.ButtonsPanel.CloseButton.Visibility = ElementVisibility.Collapsed;
radPageViewPage4.Item.ButtonsPanel.CloseButton.Visibility = ElementVisibility.Collapsed;
((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.RadPropertyChanged +=
new
RadPropertyChangedEventHandler(CloseButton_RadPropertyChanged);
}
void
CloseButton_RadPropertyChanged(
object
sender, RadPropertyChangedEventArgs e)
{
if
(e.Property.Name ==
"Enabled"
)
{
UpdateCloseButtonVisivility();
}
}
void
radPageView1_SelectedPageChanged(
object
sender, EventArgs e)
{
UpdateCloseButtonVisivility();
}
private
void
UpdateCloseButtonVisivility()
{
if
(radPageView1.SelectedPage.Item.ButtonsPanel.CloseButton.Visibility == ElementVisibility.Collapsed)
{
((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.UseDefaultDisabledPaint =
true
;
((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.Enabled =
false
;
}
else
{
((RadPageViewStripElement)radPageView1.ViewElement).ItemContainer.ButtonsPanel.CloseButton.Enabled =
true
;
}
}
void
radPageView1_PageRemoving(
object
sender, Telerik.WinControls.UI.RadPageViewCancelEventArgs e)
{
if
(e.Page.Item.ButtonsPanel.CloseButton.Visibility == ElementVisibility.Collapsed)
{
e.Cancel =
true
;
}
}
}
Regards,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>