none
How to keep Format from a PowerShell file dragged onto Richtextbox?

    Question

  • Hi all,

    Using: VS2015, C#, WinFORMS

    I am developing a dashboard with several tools with C# in a winform. A project for evaluating my skills.
    The purpose of the dashboard is to have all the needed tools right there on your WINFORM.

    I found the initial code from a forum and modified to meet my needs.

    I got the code working but, the code I found doesn’t keep the format from the PowerShell file when you Drag and drop the file onto the RichTextBox.

    The following code is what I am using….

      {
                object filename = e.Data.GetData("FileDrop");
                if (filename != null)
                {
                    var list = filename as string[];
    
                    if (list != null && !string.IsNullOrWhiteSpace(list[0]))
                    {
    
                        Richtextbox1.Clear();
                        Richtextbox1.LoadFile(list[0], RichTextBoxStreamType.PlainText);
    
                    }
                }   

    When I drag and drop a PowerShell file onto the RichtextBox it reads and displays the content in plain text as below:

    #Disk Utilization

    Get-WmiObject –ComputerName PCName –Class Win32_Volume `
           | ft –auto DriveLetter,`
                      Label,`
                      @{Label=”Free(GB)”;Expression={“{0:N0}” –F ($_.FreeSpace/1GB)}},`
                      @{Label=”%Free”;Expression={“{0:P0}” –F ($_.FreeSpace/$_.Capacity)}}

    If I copy and paste the PowerShell code to the RichtextBox it keeps the format from PowerShell as below:

    #Disk Utilization 
    
    Get-WmiObject –ComputerName PCName –Class Win32_Volume `
           | ft –auto DriveLetter,`
                      Label,`
                      @{Label=”Free(GB)”;Expression={“{0:N0}” –F ($_.FreeSpace/1GB)}},`
                      @{Label=”%Free”;Expression={“{0:P0}” –F ($_.FreeSpace/$_.Capacity)}}
    

    I have changed the RichTextBoxStreamType.PlainText to RichTextBoxStreamType.RichNoOleObjsPlainText, RichTextBoxStreamType.RichTextPlainText, RichTextBoxStreamType.TextTextOleObjsPlainText and RichTextBoxStreamType.UnicodePlainTextPlainText but come back with error not valid format.

    What I have been looking for is, that when I Drag and Drop the PowerShell file onto the Richtextbox I want to keep the formatting from PowerShell.

    Any suggestion is appreciated.

    Regards,


    Regards

    Friday, December 2, 2016 4:16 PM

All replies

  • Hi Luis E Malave,

    Could you share a screenshot to show what's different between the Drag/Drop and Copy/Past file? It could help us understanding your problem clearly.

    According to your code snippet and the PowerShell file content, I don't find there has any format differences between Drag/Drop and Copy/Past operations.

    Following screenshot shows the result I Drag/Drop and Copy/Past the file into RichTextBox in my WinForm application. I don't find there has any difference.

    And following is my sample project, please try use it in your side to check whether there still has format issue.

    https://1drv.ms/u/s!AkHKK8SV6besg2YFO1w5n23yhW0M

    Best Regards,
    Weiwei


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 20, 2016 6:09 AM
    Moderator