VB: Auto resizing textbox residing inside toolstrip

1,511 views
Skip to first unread message

Faraz Azhar

unread,
Oct 7, 2009, 8:07:59 AM10/7/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hello

Im using VB 2008 express edition. I try to create a toolstrip on my
form and add few buttons and a text box on it.

I want to add a feature that resizes the width of the textbox
according to the width of the form window. If window is maximized, the
textbox's width should accordingly increase.

How to achieve this? I tried setting the AutoSize properties to True
for both ToolStrip and Textbox. Also but Stretch=True for Toolstrip.
Nothing works.

Need help.
Faraz Azhar

Mike Fry

unread,
Oct 8, 2009, 5:09:38 AM10/8/09
to dotnetde...@googlegroups.com

Look at the Anchor property. You can 'fix' the position in relation to
the parent container.

--
Best regards,
Mike Fry
Johannesburg.

Faraz Azhar

unread,
Oct 8, 2009, 10:16:24 AM10/8/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
No sir, no such Anchor property available. Im talking about textboxes
in toolstrip control. The ordinary textboxes have the Anchor property,
but not the ones that are available in toolstrip.

Peter Smith

unread,
Oct 8, 2009, 12:07:17 PM10/8/09
to dotnetde...@googlegroups.com
12:02 pm: ToolStrip.Never heard of it. Let's see how long it takes to research the answer

Lessee, there's an anchor for the SWF.ToolStrip:
        http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstrip.anchor.aspx

And then I suppose you're adding a ToolStripTextBox, not just a TextBox....
        http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstriptextbox.aspx

Gee, the docs say:

ToolStripTextBox is the TextBox optimized for hosting in a ToolStrip. A subset of the hosted control's properties and events are exposed at the ToolStripTextBox level, but the underlying TextBox control is fully accessible through the TextBox property.

So, maybe there? Well, lets go check the properties....

Public property Anchor Gets or sets the edges of the container to which a ToolStripItem is bound and determines how a ToolStripItem is resized with its parent. (Inherited from ToolStripItem.)

Apparently, it does in fact exist.
         http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.anchor.aspx

Fancy that. The .NET Framework Class Library uses inheritance. Who would have thunk it?

12:08 pm. Sending message. 1/10th of an hour, that'll be $5 please.

-- Peter Smith






Andrew Badera

unread,
Oct 8, 2009, 12:59:20 PM10/8/09
to dotnetde...@googlegroups.com
Peter, at $50/hour, you're a bargain.

And way undervalued.

∞ Andy Badera
+1 518-641-1280
∞ This email is: [ ] bloggable [x] ask first [ ] private
∞ Google me: http://www.google.com/search?q=andrew%20badera

Peter Smith

unread,
Oct 8, 2009, 1:18:44 PM10/8/09
to dotnetde...@googlegroups.com
On Thu, Oct 8, 2009 at 12:59 PM, Andrew Badera <and...@badera.us> wrote:
Peter, at $50/hour, you're a bargain.

And way undervalued.

 
Well, it WAS during lunch. :)
 

Faraz Azhar

unread,
Oct 8, 2009, 11:58:26 PM10/8/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
:-) guys i really appreciate for you helping me out..... for free :P
but im still stuck.. cant figure out this thing..

Faraz Azhar

unread,
Oct 8, 2009, 12:37:53 PM10/8/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hmm.. thanks for doing this research for me.

Lets me start a new project from fresh. I create a new project in VB
2008 Express. Add a form. And place Toolstrip control on it. Then I
add a button on it, a text box and then another button... ie. 3
controls are there on the Toolstrip.

I also set the Toolstrip's Dock to Top, so it stretches from left to
right at the top. Now I add the following in my form's Load event:

ToolStripTextBox1.TextBox.Anchor = AnchorStyles.Right

Ok, now i run the project... still the same ! Nothing changed. What
now?

Processor Devil

unread,
Oct 9, 2009, 4:45:13 AM10/9/09
to dotnetde...@googlegroups.com
If you use just AnchorStyles.Right, the "TextBox" will move more to right if user resizes the window, using left+right will resize the textbox as well

2009/10/8 Faraz Azhar <itzf...@gmail.com>

Arsalan Tamiz

unread,
Oct 9, 2009, 5:13:47 AM10/9/09
to dotnetde...@googlegroups.com
I don't think this will help.

The simple option is that, each time the window is resized, manually calculate the width of TextBox.

Regards,
Arsalan Tamiz

Mike Fry

unread,
Oct 9, 2009, 5:37:58 AM10/9/09
to dotnetde...@googlegroups.com
Arsalan Tamiz wrote:
> I don't think this will help.
>
> The simple option is that, each time the window is
> resized, manually calculate the width of TextBox.

The wheel has already been invented!

Arsalan Tamiz

unread,
Oct 9, 2009, 5:44:50 AM10/9/09
to dotnetde...@googlegroups.com
I will be very happy if that wheel does the trick :)

Processor Devil

unread,
Oct 9, 2009, 6:21:02 AM10/9/09
to dotnetde...@googlegroups.com
Well, I am using anchorns left+right, left+top+right, bottom+right etc for ages and it works fine :)

2009/10/9 Arsalan Tamiz <sall...@gmail.com>

Arsalan Tamiz

unread,
Oct 9, 2009, 6:31:45 AM10/9/09
to dotnetde...@googlegroups.com
Yes "Anchor" works perfectly well for a normal control but we are talking about the controls inside the ToolStrip.

Cerebrus

unread,
Oct 9, 2009, 10:51:09 AM10/9/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Welcome back, Arsalan. Haven't seen you in a while! S'all good, I
hope. :-)

Arsalan Tamiz

unread,
Oct 10, 2009, 12:12:38 AM10/10/09
to dotnetde...@googlegroups.com
:-) I am here, just NOT very active.

santhosh vs

unread,
Oct 10, 2009, 2:46:40 AM10/10/09
to dotnetde...@googlegroups.com
:)

Faraz Azhar

unread,
Oct 9, 2009, 2:41:13 PM10/9/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Arsalaan is right. ToolStripTextBox doesnt respond to the Anchor
property at all. Only the TextBox obeys Anchor setting. I even tried
to set the width manually.. nothing seems to be working. Didnt realise
it would be so darn hard to achieve such a petty thing :S :S :S

Faraz Azhar

unread,
Oct 10, 2009, 3:14:46 AM10/10/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Well i got this article from MSDN... im working on it now.. hopefully
it will work:

http://msdn.microsoft.com/en-us/library/ms404304.aspx

clownmachine

unread,
Nov 18, 2009, 7:27:12 AM11/18/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi, the anchor and/or drop is ignored, just do a quick-fix something
like this (i keep most of my controls static and only resize the one
of them) --- oh, if you want to dock your toolstrip to your toolstrip
container, follow the link "Edit Items" at the bottom of the
Properties window and set the parameter in the resulting window.
Setting the parameter in the properties window does not stick.

Private Sub Form1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Resize

'add all of your static items together (I'd dim this globally though):
'example

dim tool_strip_static_width as short = ToolStripLabel1.Width +
ToolStripSeparator1.Width + ToolStripLabel2.Width ... + Buffer ' you
need some buffer, so the last item wont slip out of view try 20

dim tool_strip_variable_width as short = Me.Width -
tool_strip_static_width

ToolStripComboBox1.Size = New System.Drawing.Size
(tool_strip_variable_width, 25)

End Sub

I know ... kludge. But hey: it works.
Reply all
Reply to author
Forward
0 new messages