CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2010

    C# Generating a textbox and anchoring it at runtime

    C# 4.0
    I have a tabcontrol called tabmain. I have a button which I click which will create a new tabpage with a richtextbox and a small textbox at the bottom. Similiar to a chat client, the richtextbox acts as the output from the server and the textbox is the input (trying to give you an image of what my tabpage looks like).

    Heres my code
    public void AddChannel(string _channel)
                RichTextBox rtxt = new RichTextBox();
                rtxt.Name = "out" + _channel.Remove(0, 1);
                rtxt.Location = new System.Drawing.Point(0, 0);
                rtxt.Size = new System.Drawing.Size(546, 352);
                TextBox txt = new TextBox();
                txt.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
                txt.Name = "in" + _channel.Remove(0, 1);
                txt.Location = new System.Drawing.Point(0, 355);
                txt.Size = new System.Drawing.Size(546, 20);
                TabPage page = new TabPage();
                page.Text = _channel;
                page.Location = new System.Drawing.Point(4,22);
                page.Padding = new System.Windows.Forms.Padding(3);
                page.Size = new System.Drawing.Size(546, 375);
                page.UseVisualStyleBackColor = true;
                page.Name = "page" + _channel.Remove(0, 1);
    Now it works fine without the anchors but with the anchors it just messes everything up. i pretty much made it then copied the designer code. The right anchor goes past the tabcontrol is in aswell as the bottom, and the bottom textbox doesnt even show up. What am I doing wrong?

  2. #2
    Join Date
    Mar 2004
    Prague, Czech Republic, EU

    Re: C# Generating a textbox and anchoring it at runtime

    Try to use Dock instead of Anchor. Use Full for RichTextBox and Bottom for TextBox.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Nov 2010

    Re: C# Generating a textbox and anchoring it at runtime

    Ya no that is not what i want. The richtextbox will fill the whole void and the textbox would overlapp it making you unable to see a line of the richtextbox. It works fine when I anchor it at design time but the generated ones just dont seem to work.

  4. #4
    Join Date
    Nov 2010

    Re: C# Generating a textbox and anchoring it at runtime

    Here I have made a tiny project that adds a tab with richtextbox and textbox at runtime and anchors them, you can see the results of the anchor. You would think, using the exact same code as the designer, it would have the same affects.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.