Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VS2008, ToolStripMenuItem handling 1

Status
Not open for further replies.

GKIL67

Technical User
Dec 1, 2009
44
Hello all,

I have a MenuStrip like this:
FILE ==> CURRENT DIRECTORY ==> textbox

When CURRENT DIRECTORY is selected, it displays the
Application.StartupPath in the textbox, but the textbox
isn't large enough for the text of the path.
There must be a way to re-size the textbox, depending on
the width of the path... BTW an RTFbox isn't given as an
option, AutoSize is not working for width and scrolling is
not provided in the properties!

Any ideas?

Thank you in advance for your time!
 
My first thought is what are you trying to accomplish? The Application.StartupPath is ReadOnly and cannot be changed. You would want to create a Setting or something to hold this value if you plan to allow change.

Is this an attempt to allow the users "choose" and save/read from location? If so, you are better off looking at an OpenFileDialog/SaveFileDialog combination. Then in your menu, you simply use a button to open the necessary dialog box. And when you return from the dialog box, you write that value to a setting or even a textbox on your forms.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 

First I want to get the path displayed on one of the menustrips and then by clicking on it to open the windows
explorer... of course there are a few dozen ways to do it,
I just want it fast and simple - no other windows and Y/N windows and buttons.

The issue here is how to manipulate the TEXTBOX...
So, if we wanted to display a string of i.e. 100 chrs via the menustrip, it is NOT doable? Funny!

Thanks!
 
In my personal experience, and based only on my personal experience, textboxes - and by associations - comboboxes - used in a menu need careful consideration. Most of the time I have found I can avoid them in various other ways. I have found them to be difficult to work with and also not the most intuitive control feature. How will the user "select" the windows path?? I know I would not want to type: "C:\Documents and Settings\rjohnson9\My Documents\OneNote Notebooks\Personal Notebook" every time and ensure I got it right. You will need to ensure you have some good error handling and the like if that is the case.

As a reference/example, open any number of programs that are available today...Windows Explorer, Microsoft Outlook, Word, Excel, Firefox, Opera, Internet Explorer, even Visual Studio... You will find that textboxes and comboboxes have VERY SPECIFIC usages in any of these applications menus. They are selections for other buttons and controls...not truly controls in the traditional sense in and of themselves.

The proper way to do what you are trying to do will involve a lot of coding and thought...You will most likely need to leverage one of the Draw or Paint events and control the creation of the textbox and the menuitem container for that textbox yourself. You will then need to add it to the menu during runtime and assign some EventHandling to it to make it do what you want.

Instead, I challenge you to look for an alternate solution...perhaps a small modal form that you pop up when a button or menuitem is pressed that houses the textbox you want. Here the user can enter the info. You them have control over the textbox at design time and can control the events as necessary.

I would also like to recommend the use of the FolderBrowserDialog control available in Visual Studio. This control allows a user to pick a folder, just like Windows Explorer, and then you can open that folder in Widows Explorer if that is your wish. Also, I recommend you look to AppConfig or the Settings for the project to record the selection by the user and return to that selection the next time they execute this task. This method is very simple, as the FolderBrowserControl is built into the Visual Studio suite and is designed for just this purpose. And your code would look something like:

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If FolderBrowserDialog1.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
            Process.Start("explorer.exe", FolderBrowserDialog1.SelectedPath)
        End If

    End Sub

In short, what you originally requested is doable, but will require a bit of work (OwnerDraw controls and AddHandler for events), but you may be able to accomplish this task in a more effective and intuitive manner.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Hmmmmm...I just looked and if all you REALLY want to do is size the textbox out more....

Open your form in design mode...navigate down the menu to the textbox you want to resize. In your properties window, click the plus next to Size. Then set your width.

NOTE: This will also change the width of that menu level...

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
I hear you, Thanks on your tip on the FolderBrowserDialog control!

Your 2nd reply puts it right... however that's exactly my problem, I don't want to change the width permanently, but only according to the length of the text/path (it could even split in 2 or 3 short lines).

Kindly see the attached picture - excuse the greek!
You see where the mouse arrow is?
Half of the path is missing and the user has to click in
and crawl right to see it.
At the same time the user can double click and open the
explorer.
Of course no serious code in all this and works nicely,
except if could only the textbox could display the full
path...

 
 http://www.4shared.com/photo/ImyraaI7/1_online.html
Well...the width of that textbox can be controlled programtically. If you are using a fixed-width font, you can determine the number of pixels needed to show one character, multiply that but the number of characters in the path and set the width of the textbox to that. Of course, that menu will be that width.

The question then comes down to how and when do you determine what path to show in this textbox?

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
I think I was pretty clear on that... the path is always the Application.StartupPath, i.e.:
Code:
Private Sub ??????ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ??????ToolStripMenuItem.Click
        'Preload the path information, show upon highlight
        ToolStripTextBox1.Text = Application.StartupPath
    End Sub
It can load up at any time, as soon as the application starts.

Now, if you look at the previously attached picture,
my desire is the ToolStripTextBox1.Text to expand in one or more lines so that the path is fully visible.

Thank you for your time, anyway!
 
OK....so we want to set the menuitem width based on the length of the Application.Startup property...

You will need to delete the current textbox off the menu...it will be added dynamically based on the below work.

Using the Load event of the form, you should be able to do something like:

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ApplicationStartupToolStripTextBox As New ToolStripTextBox  ' Creates an in memory-version of the textbox
        ApplicationStartupToolStripTextBox.Text = Application.StartupPath  ' Put the Application.StartupPath as the text of the textbox
        ApplicationStartupToolStripTextBox.Size = New Size(Application.StartupPath.Length * 10, 21)  ' Dynamically build the size of the textbox
        AddHandler ApplicationStartupToolStripTextBox.Click, AddressOf AppStartupTextBox_Click  ' Add the "click" event code so when the user clicks on the textbox, the action is performed
        Me.ConnectionToolStripMenuItem.DropDownItems.Add(ApplicationStartupToolStripTextBox)  ' Add the textbox to the menu dynamically

    End Sub

    ' This code is the section that is run when the user clicks on the textbox - it is called due to the AddHandler AddressOf in the Load event of the form
    Private Sub AppStartupTextBox_Click(ByVal sender As Object, ByVal e As EventArgs)

        Process.Start("explorer.exe", DirectCast(sender, ToolStripTextBox).Text)  ' Open explorer to the path sepcified

    End Sub

Replace "ConnectionToolStripMenuItem" with the name of the menuitem that will contain the textbox.
In the line where the "New Size" is defined, you will need to determine what number belongs instead of the 10...I used ten as an example and it is a bit longer than the actual value you may want. You will have to play with it to find the right number.

Any problems, let me know.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Yes! Your
Code:
ApplicationStartupToolStripTextBox.Size = New Size(Application.StartupPath.Length * 6, 21)
is the star, Thank you!
Of course the same thing can be accomplished with a textbox,
using more simple code, your way however should be the leading!

BTW could you suggest a way to set the minimum length size?
Because when the path is the root, i.e. C:\, then what you
get visible is only a ":"

Warmest Regards!
 
I will make two suggestions and personally, I might implement both.

1. Force the font in the textbox. By default, the textbox is inheriting the font of the menustrip itself. You can set the font to a fixed-width font to ensure that a letter "i" takes up the same space as a letter "m". Insert the red line into your code shown below.

Code:
        ApplicationStartupToolStripTextBox.Size = New Size(Application.StartupPath.Length * 10, 21)
        [COLOR=red]ApplicationStartupToolStripTextBox.Font = New Drawing.Font("Courier New", 9, FontStyle.Regular, GraphicsUnit.Point)[/color]
        AddHandler ApplicationStartupToolStripTextBox.Click, AddressOf AppStartupTextBox_Click

2. The second method is to calculate the size and then ensure it meets some minimum. Replace the ApplicationStartupToolStripTextBox.Size line with the blow code.

Code:
        Dim textboxWidth As Int32 = Application.StartupPath.Length * 6
        If textboxWidth < 45 Then
            textboxWidth = 45
        End If
        Me.ApplicationStartupToolStripTextBox.Size = New Point(textboxWidth, 21)

You may need to adjust the multiplier (the value of 6 you have chosen).

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
That wraps it up very nicely, Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top