Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello there can anyone help me?
I dont know how to use dock button in my main form of my project

pliz send to my email: [DELETED]@yahoo.co.in


[edit]Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know - OriginalGriff[/edit]
Posted
Updated 6-Jun-11 8:19am
v2
Comments
Sergey Alexandrovich Kryukov 6-Jun-11 14:06pm    
Tag it: "Forms"!
--SA

You always need to use Docking technique if you want to make consistent form Layout. You will also need plenty of Panels.

I'll show this all in code, without Designer, so you would see how it is really done:

C#
using System;
using System.Drawing;
using System.Windows.Forms;

public partial class FormMain : Form {
    
    public FormMain() {
        InitializeComponent();
        Setup();
    } //FormMain

    void Setup() {
        Panel left = new Panel();
        this.Padding = new Padding(10);
        left.BackColor = Color.Yellow;
        left.Dock = DockStyle.Left;
        Panel right = new Panel();
        right.BackColor = Color.Wheat;
        right.Dock = DockStyle.Fill;
        right.Padding = new Padding(4);
        Button button = new Button();
        button.Dock = DockStyle.Bottom;
        button.Text = "&Click it!";
        button.Click += delegate(object sender, EventArgs eventArgs) {
            MessageBox.Show("Clicked!");
        };
        button.BackColor = Color.LightGray;
        ListBox lb = new ListBox();
        lb.Dock = DockStyle.Fill;
        right.Controls.AddRange(new Control[] { lb, button, });
        this.Controls.Add(right);
        this.Controls.Add(left);
    } //Setup

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FormMain());
    } //Main (entry point)

} //class FormMain


I used colors to show what control goes where. IUsing colors on temporary bases is also a good tool during design phase. It is especially useful to design proper padding. Later on, you will remove the colors.

For further design directions, please see my past solutions:
GUI Apperance - C#.Net[^],
Zom Out malfunctions when Screen resolution changes[^],
Code behind class implementation[^].

—SA
 
Share this answer
 
v4
Comments
Ciumac Sergiu 6-Jun-11 16:29pm    
My 5, docking is the best approach.
Sergey Alexandrovich Kryukov 6-Jun-11 17:48pm    
Thank you, Sergiu.
--SA
Depending on what you actually want to do, you may find the Anchor property works better for buttons.

Dock will fill that side of the form with the object: so if you Dock a button to the left, it will fill the left hand side of te form, from the top to the bottom - not a good idea for a button!

If you play with the Anchor[^] settings, you can make the button expand and contract with the form, or fix it relative to the bottom or right. Give it a go: the effects are immediate, so you can play and see what happens in the designer.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jun-11 14:28pm    
Griff, Anchors are always bad my a couple of convincing reasons. Dock + Padding should always be preferred.
Please see my solution, code sample and links.
--SA
OriginalGriff 6-Jun-11 14:43pm    
Not necessarily. If you have a simple form, with say a multiline text box and an OK + Cancel buttons, then anchoring the buttons to the bottom right and the text box to all four sides lets the whole thing expand and contract nicely, all you have to do is set a sensible minimum form size. Easy to do, not extra work needed.
If you want to do something complex, then yes, panels, splitters and docking is the way to go. But it can be overkill for a tiny dialog! :laugh:

[edit]Suddenly, I can't tell my left from my right. It's the Alzheimer's, I tell you...[/edit]
Sergey Alexandrovich Kryukov 6-Jun-11 20:13pm    
All you can do with Anchor you can do with Panels, Dock and Padding -- absolutely. The problems with Anchors: 1) you have to calculate left and right position (or Left and Width) manually or by just looking in Designer; 2) Form with Anchors tends to flicker. There no such problems with Dock -- try and compare.
--SA
OriginalGriff 7-Jun-11 3:27am    
Oh I agree with you, just saying that for simple forms, Anchor is a much simpler solution.
Sergey Alexandrovich Kryukov 7-Jun-11 16:16pm    
Maybe (especially of form is non-resizeable and for ad-hoc purpose) but I would recommend to do everything accurately, which is better with Dock; it is also not more difficult, really.
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900