PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2019 [RESOLVED] A trick to not have a solitary TextBox focused on form load.-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] A trick to not have a solitary TextBox focused on form load.

  1. #1

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    896

    Resolved [RESOLVED] A trick to not have a solitary TextBox focused on form load.

    I have no question, I only want to share a trick I found. I thought maybe someone else could benefit.
    I wanted a non-modal MessageBox but that's impossible. I took the common advice to create a simple form. I didn't want the message to wrap so I couldn't use a label so used a read only TextBox. But my problem was when I showed the form, the text was selected. I saw many people had this problem and most suggested adding another control to set focus to. But that seemed inelegant but I tried it anyway. When I was considering the tab index, I had a thought. I removed my other control and set TabStop to false. Et Voilą! Now when I load the form the TextBox, solitary control, isn't focused.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,746

    Re: A trick to not have a solitary TextBox focused on form load.

    It sounds like you have found the best solution in this particular case but there may be other cases where you do want to be able to Tab to that control, so you want TabStop set to True, but you don't want a control to have focus initially. In that case, you can handle the Shown event of the form and set the ActiveControl to Nothing.

    That said, I don't really understand why you aren't using a Label in the first place. If you set the AutoSize property of the Label to True, which it is by default, then the text won't wrap. If you want to size the form based on the size of the Label then you simply set the Text of the Label in the Load event handler, then get the Size of the Label, then set the Size of the form based on that.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,746

    Re: A trick to not have a solitary TextBox focused on form load.

    As an example, I just did this:
    vb.net Code:
    1. Public Class MyMessageBox
    2.  
    3.     Private message As String
    4.  
    5.     Private Sub New(message As String)
    6.         ' This call is required by the designer.
    7.         InitializeComponent()
    8.  
    9.         ' Add any initialization after the InitializeComponent() call.
    10.         Me.message = message
    11.     End Sub
    12.  
    13.     Public Overloads Shared Sub Show(message As String)
    14.         Dim instance As New MyMessageBox(message)
    15.  
    16.         instance.Show()
    17.     End Sub
    18.  
    19.     Private Sub MyMessageBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    20.         Dim widthDifference = Width - Label1.Width
    21.         Dim heightDifference = Height - Label1.Height
    22.  
    23.         Label1.Text = message
    24.  
    25.         Size = New Size(Label1.Width + widthDifference, Label1.Height + heightDifference)
    26.     End Sub
    27.  
    28. End Class
    and it worked just as expected. The form was sized based on whatever text I passed in when calling Show, whether it was long or short and whether or not it contained line breaks. I had a Button on the form with its Anchor set to Bottom, Right, so it remained in the correct position regardless of wht the text was.

  4. #4

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    896

    Re: A trick to not have a solitary TextBox focused on form load.

    That's cool. I'm going to have to play with that later.

  5. #5

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    896

    Re: A trick to not have a solitary TextBox focused on form load.

    It took me forever to come back to this. Been hectic lately.
    I misspoke. I mean I do want it to wrap. Sometimes the error is an HTTP or SQL error and can be long and need to wrap. But I don't see any way to have a label wrap.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,746

    Re: A trick to not have a solitary TextBox focused on form load.

    Quote Originally Posted by cory_jackson View Post
    But I don't see any way to have a label wrap.
    Given that I said this:
    If you set the AutoSize property of the Label to True, which it is by default, then the text won't wrap.
    the logical inference should be that the text WILL wrap if you set AutoSize to False. The text will then wrap when it fills the width of the Label. You obviously have to set the height big enough to display the additional lines of text. You can measure the text to determine how much space it occupies if that's what you want to use to determine the height of the form. Alternatively, don't use a Label and just draw the text on the form. The standard WinForms MessageBox class would presumably use one of those two methods. Have you considered looking at the source for that class to see how it does it?
    Last edited by jmcilhinney; Jun 23rd, 2019 at 08:18 PM.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,985

    Re: A trick to not have a solitary TextBox focused on form load.

    I'm glad you came back to this. I didn't see it the first time around, but while reading it today I was totally puzzled as to why you wouldn't want the text to wrap. Fortunately, you clarified the situation.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    896

    Re: A trick to not have a solitary TextBox focused on form load.

    Thanks John. I was ignorant about the AutoSize property. Yours is a better method. I can size it and anchor it on all 4 sides and it works perfectly.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width