locked
form witrh textbox to display logged on user RRS feed

  • Question

  • Hi Everyone, Just downloaded and installed visual studio 2019, never used this before (and only know the basics of vba) I need advice or help with my first form(ie. hello world), I wish to add a text box that'll show the logged on users name, when I build it and run it. Any pointers are really welcome, I am finding it really overwhelming at the moment..

    Lables easy to add

    Comboxs same

    accessing the code behind each is fine

    typing or I should say finding a resource to get the cod ehtat I need ...no idea (Yet)

    thanks for reading

       
    Sunday, January 27, 2019 1:16 PM

Answers

  • If you double-click on the Form, it will add :

    Private Sub Form1_Load...

    then you can add inside (if you added a TextBox named TextBox1) :

    TextBox1.Text = Environment.UserName


    • Edited by Castorix31 Sunday, January 27, 2019 1:24 PM
    • Marked as answer by G-Ger Sunday, January 27, 2019 2:56 PM
    Sunday, January 27, 2019 1:23 PM
  • Hello,

    Use Environment.UserName for user name and Environment.UserDomainName for the domain name. 

    So given a label named userLabel is on the form, add this code by double clicking the form, place the code in the newly created load event or create a button, double click for a click event and place the code there.

    userLabel.Text = Environment.UserName

    Get user name and domain together.

    Security.Principal.WindowsIdentity.GetCurrent().Name


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    • Edited by KareninstructorMVP Sunday, January 27, 2019 1:27 PM
    • Marked as answer by G-Ger Sunday, January 27, 2019 2:56 PM
    Sunday, January 27, 2019 1:24 PM

All replies

  • If you double-click on the Form, it will add :

    Private Sub Form1_Load...

    then you can add inside (if you added a TextBox named TextBox1) :

    TextBox1.Text = Environment.UserName


    • Edited by Castorix31 Sunday, January 27, 2019 1:24 PM
    • Marked as answer by G-Ger Sunday, January 27, 2019 2:56 PM
    Sunday, January 27, 2019 1:23 PM
  • Hello,

    Use Environment.UserName for user name and Environment.UserDomainName for the domain name. 

    So given a label named userLabel is on the form, add this code by double clicking the form, place the code in the newly created load event or create a button, double click for a click event and place the code there.

    userLabel.Text = Environment.UserName

    Get user name and domain together.

    Security.Principal.WindowsIdentity.GetCurrent().Name


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    • Edited by KareninstructorMVP Sunday, January 27, 2019 1:27 PM
    • Marked as answer by G-Ger Sunday, January 27, 2019 2:56 PM
    Sunday, January 27, 2019 1:24 PM
  • re-Security.Principal.WindowsIdentity.GetCurrent().Name

    this shows up underlined in red, regardles of where I put the code in

    If i hover over it it shows message "property access must be assigned to the property or use its value."

    So I tried the below and finally got it work then..though this is vba and i wish to get used to vb.net so could you show me what the translation of the sysntax to vb

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click 'Get user name And domain together. Dim loggedon As Object loggedon = Security.Principal.WindowsIdentity.GetCurrent().Name TextBox1.Text = loggedon

    End Sub


    Monday, January 28, 2019 3:29 AM
  • Here is zero reasons for this not working, here are my references for a project.

    And here it is in the Framework source.

    // ==++==
    // 
    //   Copyright (c) Microsoft Corporation.  All rights reserved.
    // 
    // ==--==
    // <OWNER>Microsoft</OWNER>
    // 
    
    //
    // WindowsIdentity.cs
    //
    // Representation of a process/thread token.
    //
    
    namespace System.Security.Principal
    {
        using System.Diagnostics.Contracts;    
        using System.Reflection;
        using System.Runtime.CompilerServices;
        #if FEATURE_CORRUPTING_EXCEPTIONS
        using System.Runtime.ExceptionServices;
        #endif // FEATURE_CORRUPTING_EXCEPTIONS
        using System.Runtime.InteropServices;
        using System.Runtime.Serialization;
        using System.Runtime.Versioning;
        using System.Security.AccessControl;
        using System.Security.Permissions;
        using System.Text;
        using System.Threading;
        using Microsoft.Win32;
        using Microsoft.Win32.SafeHandles;
    
    #if !FEATURE_CORECLR
        using System.Security.Claims;
        using System.Collections.ObjectModel;
        using System.Collections.Generic;
        using System.Globalization;
    #endif
    
        [Serializable]
        [System.Runtime.InteropServices.ComVisible(true)]
        public enum WindowsAccountType {
            Normal      = 0,
            Guest       = 1,
            System      = 2,
            Anonymous   = 3
        }
    
        // Keep in sync with vm\comprincipal.h
        internal enum WinSecurityContext {
            Thread = 1, // OpenAsSelf = false
            Process = 2, // OpenAsSelf = true
            Both = 3 // OpenAsSelf = true, then OpenAsSelf = false
        }
    
        internal enum ImpersonationQueryResult {
            Impersonated    = 0,    // current thread is impersonated
            NotImpersonated = 1,    // current thread is not impersonated
            Failed          = 2     // failed to query 
        }
    
        [Serializable]
        [System.Runtime.InteropServices.ComVisible(true)]
    #if !FEATURE_CORECLR
        public class WindowsIdentity : ClaimsIdentity, ISerializable, IDeserializationCallback, IDisposable {
    #else
        public class WindowsIdentity : IIdentity, ISerializable, IDeserializationCallback, IDisposable {
    #endif

    And my code without errors.

    Now since VS2019 is in preview mode, not fully released this may be why it does not work. I have used in in VS2015 and VS2017, zero issues.

    Lastly, here is an example done in VB.NET by Microsoft

    https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity?view=netframework-4.7.2


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, January 28, 2019 3:41 AM
  • Many thanks for the info..Can't even find where the refernces are located as yet though, and F!/help is useless. but thanks a bunch for replying.

    Monday, January 28, 2019 5:46 AM