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 A trick to not have a solitary TextBox focused on form load.-VBForums
Results 1 to 4 of 4

Thread: 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
    858

    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,202

    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,202

    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
    858

    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.

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