Skip to content

Custom Listview Control

Carlos Ponce

Asked Feb 10, 2006, 6:26 AM EST2 answers
Share
LinkedInX
Hi, i'm Carlos from mérida, yucatán, méxico.

I'm beginining with C# and i already made a database application (MySQL). But now, i want to grow in my knowledge of C# and i'm trying to do a custom control.

What allegedly will do my custom control?? Well, for my application i used a listview to show the database information instead a datagrid. I populate the listview in my app but now, due for my further applications are very similars i want to create a "bind" listview to a mysql table. Additionally my custom control contains a customizable label that works like a title of my listview (like the title of a frame control with background, font type, font color, etc.). At this time, the visual part of my control is achieved, but now i face my first dude, how exactly i will managed the events of my custom listview (that i name BindListView)??. I mean, i want that the BindLlistview preserve the events of the inner listview, for example, SelectedItemChanged event that returns the last selected item in the listview, but i don't know how. My custom listview inherit from the UserControl class.

I'll appreciate your help. Have a nice day...

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Hello Carlos,

    You must to implement Event's handlers and delegates.

    for example:

    /// <summary>
    /// Evento que verifica el bot=n del rat=n que fue presionado a fin de
    /// ejecutar el evento correpondiente <see cref="OnUserSelectLeftClick"/>
    /// y <see cref="OnUserSelectRightClick"/>
    /// </summary>
    /// <param name="sender">El control de tipo optionbutton</param>
    /// <param name="e">De tipo MouseEventArg</param>
    public void MouseDownHandler(object sender, MouseEventArgs e)
    {
      if (!this.ReadOnly)
      {
        if (e.Button == MouseButtons.Left)
        {
          this.ClickHandler(sender, new EventArgs());
          if (null != OnUserSelectLeftClick)
            OnUserSelectLeftClick(sender, e);
        }
        else if (Control.MouseButtons==MouseButtons.Right)
        {
          if (null != OnUserSelectRightClick)
            OnUserSelectRightClick(sender, e);
        }
      }
    }

    Este evento se disparará cuando el usuario presione un botón del ratón (izq. o der). Debes definir un delegado para que desde tu aplicación puedas personalizar posteriormente el funcionamiento de ese evento (ligarlo, por ejemplo a otro evento que cumpla con los mismos argumentos).

    public delegate void OnUserSelectLeftClickEventHandler(object sender, MouseEventArgs e);
    public event OnUserSelectLeftClickEventHandler OnUserSelectLeftClick;

    public delegate void OnUserSelectRightClickEventHandler(object sender, MouseEventArgs e);
    public event OnUserSelectRightClickEventHandler OnUserSelectRightClick;


    Saludos


    0
  • 0
    You can try this too.

    YourControl.OnSelectedItemEvent += new OnSelectedItemEventHandler(form_YourPersonalizedEvent);

    regards

    0