none
change debug writeline to textbox.text RRS feed

  • Question

  • Hi all,

    i am very beginner,and my english is very bad too. I need  little help.

    I find one very good program from sim0n :Get the amount and colour of pixels in an Image

    (https://sim0n.wordpress.com/2009/03/30/vbnet-q-get-the-amount-and-colour-of-pixels-in-an-image/#comments)I tried to send him/her an email,but his/her email address is invalid.

    I need use this program with textbox (without debug writeline ).

    Thank you very much for your help.

    Dim PixelList As New List(Of String())
    
        Public Sub ColourInList(ByVal Colour As Color)
            Dim Found As Boolean = False
            For Each str As String() In PixelList
                If str(0) = CStr(Colour.ToArgb) Then
                    str(1) = CStr(CInt(str(1)) + 1)
                    Found = True
                    Exit For
                End If
            Next
            If Found = False Then PixelList.Add(New String() {CStr(Colour.ToArgb), 1})
        End Sub
    
        Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
            Dim btmp As New Bitmap(PictureBox1.Image)
            For x As Integer = 0 To btmp.Width - 1
                For y As Integer = 0 To btmp.Height - 1
                    ColourInList(btmp.GetPixel(x, y))
                Next
            Next
            For Each str As String() In PixelList
                Debug.WriteLine(Color.FromArgb(str(0)).ToString & " | Count: " & str(1))
            Next
        End Sub



    Friday, November 22, 2019 2:16 PM

Answers

  • Hello,

    If you were open to doing Console.WriteLine rather than Debug.WriteLine continue.

    Add this class to your project.

    Imports System.IO
    Imports System.Text
    
    Public Class ControlWriter
        Inherits TextWriter
    
        Private _textbox As Control
        Public Sub New(textbox As Control)
            _textbox = textbox
        End Sub
    
        Public Overrides Sub Write(value As Char)
            _textbox.Text += value
        End Sub
    
        Public Overrides Sub Write(value As String)
            _textbox.Text += value
        End Sub
    
        Public Overrides ReadOnly Property Encoding() As Encoding
            Get
                Return Encoding.ASCII
            End Get
        End Property
    End Class

    Add the following to Form Load or Form Shown event and if the TextBox name is different from your's change it.

    Console.SetOut(New ControlWriter(TextBox1))

    Now using the following information is written to the TextBox.

    Console.WriteLine("Place your text here")


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Identity80 Friday, November 22, 2019 5:06 PM
    Friday, November 22, 2019 3:09 PM
    Moderator

All replies

  • Hello,

    If you were open to doing Console.WriteLine rather than Debug.WriteLine continue.

    Add this class to your project.

    Imports System.IO
    Imports System.Text
    
    Public Class ControlWriter
        Inherits TextWriter
    
        Private _textbox As Control
        Public Sub New(textbox As Control)
            _textbox = textbox
        End Sub
    
        Public Overrides Sub Write(value As Char)
            _textbox.Text += value
        End Sub
    
        Public Overrides Sub Write(value As String)
            _textbox.Text += value
        End Sub
    
        Public Overrides ReadOnly Property Encoding() As Encoding
            Get
                Return Encoding.ASCII
            End Get
        End Property
    End Class

    Add the following to Form Load or Form Shown event and if the TextBox name is different from your's change it.

    Console.SetOut(New ControlWriter(TextBox1))

    Now using the following information is written to the TextBox.

    Console.WriteLine("Place your text here")


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Identity80 Friday, November 22, 2019 5:06 PM
    Friday, November 22, 2019 3:09 PM
    Moderator
  • Thank you all Karen, you are great !
    Each day i learning something new...
    Friday, November 22, 2019 5:09 PM