How to add navigation links in c# windows application - c#

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.

Related

How to hide usercontrol and main form c# winforms

how to hide user control and main form. The case is like this,
I have two form and one UserControl. Main form and form registers, and UserControl inside the main form. Inside the UserControl there are two buttons, create and delete. When I press the create button, then UserControl and main form hidden and form register show. I've try, but only hidden UserControl.
Here is MSDN reference to hiding controls Reference
private void btnCreate_Click(object sender, EventArgs e)
{
var registerForm = new Register();//Create instance of register form
this.Hide();//To hide main form
registerForm.Show();//To show register form
userControl.Hide();//To hide user control
//but do not need to hide usercontrol because when form will hide its automatically hide
}

Detect CloseEvent through different forms

PROBLEM SOLVED
SHORT STORY
I want to detect "FormClosing()" event through different forms, ie, when form1 is closed that is instantiated within form2, can form2 detect when user presses exit in form1?
LONG STORY
My team and I are working on a windows form application. Project has two forms: one is the main form page and the other is accessed via this main form. Main form looks like this:
And the second one looks like this:
If you press "Ekle/Sil" buttons within the main form, you are directed to form 2 where you can edit database entries. When you press "Sayfayı Yenile" button in the main form, the content of the text areas are refreshed by re-fetching entries from the database.
My problem is, I want to automatically refresh the main form when the user closes the second form. My research suggests I should use an "FormClosing()" event to detect a closing form. However, I want to detect this from the main form. Instantiating main form in second form's source code doesn't seem to be a reliable solution. Anyone can tell me how to do this?
EDIT
I solved the problem:
1) Created a public method within the main form that refreshes the page.
2) Send "this" property from the main form when creating the second form.
3) Added an "FormClosed()" handler within the second form that invokes this public method.
Still, I'm looking for a better solution.
EDIT 2
Better solution InBetween's answer
Simply use the Form.Closed event of the new child windows form. Everything is handled from the main form:
void EkleSil_Clicked(object sender, EventArgs e) //or whatever method is called when button is clicked
{
var newChildForm = new ChildForm();
newChildForm.Closed += childFormClosed;
newChildForm.Show();
}
void childFormClosed(object sender, EventArgs e)
{
((Form)sender).Closed -=childFormClosed;
updateShownData();
}
You can create a event in the second form and raise it when the form is closing .
Handle the event in the main form and refresh the main form when the event is raised
Another option would be to pass Form1 as an argument to Form2. Then use the Form.Closing event in Form2 and use the Form1 reference to trigger something.
Form1 form1Ref;
public Form2(Form1 mainform)
{
form1Ref = mainform;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.SomeMethod();
}

Usercontrol removal

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.

Updating textbox on mainform with a variable on a child form that is launched from main form

I have a windows form app. The main form has a textbox, and a button to launch another form. When the other form is launched, both forms are on screen (but the launched form is modal). The child form has a textbox and button, and when the button is pressed, I want the textbox on the main form (the parent) to be updated with the value in the textbox on the child form.
How is this functionality achieved?
Ideally you want to keep both forms from being dependent on each other, this could be achieved with interfaces:
public interface IMainView
{
public void UpdateValue(string val);
}
public interface IChildView
{
public void Show(IMainView parent);
}
have your main form implement IMainView and the child implement IChildView, the parent calls child.show(this) and the child calls parent.UpdateValue(blah);
Hope this helps.
If the child form is closed when the button is clicked, you could put a public property which wraps the value of the textbox on the child form. Then the main form can read this property after calling ShowDialog.
If you want this to happen without closing the child form, you can create a function on the main form to change the textbox. Then the child form would call that function.
The best ways to achive this situation are clockWize's and Hans Passants's advices.
But what about that?
Write a property for your textbox at parent form, like this.
public string TextBoxText
{
get { return txtTextBox.Text;}
set { txtTextBox.Text = value;}
}
When you are opening the child form set the owner.
ChildForm f = new ChildForm();
f.Owner = this;
f.Show();
Create an event handler to child forms button click event.
public Button1_Click(object sender; EventArgs e)
{
ParentForm f = (ParentForm)this.Owner;
f.TextBoxText = txtChildTextBox.Text;
}
i didn't compile code; so may have errors :)
}
When a button is pressed to close the launched form, returning you to the main form- the launched form's text box is still in scope.
Closing a form is merely changing the object's state, not disposing of it. So in the button eventhandler that launches the form from the main form, the next line after launching your modal window, it can access the text from the object it launched as the textbox is a child of that form's object. Unless you're launching your modal window in another thread, which I wouldn't figure you are since it's modal, when it is closed, it should go to the next line in the buttons eventhandler that launched it.
your main form may have code something like this right now (haven't done winforms in a while so bear with me if I miss something):
public void Button1_Click(object sender, ClickEventArgs e)
{
SomeFormIWantToLaunch launchForm = new SomeFormIWantToLaunch();
launchForm.ShowDialog(this);
}
You need to just add after launchForm.ShowDialog(this); something like:
this.SomeTextBox.Text = launchForm.ATextBox.Text;

MDI window list not updating child title bar texts

I have a MDI container form, and some child forms that update their title bar texts themselves, independently. After the Text property is changed on the child form, the new title bar text from the child is not updated in the window list menu when the menu is opened. This is the auto-generated window list provided by .NET via the MdiWindowListItem property.
The change only propagates when another event changes the window list physically (opening a new child, closing a child, switching to another child).
Is there a way to force an update of the window list programmatically? I already have some code in place to do menu enabling/disabling at the same time the child's title bar text is changed.
I tried the following with no success:
Update() on the main MenuStrip
Refresh() on the main MenuStrip
Invalidate() on the window MenuStrip
Invalidate() on one of the window list items at runtime
Toggling the Checked state twice on one of the window list items at runtime
There don't seem to be any other remotely viable functions to call on the menu item, its parent ToolStrip, or the parent form that contains the menu system.
The above solution did not work for me. But I followed the link, and found this, which works perfectly:
private void windowMenu_DropDownOpening(object sender, EventArgs e)
{
if (this.ActiveMdiChild != null)
{
Form activeChild = this.ActiveMdiChild;
ActivateMdiChild(null);
ActivateMdiChild(activeChild);
}
}
Thank you!
You need to add a TextChanged event to the child form, with this handler:
private void childForm_TextChanged(object sender, EventArgs e) {
this.ActivateMdiChild( null );
this.ActivateMdiChild( sender as Form );
}
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/a36b89aa-57aa-48b5-87a6-49fbddc9c92d
Instead of activate/deactivate, you can send WM_MDIREFRESHMENU message to the MDI client (not frame) window whenever a window title changed.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644919%28v=VS.85%29.aspx

Categories

Resources