[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)
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..
Re: [2008] Setting Anchor property on control created at runtime
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?
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
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..
Re: [2008] Setting Anchor property on control created at runtime
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?
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.
Re: [2008] Setting Anchor property on control created at runtime
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
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
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.
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:
_shortfallsbox.Name = "Shortfallsbox"
_shortfallsbox.Location = New Point(17, 335)
_shortfallsbox.Size = New Size(159, 30)
_shortfallsbox.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom
_shortfallsbox.StateCommon.Border.Rounding = 4
_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.
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:
Public Class testtabpage : Inherits Windows.Forms.TabPage
Dim rtb As New RichTextBox
Public Sub New()
rtb.Name = "rtb1"
rtb.Size = New Size(256, 60)
rtb.Location = New Point(51, 116)
rtb.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
Me.Controls.Add(rtb)
End Sub
using it like this in my form code:
vb Code:
Dim page As New testtabpage
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:
Friend WithEvents shortfallsbox As New RichTextBox
'In a button's click event handler code..
Dim newpage As New customertabpage(newcustomernamebox.Text)
'add my version of the tabpage class to a tabcontrol
Form1.CustomerTabs.TabPages.Add(newpage)
'set anchoring on the richtextbox control
shortfallsbox.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
'set various other standard properties on the richtextbox control
shortfallsbox.Location = New Point(17, 335)
shortfallsbox.Name = "Shortfallsbox"
shortfallsbox.Size = New Size(Form1.CustomerTabs.Width - 49, Form1.CustomerTabs.Bottom - 438)
shortfallsbox.TabIndex = 8
'add the richtextbox control to the newly created tab
newpage.Controls.Add(shortfallsbox)
Last edited by chris128; Jun 26th, 2008 at 06:45 PM.
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.
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