Relevant Tabpage show when click the button in c# - c#

I insert tab-control in my windows application.It has 4 tab-pages.I wanted to show only relevant tab-page when I click the relevant button..my code as follows
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage1.Hide();
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
first every tab-page removed when form load
Here is the code for button click and I coded for 4 buttons...
private void button2_Click(object sender, EventArgs e){
tabControl1.TabPages.Insert(0, tabPage1);
this.tabPage1.Show();
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
I again used ....
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
these code. if another tabpage is opened when I click the button it should be remove and show relevant tabpage.Its working.
My problem is.. if I click the same button again and again same tabpages adding continuously
Can anyone give me a solution for it..

I founded a one way...In button click I modify like this.
private void button1_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
if (tabControl1.TabPages.Count <= 1)
{
tabControl1.TabPages.Insert(0, tabPage1);
this.tabPage1.Show();
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
}
first code is tabControl1.TabPages.Remove(tabPage1).Then if tab-page is open It removed.If statement responsible for when button click relevant tab page is opened.then always show only one tab-page for button click moment

Related

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();
}

Change tab on button click

Does anybody know how to move to a different tab when you click a button? Is there a general statement?
private void button3_Click(object sender, EventArgs e)
{
}
I know that something needs to be entered into the method above but I really don't know where to start.
tabControl1.SelectedTab = DesiredTabPage;
DesiredTabPage is the name of the tabPage you want to move to it

how to collapse panel by clicking on button without clicking on spliter

As the picture has been attached below,I have 2 panel from splitcontainercontrol .Panel 1 contains 1 button and panel 2.
I want to ask everybody how to write code for this button(panel1) to collapse panel 2 without changing properties and clicking on spliter when runs app .
You can simply collapse the panel with the following code :
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel2Collapsed = true;
}
If you do not want to change the propery of the panel, then you can hide the panel. Try this :
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Hide();
}
And you can show the panel with this : splitContainer1.Panel2.Show();

How remove unnecessary lines

this is my code:
public RegForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label10_Click(object sender, EventArgs e)
{
}
private void label28_Click(object sender, EventArgs e)
{
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
private void label29_Click(object sender, EventArgs e)
{
}
private void label30_Click(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
I would like to remove all the unnecessary lines of code for the labels that just came up out of nowhere for no reason and do absolutely nothing. But when I delete them - it's all errors. I'm a beginner, so please go easy on me. Thank you in advance.
To remove those empty event handlers:
Switch to the designer view,
Select each label, one at a time,
Look at the properties window (press F4 if it's not visible), and check out the events view
Find the click event, right-click and choose "reset".
Be careful when you're selecting your different controls in the designer.
If you double-click by accident, you end up creating an empty event handler for whatever the default event is for a particular control. In the case of a Label, it happens to be the Click event.
This are auto generated events which are created when you double click
on control or some other action is performed on the controls in the
design view.
Remove the lines from code behind and also remove them from *.designer.cs file.
IF you build the application after the removing of the lines and check errors: You can click on every error and it will leads you to the place which should be deleted !
If those are unnecessary then just go ahead and delete them from your code behind *.cs file. You will also have to delete the event registration from the corresponding *.designer.cs file and because of the same thing you are getting error (that you are not removing the corresponding event registration from designer file).
You can as well do the same from Designer window by select the control -> press F4 -> go to the event pane by clicking the lighting bolt icon -> remove the event registration.

How can I navigate and pass data between Pages?

I'm a bit of a beginner with this so i'll try and keep it simple.
I have a a xaml page with a button click event linking it to another xaml page. What I'm trying to do is on the click event take two strings and pass them to a text box on the second page. Can you please show me a simple code example of how to do this?
On the button click event of the first page you do something like the following
private void button1_Click(object sender, RoutedEventArgs e)
{
string urlWIthData = string.Format("/Page2.xaml?name={0}", txtName.Text);
this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
}
On the desintation page, you do the following:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Text = this.NavigationContext.QueryString["name"].ToString();
}

Categories