Silverlight tabchanged event - tabcontrol - c#

I'm using tab control and I want to handle tabchanged event.
I was trying to use SelectionChanged event with no luck. It's being fired too many times (after loading tabcontrol, or adding new tab).
I would like to handle this event only when user navigates between tabs.
I have found solution for WPF (Is there Selected Tab Changed Event in the standard WPF Tab Control) but it's no good for Silverlight.
TIA.

Firing "too many times" should not be a problem if you check for an actual change to the SelectedIndex property in the event.
private int LastSelectedTab = -1;
void tab_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tab = sender as TabControl;
if (this.LastSelectedTab != tab.SelectedIndex)
{
this.LastSelectedTab = tab.SelectedIndex;
// Now do your thing...
}
}

Related

Every second tabpage added to tabcontrol using ctrl+tab does not fire events

I have a form with a tabcontrol and multiple tabpages. Those tabpages can be added dynamically and then
create some child controls.
The process is to click the last tabpage's header ("+"). That fires a deselecting and selecting event on the tabcontrol, for the current page and the +page respectively. Selecting the +page causes a new page to be inserted at the second to last index (and creating childcontrols) and to select it. This last part again fires the deselecting and selecting events on the tabcontrol, now for the +page and new page respectively.
My problem is that when using ctrl+tab to add a tabpage, the selecting and deselecting events for every second new tabpage are not fired. This causes the new tabpage to be empty of child controls.
The weird thing is that it works perfectly when using the mouse, arrow keys, and navigating through existing tabpages in any way. Only creating new pages using ctrl+tab doesn't work correctly.
public FormConstructor()
{
tabControl.SelectedIndex = 0;
tabControl.Selecting += tabControl_Selecting;
tabControl.Deselecting += tabControl_Deselecting;
}
private void tabControl_Deselecting(object sender, TabControlCancelEventArgs e)
{
//Code removes events from and disposes of childcontrols - works correctly
}
private void tabControl_Buizen_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPage != tabPage_plus) e.TabPage.Controls.Add(new TextBox()); //create controls
if (e.TabPage == tabPage_plus)
{
tabControl_Buizen.TabPages.Insert(e.TabPageIndex, "Buis " + (e.TabPageIndex + 1).ToString()); //create new tabpage
tabControl_Buizen.SelectedIndex -= 1; //Commenting this out and selecting manually works
}
}
I have found that not selecting the newly created page automatically, and instead navigating backwards using ctrl+shift+tab works fine, but I have not been able to find out why. I have taken a look at the SelectedIndex property, but do not know if this is the cause.
Does anyone have an idea what causes this behaviour of events not being fired? I can add more code if needed.
Why is the tab page body not updating with a .NET tab control?
So after searching for over 3 days I stumbled upon this by clicking through layers of related questions.
It looks like the timing of changing the selected tab while the tabcontrol is still processing events messes it up to the point it stops firing events (in my case).
Substituting the last line of code with this.BeginInvoke(new Action(() => tabControl_Buizen.SelectedIndex -= 1)); has solved my problem.

winforms tab page event not firing

I have a TabControl to which the user can add tab pages.
I am trying to attach some events to it such as: MouseEnter, MouseLeave, MouseClick, But it seems the these events are not firing at all, they only fire when I attach them to the TabControl itself, but this is not what I need.
What is the problem with attaching events to a tab control tab page ?
Here is my latest attempt to attach these event from my code:
private void customerTabCtrl_ControlAdded(object sender, ControlEventArgs e)
{
TCTabPage tctab = (TCTabPage)e.Control; // Option A
TCTabPage tctab = (TCTabPage)customerTabCtrl.Controls[customerTabCtrl.Controls.Count - 1]; //Option B
tctab.MouseEnter += new EventHandler(tctab_MouseEnter);
tctab.MouseLeave += new EventHandler(tctab_MouseLeave);
}
I fill so silly...
I found out the "problem", I thought that the MouseEnter, MouseLeave, MouseClick events should fire even when the cursor is on the tab header, but it appears that these events fire only when the cursor is at the tab body...
Sory for the trouble, I am using winforms only 6 months now...
You don't need an event for this since by default, the end user cannot add TabPages to the TabControl without you providing the code for it.
So wherever you are adding the TabPage, that's when you should wiring up those events:
TCTabPage tctab = new TCTabPage();
tbtab.Text = "New Tab";
tctab.MouseEnter += tctab_MouseEnter;
tctab.MouseLeave += tctab_MouseLeave;
customerTabCtrl.TabPages.Add(tctab);

How to check if DataGrid control has lost focus

After datagrid gets focused it shows some buttons on the form. But what is the event that I should use to hide those buttons after datagrid loses it's focus? I should probably mention that this project is done in .Net 2.0.
You should register on dataGridView.Leave event.
gridview.LostFocus += new EventHandler(gridview_LostFocus);
public void gridview_LostFocus(object sender, EventArgs e)
{
//Do stuff when focus is lost
}

determine if item is selected in C# ComboBox

I am developing a windows forms application and load the list from this code:
private void showList()
{
TeamTableAdapter teamAdapter = new TeamTableAdapter();
lstTeamName.DataSource = teamAdapter.GetTeamsActive();
lstTeamName.DisplayMember = "TeamName";
lstTeamName.ValueMember = "TeamID";
}
I want to enable a button if the user selects one of the items. What event should I put the code into. I the following code but the event seems to fire before the user clicks on the list.
private void lstTeamName_Click(object sender, EventArgs e)
{
if (lstTeamName.SelectedIndex > -1)
btnImportXML.Enabled = true;
}
I moved my code to the SelectedIndexChange event but it still fires before the user selects an item and the selectedIndex is 0.
You dont want to bind to the Click event but to the SelectedIndexChanged event. You should be able to accomplish this by simply double clicking on the Control in designer.
I would agree that you don't want to bind to Click as that will likely fire too early.
I recommend you look into the DropDownStyle property. http://msdn.microsoft.com/en-us/library/system.windows.forms.comboboxstyle(v=vs.110).aspx. If you set that to DropDownList then the SelectedItemChanged will fire and SelectedIndex could be > -1
If you leave it as the default DropDown then you may want to use TextChanged and check the Text property.

Combobox and autocomplete in C#

i have small problem with autocomplete option in combobox. Everything is working correct, except that i want to work it diffrent :)
When I start typing in combobox, autusuggest working the way i like :
But when i first open combobox, and then start typing i get something like that:
What's more i can't pick item from autosuggest combobox, only from this list under.
AutocompleteMode is SuggestAppend
I'd like to have autosuggest like on the first picture, and in situations like picture 2, this first combobox list should be closed somehow..
I had the same problem and solved it this way:
private void comboBox_DropDown(object sender, EventArgs e)
{
ComboBox cbo = (ComboBox)sender;
cbo.PreviewKeyDown += new PreviewKeyDownEventHandler(comboBox_PreviewKeyDown);
}
private void comboBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
ComboBox cbo = (ComboBox)sender;
cbo.PreviewKeyDown -= comboBox_PreviewKeyDown;
if (cbo.DroppedDown) cbo.Focus();
}
Once the user clicks on the DropDown button PreviewKeyDown event is attached to that ComboBox. When user starts typing, freshly added event is triggered. In that event we check if ComboBox is DroppedDown, if it is, focus that ComboBox. On ComboBox focus DropDown disappeares and that's it.
What about using the DropDown and DropDownClosed events to disable or change the auto-complete mode?
I was having exactly the same problem.
I tried the DropDown and DropDownClosed events to set the AutoCompleteMode property to none and suggest.
In this situation the SelectedIndexChanged event does not get fired after selecting an item with the mouse.
I was using the SelectedValue property in the SelectedIndexChanged event and this property is already changed at the moment the DropDownClosed event is triggered.
In my case I simply called the SelectedIndexChanged method from the DropDownClosed event to solve the problem.
Implement event on ComboBox KeyDown. It should look like this.
void cmbExample_KeyDown(object sender, KeyEventArgs e)
{
if ((sender as ComboBox).DroppedDown)
(sender as ComboBox).DroppedDown = false;
}
Have you tried the other possible values for AutoCompleteMode, which are Append, None, and Suggest? I think that what you are looking for is Suggest instead of AppendSuggest.
Here is some downloadable sample code illustrating the different modes, if you need it.
I also found the default UI implementation distracting as the two dropdowns fight for mouse control.
You want to hide the dropdown list when autocomplete suggestions are shown. There is a windows message that the combobox makes before showing the autocomplete suggestions. I chose to collapse the droplist in response to this message. It requires a small override of the combobox to achieve this:
Public Class Combobox2
Inherits ComboBox
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = 135 AndAlso DroppedDown Then 'WM_GETDLGCODE
DroppedDown = False
End If
MyBase.WndProc(m)
End Sub
End Class
void cmbExample_KeyDown(object sender, KeyEventArgs e)
{
cmbExample.DroppedDown = false;
}

Categories

Resources