Click here to Skip to main content
15,917,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used all methods and properties possible such as SelectTab, SelectedIndex, SelectedTab, but they didn't work.
I tried tabPage1.Show() but it showed only the Page not the "Tab"(where the tabPage's title is), for example, if the tabPage1 has a title like "TabPage1", the same for 2, 3. If I click on one of these titles, the corresponding tabPage will be shown, but when I want to do that by coding, suppose the title "TabPage2" is focused, if I try using tabPage1.Show() to show all tabPage1 with its title being focused, I won't get the expected result! The title of tabPage1 won't be focused (as if it is after being clicked on), I say that the "Page" is shown but the "Tab" is not, and I have this "Page" goes with that "Tab". It is so messy!
Could you please tell me how to select a tabPage using coding with the same effects like using mouse to click directly on it?
Thank you so much!
Posted
Updated 19-Apr-11 15:09pm
v2
Comments
Sergey Alexandrovich Kryukov 19-Apr-11 21:41pm    
Tag it! Forms, WPF or what? Please tag it -- always.
--SA
[no name] 20-Apr-11 8:09am    
Could you please give me an example of how to tag with 2 tags such as "C#" and "Form" together. I only know how to do with 1 tag only "C#" or "Form". Maybe just for I use mobile phone, even I don't know what tags are supported. It will be easier if I use computer. In these days, I intend to ask all questions about Form, even I have never created a WPF project!
Thank you!
thatraja 20-Apr-11 12:00pm    
It's simple....
Type C# (But it will show Tag list [In autocomplete])
Then put ,(comma)
And give space (Now it will show Tag list again[In autocomplete])
select Tag which you want
[no name] 20-Apr-11 16:17pm    
Thank you! Nice!

Surely SelectedTab[^] can achieve this.

Can you include your code in your question? Maybe some code might block this thing in your code.
 
Share this answer
 
Comments
BobJanova 20-Apr-11 7:48am    
Correct and quick = 5. SelectedTab is the best way to select a tab in a tab control.
[no name] 20-Apr-11 8:22am    
Not simple like that! As I said in my OP, could you please give me some cause that makes it not work?
My tabPage1 should be selected right after the tabControl is shown, yeah maybe this is the problem, if my tabControl is always shown when selecting a tabPage using code, it will may work, but the tabControl is hidden first, right after it is shown, one of its tabPages (specified by me) should be selected!
Thank you so much!
thatraja 20-Apr-11 11:57am    
Dude actually 'SelectedTab' surely select the tab which you want. But I mentioned my guess is you6 may be included some other code in the same method which may be an issue. Better you may include your code in your question so that we can take a look at code & can give you solution.
[no name] 20-Apr-11 16:36pm    
My code has nothing special! Just for I use mobile phone, so I will describe some points here:
I have 2 forms, MainForm and TabForm. MainForm contains button1 that whenever clicking on it will show TabForm, TabForm contains tabControl1 which has 4 tabPages.
Here are some codes:
private void onbutton1clicked(object sender, EventArgs e)
{
TabForm.Show();
tabControl1.SelectedIndex = 0;
//or tabControl1.SelectedTab = tabPage1;
}
Now, if my TabForm had tabPage2 selected, and then I HID it. Next I clicked on button1 on MainForm but the TabForm shown with no change in the appearance (tabPage2 still be selected not tabPage1 as I want).
I tried placing the command "tabControl1.SelectedTab" some where else but nothing different happened( such as placing it in a handler for some events of the tabControl1: VisibleChanged, Enter, SelectedIndexChanged, Selected ...) many ways have been tried!
Thank you so much!
The tabControl is hidden. tabControl.Visible = false;

Showing tabControl and select new tabpage.
this.tabControl.Visible = true;
this.BeginInvoke( new Action(() => this.tabControl.SelectedTab = myTab));


The use of "BeginInvoke" on a Control class. Just enqueue a command on the message pump.

---


Use the function as "Solution 1". TabControl.SelectedTab.

Just remember that the TabPage has to be added to TabControl before it will work.

C#
this.tabControl_ViewMode.SelectedTab = this.tabPage_Phonebook;


Use events TabControl.Selecting, you have the ability to cancel the selection and TabControl.Selected occurs when selection has been done.
 
Share this answer
 
v3
Comments
[no name] 20-Apr-11 8:25am    
Thank you! Please see my comment under the "Solution1". Of course, I have added all tabPages at design time before running any codes of mine!
Kim Togo 20-Apr-11 16:08pm    
Okay. The tabControl is hidden first, and when the time is write. You call

tabControl.Visible = true;
tabControl.SelectedTab = myTab;

Correct? - See my update solution.
[no name] 20-Apr-11 16:43pm    
Thank you! Please wait for my test! Because I am about to sleep! Anyway thank you for the interesting info on "BeginInvoke"!
My 5.004U first then accepted answer later!
you can also use SelectedIndex of Tab Control. find the index and set
TabContorl1.SelectedIndex = index
 
Share this answer
 
Comments
Robert_Pan 20-Apr-11 5:14am    
As he said
[no name] 20-Apr-11 7:53am    
I have tried all the ways possible, of course the tabPages are added at design time before any codes run!
I want the tabpage1 will always be shown whennever the form containing it shows no matter what other tabPage was shown since the last time my form was closed!
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900