Editing contents not working as control

Help
pabloko
2011-05-13
2013-04-30
  • pabloko
    pabloko
    2011-05-13

    Hi, i've made a classic activex control, but, editing isn't working, but is working perfectly on the visual studio debugger, and not working in the apps that i've included…

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    using Be.Windows.Forms;
    using System.IO;
    using System.Runtime.InteropServices;
    namespace WindowsFormsControlLibrary2
    {
        [ComVisible(true)]
        [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
        public interface IControlEvents
        {
            [DispId(1)]
            void Bark(object callData);
            [DispId(2)]
            void Howl(string callData, string dos);
            [DispId(3)]
            void Eat();
        }
        [ComVisible(false)]
        public delegate void ControlEventHandler(object callData);
        [ComVisible(false)]
        public delegate void ControlEventHandler2(string callData, string dos);
        [ComVisible(false)]
        public delegate void ControlEventHandler3();
        [ProgId("Pabloko.BEHexEditor")]
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.AutoDual)]
        [ComSourceInterfaces(typeof(IControlEvents))]
        public partial class UserControl1 : UserControl
        {
            public event ControlEventHandler Bark;
            public event ControlEventHandler2 Howl;
            public event ControlEventHandler3 Eat;
            
            byte[] _findBuffer = new byte[0];
            string _fileName;
            public DynamicFileByteProvider dynamicFileByteProvider;
            
            public UserControl1()
            {
                InitializeComponent();
               
            }
            void UserControl1_Load(object sender, EventArgs e)
            {
                //hexBox11.Width = this.Width;
                //hexBox11.Height = this.Height;
                //MessageBox.Show(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory());
                //hexBox1.ByteProvider = new  DynamicFileByteProvider("c:\\luanet.lua", true);
                try
                {
                    var enc = new System.Text.UTF8Encoding();
                    var data = enc.GetBytes("sjisdjsidjsidjsd");
                    DynamicByteProvider dbp = new DynamicByteProvider(data);
                    this.hexBox1.ByteProvider = dbp;
                }
                catch (Exception f)
                {
                    MessageBox.Show(f.ToString());
                }
                /*OpenFile("c:\\luanet.lua");
                hexBox1.ReadOnly = false;
                */
                //hexBox11.ByteProvider = new DynamicByteProvider(StringToBytes("jajajajajajjaja"));// DynamicFileByteProvider("c:\\luanet.lua", true);
            }
            public void FindAbort()
            {
                hexBox1.AbortFind();
            }
            public bool CanCopy()
            {
                return hexBox1.CanCopy();
            }
            public bool CanCut()
            {
                return hexBox1.CanCut();
            }
            public bool CanPaste()
            {
                return hexBox1.CanPaste();
            }
            public bool CanPasteHex()
            {
                return hexBox1.CanPasteHex();
            }
            public bool CanSelectAll()
            {
                return hexBox1.CanSelectAll();
            }
            public void Copy()
            {
                hexBox1.Copy();
            }
            public void CopyHex()
            {
                hexBox1.CopyHex();
            }
            public void Cut()
            {
                hexBox1.Cut();
            }
            private byte[] StringToBytes(String cadena)
            {
                System.Text.ASCIIEncoding codificador = new System.Text.ASCIIEncoding();
                return codificador.GetBytes(cadena);
            }
            private void resize(object sender, EventArgs e)
            {
                hexBox1.Width = this.Width;
                hexBox1.Height = this.Height;
            }
            private void hexBox1_dragdrop(object sender, DragEventArgs e)
            {
                object oFileNames = e.Data.GetData(DataFormats.FileDrop);
                string[] fileNames = (string[])oFileNames;
                if (fileNames.Length == 1)
                {
                    OpenFile(fileNames[0]);
                }
            }
            private void hexBox1_dragenter(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.All;
            }
            private void hexBox1_copied(object sender, EventArgs e)
            {
                //ManageAbilityForCopyAndPaste();
            }
            private void hexBox1_copiedex(object sender, EventArgs e)
            {
                //ManageAbilityForCopyAndPaste();
            }
            private void hexBox1_currentlinechanged(object sender, EventArgs e)
            {
                //linechanged
                
                /*this.toolStripStatusLabel.Text = string.Format("Ln {0}    Col {1}",
                    hexBox1.CurrentLine, hexBox1.CurrentPositionInLine);*/
            }
            private void hexBox1_currentpositionchanged(object sender, EventArgs e)
            {
                //positionchanged
                
                /*this.toolStripStatusLabel.Text = string.Format("Ln {0}    Col {1}",
                    hexBox1.CurrentLine, hexBox1.CurrentPositionInLine);*/
            }
            private void hexBox1_selectionlenghtchanged(object sender, EventArgs e)
            {
                //selectionchanged sumale la otra loco!
                
                //ManageAbilityForCopyAndPaste();
            }
            private void hexBox1_selectionstartchanged(object sender, EventArgs e)
            {
                //selectionstartchanged no sumes esta loco!
                //ManageAbilityForCopyAndPaste();
            }
            /// <summary>
            /// Opens a file.
            /// </summary>
            /// <param name="fileName">the file name of the file to open</param>
            public void OpenFile(string fileName)
            {
                if (!File.Exists(fileName))
                {
                    //noxiste
                    ////Program.ShowMessage(strings.FileDoesNotExist);
                    return;
                }
                
                try
                {
                    
                    try
                    {
                        // try to open in write mode
                        dynamicFileByteProvider = new DynamicFileByteProvider(fileName,false);
                        dynamicFileByteProvider.Changed += new EventHandler(byteProvider_Changed);
                        dynamicFileByteProvider.LengthChanged += new EventHandler(byteProvider_LengthChanged);
                    }
                    catch (IOException) // write mode failed
                    {
                        try
                        {
                                dynamicFileByteProvider = new DynamicFileByteProvider(fileName, true);
                                dynamicFileByteProvider.Dispose();
                                return;
                        }
                        catch (IOException) // read-only also failed
                        {
                            // errorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
                            return;
                        }
                    }
                    try
                    {
                        hexBox1.ByteProvider = dynamicFileByteProvider;
                        _fileName = fileName;
                        //DisplayText();
                    }
                    catch (Exception e)
                    { MessageBox.Show(e.ToString()); }
                    //UpdateFileSizeStatus();
                    ////RecentFileHandler.AddFile(fileName);
                }
                catch (Exception ex1)
                {
                    ////Program.ShowError(ex1);
                    return;
                }
                finally
                {
                    //ManageAbility();
                }
            }
            void byteProvider_Changed(object sender, EventArgs e)
            {
                //ManageAbility();
            }
            void byteProvider_LengthChanged(object sender, EventArgs e)
            {
                //UpdateFileSizeStatus();
            }
            /// <summary>
            /// Saves the current file.
            /// </summary>
            private void SaveFile()
            {
                if (hexBox1.ByteProvider == null)
                    return;
                try
                {
                    DynamicFileByteProvider dynamicFileByteProvider = hexBox1.ByteProvider as DynamicFileByteProvider;
                    dynamicFileByteProvider.ApplyChanges();
                }
                catch (Exception ex1)
                {
                    ////Program.ShowError(ex1);
                }
                finally
                {
                    //ManageAbility();
                }
            }
            /// <summary>
            /// Closes the current file
            /// </summary>
            /// <returns>OK, if the current file was closed.</returns>
            private DialogResult CloseFile()
            {
                if (hexBox1.ByteProvider == null)
                    return DialogResult.OK;
                try
                {
                    if (hexBox1.ByteProvider != null && hexBox1.ByteProvider.HasChanges())
                    {
                        DialogResult res = MessageBox.Show("",
                            "",
                            MessageBoxButtons.YesNoCancel,
                            MessageBoxIcon.Warning);
                        if (res == DialogResult.Yes)
                        {
                            SaveFile();
                            CleanUp();
                        }
                        else if (res == DialogResult.No)
                        {
                            CleanUp();
                        }
                        else if (res == DialogResult.Cancel)
                        {
                            return res;
                        }
                        return res;
                    }
                    else
                    {
                        CleanUp();
                        return DialogResult.OK;
                    }
                }
                finally
                {
                    //ManageAbility();
                }
            }
            private void CleanUp()
            {
                if (hexBox1.ByteProvider != null)
                {
                    IDisposable byteProvider = hexBox1.ByteProvider as IDisposable;
                    if (byteProvider != null)
                        byteProvider.Dispose();
                    hexBox1.ByteProvider = null;
                }
                _fileName = null;
                DisplayText();
            }
            void DisplayText()
            {
               /* if (_fileName != null && _fileName.Length > 0)
                {
                    string textFormat = "{0}{1} - {2}";
                    string readOnly = ((DynamicFileByteProvider)hexBox1.ByteProvider).ReadOnly
                        ? "ReadOnly" : "";
                    string text = Path.GetFileName(_fileName);
                    this.Text = string.Format(textFormat, text, readOnly, "PablokoBEHexEditor");
                }
                else
                {
                    this.Text = "PablokoBEHexEditor";
                }*/
            }
            /// <summary>
            /// Opens the Find dialog
            /// </summary>
            private void Find()
            {
                _findBuffer = StringToBytes("abc");
                FindNext();
            }
            /// <summary>
            /// Find next match
            /// </summary>
            private void FindNext()
            {
                if (_findBuffer.Length == 0)
                {
                    //no hay nada que buscar loco
                    return;
                }
                // start find process
                long res = hexBox1.Find(_findBuffer, hexBox1.SelectionStart + hexBox1.SelectionLength);
                if (res == -1) // -1 = no match
                {
                   /* MessageBox.Show("eof", "",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);*/
                    return;
                }
                else if (res == -2) // -2 = find was aborted
                {
                    return;
                }
                else // something was found
                {
                    if (!hexBox1.Focused)
                        hexBox1.Focus();
                }
            }
            /*
            /// <summary>
            /// Aborts the current find process
            /// </summary>
            void FormFindCancel_Closed(object sender, EventArgs e)
            {
                hexBox1.AbortFind();
            }
            */
            /*
            /// <summary>
            /// Put focus back to the cancel form.
            /// </summary>
            void FocusToFormFindCancel(object sender, EventArgs e)
            {
                //_formFindCancel.Focus();
            }
            */
            /// <summary>
            /// Displays the goto byte dialog.
            /// </summary>
            private void Goto()
            {
                    hexBox1.SelectionStart = 5;
                    hexBox1.SelectionLength = 1;
                    hexBox1.Focus();
            }
            private void hexBox1_KeyDown(object sender, KeyEventArgs e)
            {
                //MessageBox.Show("eeeeeeeee");
            }
        }
    }