System.Windows.Forms.HexBox.Copy C# (CSharp) Method

Copy() public method

Copies the current selection in the hex box to the Clipboard.
public Copy ( ) : void
return void
        public void Copy()
        {
            if(!CanCopy()) return;

            // put bytes into buffer
            byte[] buffer = new byte[_selectionLength];
            int id = -1;
            for(long i = _bytePos; i < _bytePos+_selectionLength; i++)
            {
                id++;

                buffer[id] = _byteProvider.ReadByte(i);
            }

            DataObject da = new DataObject();

            // set string buffer clipbard data
            string sBuffer = System.Text.Encoding.ASCII.GetString(buffer, 0, buffer.Length);
            da.SetData(typeof(string), sBuffer);

            //set memorystream (BinaryData) clipboard data
            System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer, 0, buffer.Length, false, true);
            da.SetData("BinaryData", ms);

            Clipboard.SetDataObject(da, true);
            UpdateCaret();
            ScrollByteIntoView();
            Invalidate();
        }