Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting the name (vb6)

    Hello all

    You know how sometimes when you first set up an application it asks for your name, but the field is filled in for you automatically with whatever name you chose to name your computer. What is the code to grab the name of your computer?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Getting the name (vb6)

    Not sure exactly which name you're looking for: computer or user. Try this VBScript fragment. If it really is the computer name you want, and you can use WinAPI in the relevant context, check out the rest of that thread.

  3. #3
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Getting the name (vb6)

    Okay, I will try this again. You have a new program that you just bought, you install it on your computer, when it askes you for your name the feild is already filled with a name (Usually yours) and is highlighted blue in case you want to modify it. I want to be able to mimic exactly that. How????????

    thanks

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Getting the name (vb6)

    (1) Add a new module to your project. Copy the following code into it:

    Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
    (ByVal Buffer As String, Size As Long) As Long

    Public Function CompName() As String
    Dim strBuff As String * 16
    GetComputerName strBuff, Len(strBuff)
    CompName = Left(strBuff, InStr(strBuff, vbNullChar) - 1)
    End Function

    This code will get the computer name using a Windows API call. There are also Windows API calls to get the login name, network user name etc.

    (2) On your form, add a text box. In the following example, it is called txtName.

    Add the following code to the form:

    Private Sub Form_Load()
    ' Set the text of the text box to the computer name
    txtName = CompName
    ' Select the entire name
    txtName.SelLength = Len(txtName.Text)
    End Sub

    This code sets and selects the computer name in the text box.

  5. #5
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Getting the name (vb6)

    Thanks that worked great except for the very last part

    txtName.SelLength = Len(txtName.Text)

    For some reason the code does not set the left side equal to the right side. I don't have any explantion for it.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Getting the name (vb6)

    Perhaps the text box is not the first in the tab order. You will only see the entire text selected if the text box is the active control. Try setting the TabIndex property of the text box to 0.

  7. #7
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Getting the name (vb6)

    Nope, still did not work. In my case I am pushing a button to check the value inside the textbox first, if the value is wrong then I want it highlighed, I tried to setfocus on the text box first and then do the sellength but that did not work either.

  8. #8
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Getting the name (vb6)

    Okay problem was solved

    txtName.SetFocus
    txtName.SelStart = 0
    txtName.SelLength = Len(txtName.Text)

    is the way I had to do it.

Posting Permissions

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