I have some problem about window form in VS C# . I make a tabcontrol. it 's compound with tabpage1, tabpage2
. All tabpage will add same panel. when I click for switch tab it should show the panel. but It's not work. It' will show first tabpage but it's not show in other tabpage when i switched tab.
public partial class Form2 : Form
{
public Form2(.....){
..........
InitializeComponent();
...........
panelButton.Controls.Add(btnArr1[i]);
tabControl1.TabPages[0].Controls.Add(panelButton); //It's work
tabControl1.TabPages[1].Controls.Add(panelButton); //it's not show panel
}
}
In another way I try to make event when i click another tabpage. Like this code.
private void tabPage1_Click_1(object sender, EventArgs e)
{
tabControl1.TabPages[0].Controls.Add(panelButton); //It 's work
}
and
private void tabPage2_Click_2(object sender, EventArgs e)
{
tabControl1.TabPages[1].Controls.Add(panelButton); //It is not show panel.
}
What Wrong?
You are trying to add the single object (panelButton) in the different tabs. You should create two panelButtons for each tab, or you should to remove panelButton from the current tab, before adding it in the selected.
you can't add controls with same referece to two different tabPages
the solution is to make a new instance of panelButton and add it to second tab,
Do Not forget that all contorls inside your panelButton must be init too
Related
I have got a Windows forms application with a main application form and two controls I added to my main form.
The first control has got a login button. When that button is pressed, I want to hide control1 and show control2, which is currently hidden.
Hiding control1 works by using this.Hide() in the onClick event of the login button, but I have so far been unable to find a way to reference control2 and call .Show()
How can I show control2 from within the onClick event of the login button?
EDIT:
Hope the below information helps.
Controls:
LoginMenu.cs - Has all of the control's code in it. - Control name in MainForm.cs is loginMenu1
TicketSearch.cs - Has all of the control's code in it. - control name in MainForm.cs is ticketSearch1
After adding all controls, I rebuilt the project and dragged them into the main form from the tools menu.
The code for the login button is in LoginMenu.cs
public partial class LoginMenu : UserControl
{
public LoginMenu()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
*CODE HERE*
}
void ButtonLoginClick(object sender, EventArgs e)
{
*CODE HERE*
try
{
HttpWebResponse response =
(HttpWebResponse)loginTest.GetResponse();
}
catch (System.Exception ex)
{
if (ex.Message.Contains("401") == true)
{
*CODE HERE*
}
else if (ex.Message.Contains("403") == true)
{
*CODE HERE*
//Hide login control. This works
this.Hide();
//This is where I want to show the control ticketSearch1
}
}
}
}
Thanks to those who tried to help. I learnt something new today.
For some reason, I don't quite understand I could not access the controls in MainForm.cs from LoginMenu.cs even though they are in the same namespace and the control 'Modifiers' property is set to 'Public'.
I resolved this by adding the following method call to my login button onClick action to reference the control I wanted Form.ActiveForm.Controls.Find("ticketSearch1", true)[0].Show();
But it should work.
Look at the properties of Control2 and pick the name (or set a name) under the line "(Name)".
then you should:
private void button_Click(object sender, EventArgs e)
{
firstcontrol.Hide();
secondcontrol.Show();
}
it should work.
Maybe you failed with the placing of the control. So look what happens if you start your programm with the first control hidden and the second one shown. And try to hide the second control and show the first.
There must be the problem.
Did you place your Control via "Toolbox"?
Or do you call your Control via code?
EDIT:
Referring to your inserted Code, it seems, that you call this code in your Usercontrol.
So you need to set the second Usercontrol to "Public". When you click on it and go to the Properties, you choose "Mofifiers" and set it to public.
Now you should look for the (Name) Attribute in his properties.
Then just add:
NameOfYourSecondControl.Show();
This will fix your problem.
I'm working on a WindowsForm which has a TabControl with two TabPages. And I am trying to clear datagridview selection when I click on the 2nd tab, where the datagridview is. Howerver, if I first click on the 2nd TabPage it does not work. But, if I click back on 1st TabPage and then click back to 2nd TabPage again it works.
I tried to solve it by puttting the ClearSelection() on the form load but it didn't work.
Please help me out.
this is my code:
private void tcGrupoClientes_Selected(object sender, TabControlEventArgs e)
{
if (tcGrupoClientes.SelectedTab == tpConsultaGrupoClientes)
{
Logica.GrupoClientes objGrupoClientes = new Logica.GrupoClientes();
dgvTodosLosGrupos.DataSource = objGrupoClientes.ConsultaMasiva();
dgvTodosLosGrupos.ClearSelection();
}
}
Thanks in advance.
I solved the problem using "SelectedIndexChanged" event instead of "Selected" event
I am using Windows Forms application using C#.I would like to know, how can I disable the TabPages while it is not open or selected and it can be open by using another button.
Specifically, I want to disable the TabControl of TabPages and TabPages can be controlled by button click using C#.
In your code you have to remove the tab and re-add it when you want it
tabControl1.TabPages.Remove(tabPage1); // Hide the tab page
tabControl1.TabPages.Insert(0, tabPage1);// Show the tab page (insert it to the correct position)
For example:
Hiding a TabPage
private void ButtonHide_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
}
Showing a TabPage
private void ButtonShow_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Add(tabPage1);
}
My problem is the following. I have a form which when it loads up invokes a user control onto it. On the user control I have buttons. With the buttons I would like to make the user control disappear with all the buttons and picture boxes on it and make a new user control appear. The appear part is all right but I can't make the user control disappear with all the objects on it.
Can you help me with this one?
So the dispose method closes the usercontrol well, but then I can't make the new user control appear. Code is like this:
akusztikusUserControl auc = new akusztikusUserControl();
public menuUserControl()
{
InitializeComponent();
}
private void akusztikus_btn_Click(object sender, EventArgs e)
{
this.Dispose();
this.Controls.Add(auc);
}
}
Have you considered using Panel or a TableLayoutPanel control ? In both cases you can remove controls through the .controls.remove option
You can then just add the user control to the panel / tablelayoutpanel whenever you need to use it again.
I want to create a form with 2 panels in which the left panel contains links. When a link is clicked, the corresponding form should open up in the right area and should refresh when another link is clicked and should show that form. I want to do this in c# windows application. How do I do it?
For your problems, you can use Panel controls. and User Controls. First create UserControl, Then you can add it to the panel container. UserControls help to re usability, and will be easier to re use it.
Steps to do
Create a user control and design it accoriding to your need
Put a panel Control
Load the Usercontrol object in Panel and display it
For Eg .
U put one Link Label or Image or Button in your Left Side of Form, and in right side the content Panel .
when u clicked LinkLabel do the following
Protected void LinkLabel_Click()
{
UserControl1 UserObj =new UserControl1(); // UserControl which u want to display
panel1.controls.Clear();
Panel1.Controls.Add(userobj); //Adding the control to Panel Container.
}
As #AVD suggests for link you should use LinkLabel but in order to open forms in 'right' or anyother specified panel, you have to set the Parent handle of Forms to the handle of containing Panel.
So lets say you have two panels, splitContainer1.LeftPanel and splitContainer1.RightPanel. In left Panel you have LinkLabel with LinkClicked event. Now in order to open a Form in splitContainer1.RightPanel when LinkLabel is clicked, instantiate an object of Form, call the Win API method SetParent() to set the parent handle and then call the Form.Show() method to open it in splitContainer1.RightPanel
//Declare a WinAPI method
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
//Inside LinkClicked event
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form1 f = new Form1();
SetParent(f.Handle, splitContainer1.Panel2.Handle);
f.Show();
}
Edit: A workaround to close any existing form in panel before opening a new
Not the best but easiest way to close existing form:
Form currentForm = null;
private void CloseCurrentForm()
{
if(currentForm != null)
currentForm.Close();
}
and in every LinkClicked event call this method before opening a new Form like this, don't forget to set the currentForm:
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
CloseCurrentForm();
Form2 f = new Form2();
SetParent(f.Handle, splitContainer1.Panel2.Handle);
currentForm = f;
f.Show();
}
You should use LinkLabel control and create MDI to open/show child forms.
Have you tried to use the Custom Properties used in c# to open new forms, try it here. Use the Link button.
I think you searching for http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel.aspx . LinkLabel control. Handle its LinkClicked event and do what you want.