How to open a specific tab in windows form - c#

all these tab are created dynamically in windows form . I want to open specific tab page on button click.
For example when clicking on a button(button is not tab page button ,its some other execution button), i want to display tab3 .
I am able to get no of tab pages, but unable to open specific tab..
private void toolStripButton1_Click(object sender, EventArgs e)
{
int tabcount = Main_tab.TabCount;
MessageBox.Show(tabcount.ToString());
}

TabControl.SelectTab Method
this.tabControl1.SelectTab(1); // by index
this.tabControl1.SelectTab("tab3"); // by tabPageName
this.tabControl1.SelectTab(tabPage); // by tab page
Or
TabControl.SelectedIndex Property
this.tabControl1.SelectedIndex = 1; //Selects second tab of the tab control
or
TabControl.SelectedTab Property
this.tabControl1.SelectedTab = tabPage2;

You can access it through its index and call its methods for showing.
Main_tab.GetControl(index_of_your_tab);

You can find the tabs under Controls of the TabControl.
Use Find to find the specific tab by name
Main_tab.SelectedTab = (TabPage)Main_tab.Controls.Find("tab3", searchAllChildren: false).First();

private void toolStripButton1_Click(object sender, EventArgs e)
{
int tabcount = Main_tab.TabCount;
for (int count = 0; count < class_new_tab.tab_count; count++)
{
Main_tab.SelectTab(count);
//perform tab operation
}
}

Related

C# WinForms contextmenu focus issue when textbox is added

In a C# WinForms application I need to create a ContextMenuStrip with dropdown and textbox:
private System.Windows.Forms.ContextMenuStrip ct1;
private void button_Click(object sender, EventArgs e)
{
var header = new ToolStripMenuItem("Header");
header.Enabled = false;
var options = new ToolStripMenuItem("Options");
for (int i = 0; i < 5; i++)
{
var checkoption = new ToolStripMenuItem("Check Me " + i + "!");
checkoption.CheckOnClick = true;
options.DropDownItems.Add(checkoption);
}
var txt = new ToolStripTextBox();
txt.Text = "changeme";
options.DropDownItems.Add(txt);
options.DropDown.Closing += DropDown_Closing;
ct1.Items.Clear();
ct1.Items.Add(header);
ct1.Items.Add(options);
ct1.Show(this, button.Left, button.Top);
}
private void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked);
}
Now, e.Cancel will prevent closing the dropdown if the reason is ItemClicked, so I can select more items without having to open the menu again:
Please note that "changeme" is a ToolStripTextBox!
Once I focus it (click on it), I can edit the text inside:
After finish editing the textbox, I still can change the checkbox items, but there is no focus indicator:
How can I get back the focus indicator just as shown on the first picure?
Note: if I move the mouse onto "Header", the dropdown will close, and then moving it back to "Options", will reopen the dropdown and then the focus indicator is good again:
How can I do this without closing and reopening the dropdown?
I have tried Select() for the options item, but it did not help, neither Invalidate() on ct1.
Just have found it:
First needs to add a click handler on the dropdown:
options.DropDown.Click += DropDown_Click;
Then in the click handler it needs to be focused:
private void DropDown_Click(object sender, EventArgs e)
{
var dropdown = (ToolStripDropDown)sender;
dropdown.Focus();
}

How to cause validation when clicking on a ToolStripButton Menu from a MDI in Windows Form Application?

I have a ContextMenuStrip with a ToolStripButtonMenu "Print".
A MDI child form is open containing a DataGridView. I am doing a validation to an editable column "Copies" in that grid. I don't want the user to input letters for example. The validation is working fine when leaving the cell but if I am clicking on a control such as the "Print" button, the validation is not caused.
The following screen shot shows how I can click on the "Print" button while the Copies cell contains letters:
// The code for the cell validation
private void QuantitiesDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == QuantitiesDataGridView.Columns[COL_COPIES].Index)
{
QuantitiesDataGridView.Rows[e.RowIndex].ErrorText = "";
int enteredValue;
if (!int.TryParse(e.FormattedValue.ToString(), out enteredValue) || enteredValue < 1)
{
e.Cancel = true;
QuantitiesDataGridView.Rows[e.RowIndex].ErrorText = "Invalid number of copies";
}
}
}
I was looking for a property of the ToolStripButtonMenu such as CauseValidation but there is not such one.
Is there a way to trigger the validation when clicking on one of the ToolStripButtonMenu so the Print button will not be triggered until the Copies value is valid?
In your ToolStripButton's Click method, try calling the active form's ValidateChildren function:
private void toolStripButton1_Click(object sender, EventArgs e) {
if (this.ActiveMdiChild.ValidateChildren()) {
// do your processing ...
}
}

Switching to a tab in TabControl using code

I have a tabcontrol in my application that has several tabs in it.
I want to automatically switch to another tab when the "Next" button is pressed.
I cannot figure out how to change which tab is visible programmatically.
private void Next_Click(object sender, EventArgs e)
{
// Change to the next tab
tabControl1.???;
}
Use the TabControl.SelectedTab property. MSDN.
tabControl1.SelectedTab = anotherTab;
But you can also use the TabControl.SelectedIndex property. MSDN.
try
{
tabControl1.SelectedIndex += 1;
}
catch
{
//This prevents the ArgumentOutOfRangeException.
}
For this particular scenario you can use SelectedIndex property of the TabControl. This gives you an integer representing the index of the currently selected tab. Likewise you can set a tab as selected by setting an integer value to this property.
private void btnNext_Click(object sender, EventArgs e)
{
int currentTabIndex = tabControl1.SelectedIndex;
currentTabIndex++;
if (currentTabIndex < tabControl1.TabCount)
{
tabControl1.SelectedIndex = currentTabIndex;
}
else
{
btnNext.Enabled=false;
}
}

WinForms TabControl

I noticed that by programmatically selecting a Tab in the Tab control selects a control contained in the tab page selected.Is it possible to change this behaviour. I have a control in a tabpage that I do not want to be selected when the its tab page is selected from a button click. I have a simple form with a tab control and two tab pages. When button1 is clicked the tab page 2 is selected but so is the datagridview contained in that tab page.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.GotFocus += DataGridView1_GotFocus;
}
private void DataGridView1_GotFocus(object sender, EventArgs e)
{
//this event is called from button1_click
}
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage2;
}
}
By default when you select a tab (or even when you start a form) the control which is the first in your tab order is automatically focused. I am assuming this is what is happening here.
You can solve this by simply unfocusing the datagridView in question. There are multiple ways to do this. Firstly you can set focus to the control that you wish to be selected instead of the dataGridView. This can be done by:
myControl.Focus = True;
Or alternatively if you want non of the controls to be selected you can set the active control to Null:
ActiveControl = NULL;
Note: ActiveControl is a property which contains the current active control.
As to where this code should be placed. That is totally dependent upon you. You can do it as soon as you change the tab in the button click event. This is what I would prefer.
I am sure there are other kludges as well to acheive the same. Hope this helps.
Here is code to select tab
private void button1_Click(object sender, EventArgs e)
{
// we can select tab by tab name
tabControl1.SelectTab("tabPage2");
tabControl1.SelectedIndex = 1;
tabControl1.TabPages[0].Hide();
tabControl1.TabPages[1].Show();
}

C# .net windows forms dialog next/previous buttons

In a C# .net windows forms application, I have a dialog window with the buttons "next" and "previous", and I want to "move to the next or previous page" according to what buttons the user pressed.
How to achieve this ?
Use Panel or GroupBox to wrap your textboxes,labels and buttons.
Then in your Previous Page and Next Page button, call your groupBox name.
groupBox.Hide();
and
groupBox.Show();
will do the trick.
For example if you are calling your first page:
groupBox1.Show();
groupBoxOtherPage1.Hide();
groupBoxOtherPage2.Hide();
PS : You can do Hide() and Show() to panel also, actually all your form element, but grouping your element in container like Panel or GroupBox will be best.
There are couple of tool that you can use for Next and Previous step wizard. for example DevExpress Wizard Control.
But, if you want to go with the simple windows form application you need to follow some trick. Like take some panels and add it to your form. every panel should be placed on form and all panels location and size should be same and named it panel0, panel1, panel2, etc. and take three buttons btnNext, btnPrevious and btnClose and write some code to navigate that panels
int TotalPanelCount = 3; //last index of panel will be 2
int _index = 0;
public int Index
{
get { return _index; }
set {
if (TotalPanelCount < 3)
_index = value;
else
_index = TotalPanelCount - 1;
ChangeIndex();
}
}
private void btnNext_Click(object sender, EventArgs e)
{
Index++;
}
private void btnPrevious_Click(object sender, EventArgs e)
{
Index--;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void ChangeIndex()
{
string _panelName = "penal" + Index.ToString();
//Hide all visible panel
var panels = (From Control cnt in this.Controls
Where cnt.getType().Name.Equals("Panel") &&
cnt.Name != _panelName && cnt.Visible == true
Select cnt).ToArray();
foreach(Control cnt in panels)
cnt.Visible = false;
//Displaying current panel
Panel pnl = (Panel)this.Controls[_panelName];
pnl.BringToFront();
pnl.Visible = true;
}

Categories