How to add navigation links in c# windows application
I want to create a form with 2 panels in which the left panel contains links. When a link is clicked, the corre开发者_开发问答sponding 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.
精彩评论