how to set a TabControl tab to be invisible - c#

In C# using VS2005 I have a Winforms TabControl with 7 tabs, but I want the last tab to be only visible if a certain configuration option is set.
How to make the TabControl only show the first six tabs? In other words, how do I make the seventh tab not visible?

private void HideTab(object sender, EventArgs e)
{
this.tabControl1.TabPages.Remove(this.tabPage2);
}
private void ShowTab(object sender, EventArgs e)
{
this.tabControl1.TabPages.Add(this.tabPage2);
}
this.tabPage2 is your 7th tabpage, whatever name you give it.

No its not possible to hide a tab in tabcontrol. If you are adding the tabs ar run time then dont add 7th tab if condition not satisfied.
If you done in design time then remove the tab if condtion failed.
yourTabControl.TabPages.Remove(tabPageName);

you can implement a property
public bool TabVisible
{
get
{
return tabControl1.Contains(tabPage2);
}
set
{
if(value == TabVisible) return;
if(value)
tabControl1.TabPages.Add(tabPage2);
else
tabControl1.TabPages.Remove(tabPage2);
}
}
you should also overwrite your disposing function,
you can move out the Dispose function from the designer generated code to your own code, the designer notices that. you see that the components.Dispose(); function can not reach the tabPage any more for disposal, so you need to dispose it manually if it has not been disposed. otherways, especially if you are opening your window many times, you run out of window handles

Related

How can I disable (grey out) specific tabpage(s) of a tab control programatically in my C# form app?

I can't believe that disabling specific tabpages (visible but greyed out that means not clickable) is not possible in Visual Studio C# Form app. I have found just a workaround that I couldn't bring to work.(See link below)
I wanted to ask this question again, because perhaps there is another solution/workaround in the meantime.
How to highlight/grey-out Tabcontrol Tabpage
For just making the tab nonclickable (what is also OK for me) I have used this code, but not working
private void tabControl1_Selecting(object sender,TabControlCancelEventArgs e)
{
if (!e.TabPage.Enabled)
{
e.Cancel = true;
}
}
For example if you want to disable tabPage2 use this code in form load event. It will not show in intellisense but you can type.
tabPage2.Enabled = false;
Then use tab control selecting event like this
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPageIndex < 0) return;
e.Cancel = !e.TabPage.Enabled;
}

TabControl Cancel change of tabs

I am using TabControl_SelectedIndexChanged event when the user change tabs. The TabControl.SelectedIndex / TabControl.SelectedTab return only the new tab. Is there any way I can get the previous tab? Or must I stick with the obvious store the current tab every time I change tabs?
I want to use this to cancel a change of tabs under certain conditions, like there is unsaved changes.
If you want to cancel the change of a tab, you can use the Deselecting event. There you can cancel the change by setting property Cancel of the provided TabControlCancelEventArgs to true.
Check out http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selected%28v=vs.80%29.aspx
There are events better suited for what you want to do.
I used tabControl Selected method to prevent users selecting a certain tab, in other word, to disable a tab page.
TabPage currentPage;
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
if (e.TabPage == tabNotAllowed)
{
tabControl1.SelectedTab = currentPage;
MessageBox.Show("You cannot use the tab you selected.");
}
else
{
currentPage = e.TabPage;
}
}

ToolStripButton "Reset Appearance" / "Fire Leave Event"

I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)

A radio box is automatically checked when the program runs - but it shouldn't be

I'm writing an application in C# using Visual Studio 2010. All of a sudden the strangest thing has started happening, though.
I have two radio boxes at the top of the window, both are set to Checked = False. I have searched everywhere in the code, I see no reason why it would be anything but False.
Now, the first of these two boxes (called Radio1 and Radio2 respectively) has started being automatically checked when the application is executed. This causes a problem since there is an event associated with the boxes being checked, and now this event runs every time the program is opened (resulting in some serious issues).
Has anybody got any ideas why this box is automatically being checked? As I mentioned, I have looked everywhere through the code just in case I had a Radio1.Checked = true; dangling somewhere. But that is not the case.
The RadioButton class contains code to ensure that at least one button in the group is checked when one of them gets the focus and the AutoCheck property is set to True. This implements the standard behavior of radio buttons. If you want non-standard behavior then you have to set their AutoCheck properties to false and implement the checking yourself.
Use a counter mechanism so that you can override the click method the first time. like this
form_load
{
counter=0;
}
private void rb1_Click(object sender, EventArgs e)
{
if (counter == 0) { counter++;}
else {
//Do your stuff
}
}
private void rb2_Click(object sender, EventArgs e)
{
if (counter == 0) { counter++;}
else {
//Do your stuff
}
}
Hope this is helpfull.
There should be no reason for this to happen unless you are setting the property within the designer or via code. Strange.

Is there any way to detect a mouseclick outside a user control?

I'm creating a custom dropdown box, and I want to register when the mouse is clicked outside the dropdown box, in order to hide it. Is it possible to detect a click outside a control? or should I make some mechanism on the containing form and check for mouseclick when any dropdownbox is open?
So I finally understand that you only want it to close when the user clicks outside of it. In that case, the Leave event should work just fine... For some reason, I got the impression you wanted it to close whenever they moved the mouse outside of your custom dropdown. The Leave event is raised whenever your control loses the focus, and if the user clicks on something else, it will certainly lose focus as the thing they clicked on gains the focus.
The documentation also says that this event cascades up and down the control chain as necessary:
The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.
Overriding your UserControl's OnLeave method is the best way to handle this:
protected override void OnLeave(EventArgs e)
{
// Call the base class
base.OnLeave(e);
// When this control loses the focus, close it
this.Hide();
}
And then for testing purposes, I created a form that shows the drop-down UserControl on command:
public partial class Form1 : Form
{
private UserControl1 customDropDown;
public Form1()
{
InitializeComponent();
// Create the user control
customDropDown = new UserControl1();
// Add it to the form's Controls collection
Controls.Add(customDropDown);
customDropDown.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
// Display the user control
customDropDown.Show();
customDropDown.BringToFront(); // display in front of other controls
customDropDown.Select(); // make sure it gets the focus
}
}
Everything works perfectly with the above code, except for one thing: if the user clicks on a blank area of the form, the UserControl doesn't close. Hmm, why not? Well, because the form itself doesn't want the focus. Only controls can get the focus, and we didn't click on a control. And because nothing else stole the focus, the Leave event never got raised, meaning that the UserControl didn't know it was supposed to close itself.
If you need the UserControl to close itself when the user clicks on a blank area in the form, you need some special case handling for that. Since you say that you're only concerned about clicks, you can just handle the Click event for the form, and set the focus to a different control:
protected override void OnClick(EventArgs e)
{
// Call the base class
base.OnClick(e);
// See if our custom drop-down is visible
if (customDropDown.Visible)
{
// Set the focus to a different control on the form,
// which will force the drop-down to close
this.SelectNextControl(customDropDown, true, true, true, true);
}
}
Yes, this last part feels like a hack. The better solution, as others have mentioned, is to use the SetCapture function to instruct Windows to capture the mouse over your UserControl's window. The control's Capture property provides an even simpler way to do the same thing.
Technically, you'll need to p/invoke SetCapture() in order to receive click events that happen outside of your control.
But in your case, handling the Leave event, as #Martin suggests, should be sufficient.
EDIT: While looking for an usage example for SetCapture(), I came across the Control.Capture property, of which I was not aware. Using that property means you won't have to p/invoke anything, which is always a good thing in my book.
So, you'll have to set Capture to true when showing the dropdown, then determine if the mouse pointer lies inside the control in your click event handler and, if it doesn't, set Capture to false and close the dropdown.
UPDATE:
You can also use the Control.Focused property to determine if the control has got or lost focus when using a keyboard or mouse instead of using the Capture with the same example provided in the MSDN Capture page.
Handle the Form's MouseDown event, or override the Form's OnMouseDown
method:
enter code here
And then:
protected override void OnMouseDown(MouseEventArgs e)
{
if (!theListBox.Bounds.Contains(e.Location))
{
theListBox.Visible = false;
}
}
The Contains method old System.Drawing.Rectangle can be used to indicate if
a point is contained inside a rectangle. The Bounds property of a Control is
the outer Rectangle defined by the edges of the Control. The Location
property of the MouseEventArgs is the Point relative to the Control which
received the MouseDown event. The Bounds property of a Control in a Form is
relative to the Form.
You are probably looking for the leave event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.leave.aspx
Leave occurs when the input focus leaves the control.
I just wanted to share this. It is probably not a good way of doing it that way, but looks like it works for drop down panel that closes on fake "MouseLeave", I tried to hide it on Panel MouseLeave but it does not work because moving from panel to button leaves the panel because the button is not the panel itself. Probably there is better way of doing this but I am sharing this because I used about 7 hours figuring out how to get it to work. Thanks to #FTheGodfather
But it works only if the mouse moves on the form. If there is a panel this will not work.
private void click_to_show_Panel_button_MouseDown(object sender, MouseEventArgs e)
{
item_panel1.Visible = true; //Menu Panel
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!item_panel1.Bounds.Contains(e.Location))
{
item_panel1.Visible = false; // Menu panel
}
}
I've done this myself, and this is how I did it.
When the drop down is opened, register a click event on the control's parent form:
this.Form.Click += new EventHandler(CloseDropDown);
But this only takes you half the way. You probably want your drop down to close also when the current window gets deactivated. The most reliable way of detecting this has for me been through a timer that checks which window is currently active:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
and
var timer = new Timer();
timer.Interval = 100;
timer.Tick += (sender, args) =>
{
IntPtr f = GetForegroundWindow();
if (this.Form == null || f != this.Form.Handle)
{
CloseDropDown();
}
};
You should of course only let the timer run when the drop down is visible. Also, there's probably a few other events on the parent form you'd want to register when the drop down is opened:
this.Form.LocationChanged += new EventHandler(CloseDropDown);
this.Form.SizeChanged += new EventHandler(CloseDropDown);
Just don't forget to unregister all these events in the CloseDropDown method :)
EDIT:
I forgot, you should also register the Leave event on you control to see if another control gets activated/clicked:
this.Leave += new EventHandler(CloseDropDown);
I think I've got it now, this should cover all bases. Let me know if I'm missing something.
If you have Form, you can simply use Deactivate event just like this :
protected override void OnDeactivate(EventArgs e)
{
this.Dispose();
}

Categories

Resources