Results 1 to 23 of 23

Thread: [RESOLVED] [2008] Setting Anchor property on control created at runtime

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [RESOLVED] [2008] Setting Anchor property on control created at runtime

    OK so I am using a similar method to jmcilhinney's tabbed webbroser control to programmatically add tabpages with controls in them to a tabcontrol. All is going fine apart from the fact that whenever I set the Anchor property on a control, it makes the control extend past the bounds of the tabcontrol for some reason.

    Here's the code I am using to add a label and a textbox to a new tabpage:

    Code:
    Public Class customertabpage
        Inherits System.Windows.Forms.TabPage
    
        Private _Namelbl As New KryptonLabel
        Private _NameBox As New KryptonTextBox
        Private testlbl As New Label
        Private testbox As New TextBox
    
            Public Sub New()
            customercount += 1
            testlbl.Location = New Point(0, 10)
            testlbl.Text = "Test Label"
            testlbl.Size = New Size(45, 20)
    
            testbox.Location = New Point(96, 7)
            testbox.Name = "testbox" & customercount
            testbox.Size = New Size(Form1.CustomerTabs.Width - 114, 20)
            testbox.Anchor = AnchorStyles.Left + AnchorStyles.Right + AnchorStyles.Top
    
    
            Me.Controls.Add(testlbl)
            Me.Controls.Add(testbox)
    End Sub
    End Class
    The label and the text box get added to the correct locations, but the text box stretches out beyond the bounds of the tabcontrol and I cant figure out why. Do you have to tell it somehow which control it needs to 'anchor' itself to? (i.e the tabcontrol ) or anything like that?
    If I take the Anchor line of code out then it works fine (but obviously doesnt resize automatically with the form, which is what I am trying to do)


    Cheers
    Chris
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Setting Anchor property on control created at runtime

    It should be:
    vb.net Code:
    1. testbox.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    You combine flag values with a bitwise Or operator.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Setting Anchor property on control created at runtime

    Tried that and the same thing still happens
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Setting Anchor property on control created at runtime

    Hmmm... I should have read your post more carefully. I see what you're saying now. Not sure what's up with that.

    Maybe you could try designing a UserControl and then just adding an instance of that to your TabPage.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Setting Anchor property on control created at runtime

    Havent got a clue how I'd do that but I will read up on it and give that a go
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Setting Anchor property on control created at runtime

    ok so I've done a bit of reading on user controls but cant seem to get this to work at all.
    I'm creating a new user control project and as I need it to inherit from the TabPage class I tried just changing the bit that says inherit from UserControl to inherit from TabPage but now when I try and run it I just get errors saying that the UserControl base class isnt present etc etc

    I dont understand how you are supposed to use inheritance in a user control as changing it to anything other than UserControl causes all sorts of errors..
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] Setting Anchor property on control created at runtime

    Quote Originally Posted by chris128
    Code:
            testbox.Size = New Size(Form1.CustomerTabs.Width - 114, 20)
    Chris,

    I find it a bit odd that in this custom tabpage class, you make a direct reference to Form1. This should never happen in a class (how would it be reusable in this case???)

    The second thing, is you are setting an anchor of left, top, and right. Which means you don't actually need to set a size at all, considering setting those anchor properties cause automatic sizing to occur anyway. As long as its position (x/y) are where you want it, then size is not important, as it will size to the container control (tabpage).

    So if you just simply comment out the above line, does it now work?

  8. #8

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Setting Anchor property on control created at runtime

    I'm referencing the width of the tabcontrol as this class will only ever be used as a tabpage in the tabcontrol... so I didnt see the harm in that (?) as its still reusable for me (i.e to keep creating new tabs within that tabcontrol)

    I'll try what you said about the size and get back to you
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Setting Anchor property on control created at runtime

    OK that works... kind of. I dont want the text box to be exactly as wide as the tabcontrol, hence my sizing code. Anchoring doesnt just make the control fill whatever container it is in (as im sure you know), it just makes it resize in proportion to its container. Normally when I add a textbox that I want to auto resize when the form is resized, I could place it say 200 pixels away from the edge of the form and then when the form is resized, this distance stays the same, so if my form doubled in size then my text box would still be 200 pixels away from the edge. I hope you understand what I mean, and its for this reason that I need to have some way of setting my text box to a specific size as well as being able to anchor it..
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] Setting Anchor property on control created at runtime

    and what if you wanted to resuse this class on Form2, because Form2 also has a tabpage, and you want the same type of custom tabpage on that??

    In any event, I don't think you need the line at all, I am just saying its bad design.

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] Setting Anchor property on control created at runtime

    Quote Originally Posted by chris128
    OK that works... kind of. I dont want the text box to be exactly as wide as the tabcontrol, hence my sizing code. Anchoring doesnt just make the control fill whatever container it is in (as im sure you know), it just makes it resize in proportion to its container. Normally when I add a textbox that I want to auto resize when the form is resized, I could place it say 200 pixels away from the edge of the form and then when the form is resized, this distance stays the same, so if my form doubled in size then my text box would still be 200 pixels away from the edge. I hope you understand what I mean, and its for this reason that I need to have some way of setting my text box to a specific size as well as being able to anchor it..
    I am not sure what you mean by this. I took your exact code (minus the one line I said to leave out) and added a custom tab page to a tabcontrol.

    Here are 2 screenshots of the form, once in its initial state, and once resized to be wider. It looks good to me.

    Are you not getting this result? Or are you getting this result, but its still not what you want?
    Attached Images Attached Images  

  12. #12

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Setting Anchor property on control created at runtime

    I just tried changing the size to something really low like (30,20) instead of (600,20) and now the text box fits on the screen and anchors correctly. I altered it to (51,20) and now its exactly as I need it to be...
    I dont understand what size measurements this is using though... If I click ona text box that is in a pre-existing tab in the same tabcontrol I can see its width is 600ish so how come I have to set newly created text boxes on new tabs to a width of 50ish :S
    Last edited by chris128; Jun 26th, 2008 at 12:49 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  13. #13

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Setting Anchor property on control created at runtime

    Quote Originally Posted by kleinma
    and what if you wanted to resuse this class on Form2, because Form2 also has a tabpage, and you want the same type of custom tabpage on that??

    In any event, I don't think you need the line at all, I am just saying its bad design.
    Yeah I appreciate what you are saying and I agree that ordinarilly it would be a really bad idea but in this case I know I am not going to have nay other forms so its not going to be a problem. Thanks for the advice though
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] Setting Anchor property on control created at runtime

    Because when you anchor to both left and right, the control dynamically resizes

  15. #15

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Setting Anchor property on control created at runtime

    I know it dynamically resizes when the container resizes but surely when the control is initially being created it would use the same size measurement system as the rest of the form :S
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] [2008] Setting Anchor property on control created at runtime

    It does use the same units of measurement. If you can show me some example or screenshot where you see this is not the case, I will take a look at it for you.

  17. #17

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] [2008] Setting Anchor property on control created at runtime

    OK well its happening again now and your solution of taking the size line out doesnt fix it this time and neither does my solution of working out a decent size to set.

    This time its with a richtextbox control and Iam anchoring it to all 4 sides, like so:

    vb Code:
    1. _shortfallsbox.Name = "Shortfallsbox"
    2.         _shortfallsbox.Location = New Point(17, 335)
    3.         _shortfallsbox.Size = New Size(159, 30)
    4.         _shortfallsbox.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom
    5.         _shortfallsbox.StateCommon.Border.Rounding = 4
    6.         _shortfallsbox.TabIndex = 8

    and this is what it turns out like (note I'm using a 3rd party rich text box control but I can confirm the exact same thing happens even if I use the standard control)

    This is what it looks like if I create a rich text box at design time and set its size and anchor it to Top,Left,Right and Bottom:


    and this is what happens if I use the code posted above:



    and thiiisss is what happens when I try what you suggested and just comment out the line of code that set the size:


    As you can see, it is not wide enough as well as being too tall when I remove the size line.

    Any ideas?

    Thanks
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  18. #18

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] [2008] Setting Anchor property on control created at runtime

    Hmm I just tried creating a fresh new project and it seems to work fine in there so it must be something to do with the way things are setup in this one :S

    EDIT: after further testing, I've found that even in the fresh new project this problem occurs if I use a class to actually do the creating of the controls...

    So basically this code doesnt work if its in a class that I created but does work if I just use the standard tabpage class in a normal form sub:

    vb Code:
    1. Public Class testtabpage : Inherits Windows.Forms.TabPage
    2.  
    3.         Dim rtb As New RichTextBox
    4.  
    5. Public Sub New()
    6.         rtb.Name = "rtb1"
    7.         rtb.Size = New Size(256, 60)
    8.         rtb.Location = New Point(51, 116)
    9.         rtb.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    10.  
    11.         Me.Controls.Add(rtb)
    12. End Sub

    using it like this in my form code:

    vb Code:
    1. Dim page As New testtabpage
    2. TabControl1.TabPages.Add(page)

    Does anyone know of any reason for this? I thought seen as I ws inheriting from the tabpage class then there should be no difference between my class and the standard tabpage class other than the extra controls that get drawn on...

    EDIT EDIT:
    It looks like its not the fact that its my own class rather than the standard tabpage class that is causing the problem, it seems to just be creating/adding the richtextbox in a class like mine that causes the problem. The temporary solution that I am using at the moment is to instantiate my own tabpage class from a form and let the class add several controls to it then once the newly created tab page has been added to the tabpagecollection I just create the richtextbox from my form. it aint pretty but it works for now...

    Here's what m currenly using as a workaround:
    vb Code:
    1. Friend WithEvents shortfallsbox As New RichTextBox
    2.  
    3. 'In a button's click event handler code..
    4.  
    5.        Dim newpage As New customertabpage(newcustomernamebox.Text)
    6.        
    7. 'add my version of the tabpage class to a tabcontrol
    8.         Form1.CustomerTabs.TabPages.Add(newpage)
    9.  
    10. 'set anchoring on the richtextbox control
    11.         shortfallsbox.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
    12.                             Or System.Windows.Forms.AnchorStyles.Left) _
    13.                             Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    14. 'set various other standard properties on the richtextbox control      
    15. shortfallsbox.Location = New Point(17, 335)
    16.         shortfallsbox.Name = "Shortfallsbox"
    17.         shortfallsbox.Size = New Size(Form1.CustomerTabs.Width - 49, Form1.CustomerTabs.Bottom - 438)
    18.         shortfallsbox.TabIndex = 8
    19. 'add the richtextbox control to the newly created tab
    20.         newpage.Controls.Add(shortfallsbox)
    Last edited by chris128; Jun 26th, 2008 at 06:45 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  19. #19
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] [2008] Setting Anchor property on control created at runtime

    Could you maybe post a graphic (even if its just a mock up) of your desired UI look?

    I don't really see any problem here, but maybe once I see your goal I will have a better understanding.

  20. #20

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] [2008] Setting Anchor property on control created at runtime

    I'll post a better screenshot when I get home tonight but you can see what I mean by trying it yourself - all you need to do is create a tabcontrol then create your own class that inherits from the tabpage class and in this class write out the code that adds a richtextbox to it , sets the location and anchors it to all 4 sides. Then when you use this class you will see that the richtextbox on the new tabpage that gets created from your class will stretch out beyond the bounds of the tabcontrol. However, if you use the exact same code to create the richtextbox and anchor it etc but run it straight from a button click event on a form instead of running it from a class then it will work fine.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  21. #21

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] [2008] Setting Anchor property on control created at runtime

    alright here's a video that will hopefully make things a bit clearer
    It's 7 MB ish so might take a little while to load (best thing to do is probably just right click the link and go to "Save Target As..." because at least then you can see the progress)
    CLICK HERE FOR VIDEO http://homepage.ntlworld.com/nicola....horproblem.mov
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  22. #22

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] [2008] Setting Anchor property on control created at runtime

    bump
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  23. #23
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    211

    Re: [RESOLVED] [2008] Setting Anchor property on control created at runtime

    For my dynamic controls when setting the anchor values I just set up some constants as makes code more succinct (IMO)

    Const constAnchor_T_B_L_R As Integer = 15
    Const constAnchor_B_L_R As Integer = 14
    Const constAnchor_B_R As Integer = 10


    dgvFit_Curves.Anchor = constAnchor_B_L_R

    Kristian

Posting Permissions

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



Click Here to Expand Forum to Full Width