Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# using dockstyle and set margin

I'm trying to insert a couple of objects in a new form that I programmatically create; basically I want a Button on the bottom and a RichTextBox filling all the remaining space. I set the first as Dock = DockStyle.Bottom and the latter as Dock = DockStyle.Fill and it works.

Now I'm trying to insert a spacing between elements, so I added a padding in the form and a margin in the button. The first works correctly, but margin doesn't, so no space between RichTextBox and Button.

Here is the code and the output. Am I missing something?

// Parent Form
SMSForm.Padding = new Padding(5);

// TextBox
RichTextBox SMStext = new RichTextBox();
SMSForm.Controls.Add(SMStext);
SMStext.Dock = DockStyle.Fill;

// Button
Button SMSsend = new Button();
SMSsend.Text = "Send SMS to ";
SMSForm.Controls.Add(SMSsend);
SMSsend.Margin = new Padding(10);
SMSsend.Dock = DockStyle.Bottom;

enter image description here

like image 984
Naigel Avatar asked Jul 22 '14 09:07

Naigel


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

Setting the Margin property on a docked control has no effect on the distance of the control from the the edges of its container.

Read MSDN. Use Table layout panel

Like this

           RichTextBox SMStext = new RichTextBox();

            TableLayoutPanel pnl1 = new TableLayoutPanel();
            pnl1.RowStyles.Clear();
            pnl1.ColumnStyles.Clear();
            pnl1.RowCount += 2;
            pnl1.ColumnCount += 1;
            pnl1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0F));
            pnl1.RowStyles.Add(new RowStyle(SizeType.Percent,80.0F));
            pnl1.RowStyles.Add(new RowStyle(SizeType.Percent,20.0F));
            pnl1.Controls.Add(SMStext,0,0);
            SMStext.Dock = DockStyle.Fill;
            Button SMSsend = new Button();
            SMSsend.Text = "Send SMS to ";
            this.Controls.Add(pnl1);
            pnl1.Dock = DockStyle.Fill;
            pnl1.Controls.Add(SMSsend,0,1);
            SMSsend.Dock = DockStyle.Fill;
           SMSsend.Margin = new Padding(10);
like image 75
Sathish Avatar answered Sep 21 '22 14:09

Sathish


First undock the RTB. Then set the positions of RTB and button as you want(By specifying bounds programmatically).

Then set the anchor property of RTB to all side. i.e. Top Left Bottom Right

And then set button anchor to Left Right Bottom.

Also, if you want more control of layout, you can use flow layout panel or table layout panel control.

like image 33
Ashish Charan Avatar answered Sep 24 '22 14:09

Ashish Charan