User Control -> disappear when form.Click

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mrozu

    User Control -> disappear when form.Click

    Hi,


    i've got an inherited usercontrol with added DataGridView. I plant my
    control on form and I want to hide datagridview when user click on
    that form.

    I've tried to use LostFocus and Leave. It works great, when focus is
    set to other control on form, but not fired on form.Click.

    Is there any solution?


    Best regards,
    Mrozu
  • breitak67

    #2
    Re: User Control -> disappear when form.Click


    I may be misunderstandin g what you are trying to do. Does this example
    represent something like what you are trying to do?

    Here's my example user control:

    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows. Forms;
    namespace Form_Click_Test
    {
    public partial class UserControl1 : UserControl
    {
    bool _IsVisible = true;
    public delegate void OnClickHandler( object sender, System.EventArg s e);
    public event OnClickHandler ClickHandler;
    public UserControl1()
    {
    InitializeCompo nent();
    }
    public bool IsButtonVisible
    {
    get { return _IsVisible; }
    set
    {
    _IsVisible = value;
    this.button1.Vi sible = _IsVisible;
    }
    }
    private void panel1_Click(ob ject sender, EventArgs e)
    {
    if (ClickHandler != null)
    ClickHandler(th is, e);
    }
    }
    }


    Here's my app:

    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;
    namespace Form_Click_Test
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeCompo nent();
    }
    private void Form1_Load(obje ct sender, EventArgs e)
    {
    }
    private void Form1_Click(obj ect sender, EventArgs e)
    {
    this.userContro l11.IsButtonVis ible = false;
    }
    private void userControl11_C lick(object sender, EventArgs e)
    {
    this.userContro l11.IsButtonVis ible = true;
    }
    }
    }

    You can also bury the visibility behavior in the user conrtol if you
    want it invisible to the developer.


    --
    breitak67

    Comment

    • Mrozu

      #3
      Re: User Control -> disappear when form.Click

      Hi, thanks for quick answer.


      i think that you don't understand me at all.


      my control inherits LinkLabel. Then, i add to my control DatagridView,
      and on Me.Click (click on label text) datagridview is appearing under
      LinkLabel.
      Then, when I click on row, it's getting data from column and hiding
      datagrid. But I want to hide it also on form.click (just like for
      example combobox is)

      any suggestion now?:)



      Mrozu

      Comment

      • breitak67

        #4
        Re: User Control -> disappear when form.Click


        Ah, now I understand. I think a small tweak to what I posted before
        does exactly what you want, except with a composite control rather than
        a subclass of the linklabel control. I'll give the subclass a try and
        post back. This is the composite control version (not pretty to look
        at, but basically functional):

        using System;
        using System.Collecti ons.Generic;
        using System.Componen tModel;
        using System.Drawing;
        using System.Data;
        using System.Text;
        using System.Windows. Forms;
        namespace Form_Click_Test
        {
        public partial class UserControl1 : UserControl
        {
        bool _IsVisible = false;
        public delegate void OnClickHandler( object sender, System.EventArg s e);
        public event OnClickHandler ClickHandler;
        public UserControl1()
        {
        InitializeCompo nent();
        _IsVisible = false;
        this.dataGridVi ew1.Visible = _IsVisible;
        DataTable dt = new DataTable();
        dt.Columns.Add( \"ID\", System.Type.Get Type(\"System.I nt32\"));
        dt.Columns.Add( \"Desc\", System.Type.Get Type(\"System.S tring\"));

        DataRow dr = dt.NewRow();
        dr[\"ID\"] = 1; dr[\"Desc\"] = \"Row 1\";
        dt.Rows.Add(dr) ;
        dr = dt.NewRow();
        dr[\"ID\"] = 2; dr[\"Desc\"] = \"Row 2\";
        dt.Rows.Add(dr) ;
        this.dataGridVi ew1.DataSource = dt;

        }
        public bool IsGridVisible
        {
        get { return _IsVisible; }
        set
        {
        _IsVisible = value;
        this.dataGridVi ew1.Visible = _IsVisible;
        }
        }
        private void panel1_Click(ob ject sender, EventArgs e)
        {
        if (ClickHandler != null)
        ClickHandler(th is, e);
        }
        private void linkLabel1_Link Clicked(object sender,
        LinkLabelLinkCl ickedEventArgs e)
        {
        this.IsGridVisi ble = true;
        }
        private void dataGridView1_C ellMouseClick(o bject sender,
        DataGridViewCel lMouseEventArgs e)
        {
        this.textBox1.T ext = dataGridView1[e.ColumnIndex,
        e.RowIndex].Value.ToString ();
        this.IsGridVisi ble = false;
        }
        private void UserControl1_Cl ick(object sender, EventArgs e)
        {
        this.IsGridVisi ble = false;
        }
        }
        }


        using System;
        using System.Collecti ons.Generic;
        using System.Componen tModel;
        using System.Data;
        using System.Drawing;
        using System.Text;
        using System.Windows. Forms;
        namespace Form_Click_Test
        {
        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeCompo nent();
        }
        private void Form1_Click(obj ect sender, EventArgs e)
        {
        this.userContro l11.IsGridVisib le = false;
        }
        private void userControl11_C lick(object sender, EventArgs e)
        {
        this.userContro l11.IsGridVisib le = true;
        }
        }
        }


        --
        breitak67

        Comment

        • breitak67

          #5
          Re: User Control -> disappear when form.Click


          I think this does what you want. It needs some cosmetic work, but
          functionally behaves like a combobox:

          using System;
          using System.Collecti ons.Generic;
          using System.Componen tModel;
          using System.Data;
          using System.Drawing;
          using System.Text;
          using System.Windows. Forms;
          namespace SubclassLinkLab elTest
          {
          public partial class Form1 : Form
          {
          private ExtraSpecialLin kLabel MyExtraSpecialL inkLabel = null;
          public Form1()
          {
          InitializeCompo nent();
          }
          private void Form1_Load(obje ct sender, EventArgs e)
          {
          MyExtraSpecialL inkLabel = new ExtraSpecialLin kLabel();
          this.Controls.A dd(MyExtraSpeci alLinkLabel);
          MyExtraSpecialL inkLabel.Top = 20;
          MyExtraSpecialL inkLabel.Left = 10;
          MyExtraSpecialL inkLabel.Width = 300;
          MyExtraSpecialL inkLabel.Height = 200;
          MyExtraSpecialL inkLabel.Text = \"Click Me\";
          MyExtraSpecialL inkLabel.Visibl e = true;
          }
          private void Form1_Click(obj ect sender, EventArgs e)
          {
          MyExtraSpecialL inkLabel.IsGrid Visible = false;
          }
          }
          public class ExtraSpecialLin kLabel : LinkLabel
          {
          private DataGridView MyGrid = null;
          private bool _IsGridVisible = false;
          public ExtraSpecialLin kLabel()
          {
          _IsGridVisible = false;
          this.BackColor = Color.LightGray ;
          MyGrid = new DataGridView();
          DataTable dt = new DataTable();
          dt.Columns.Add( \"Desc\", System.Type.Get Type(\"System.S tring\"));
          DataRow dr = dt.NewRow();
          dr[\"Desc\"] = \"Row 1\";
          dt.Rows.Add(dr) ;
          dr = dt.NewRow();
          dr[\"Desc\"] = \"Row 2\";
          dt.Rows.Add(dr) ;
          MyGrid.DataSour ce = dt;
          this.Controls.A dd(MyGrid);
          MyGrid.Top = 20;
          MyGrid.Left = 10;
          MyGrid.Visible = _IsGridVisible;
          MyGrid.CellClic k += new DataGridViewCel lEventHandler(M yGrid_CellClick );
          }
          void MyGrid_CellClic k(object sender, DataGridViewCel lEventArgs e)
          {
          this.Text = MyGrid[e.ColumnIndex, e.RowIndex].Value.ToString ();
          MyGrid.Visible = false;
          }
          protected override void OnClick(EventAr gs e)
          {
          this.IsGridVisi ble = false;
          }
          protected override void OnLinkClicked(L inkLabelLinkCli ckedEventArgs e)
          {
          this.IsGridVisi ble = true;
          }
          public bool IsGridVisible
          {
          get { return _IsGridVisible; }
          set
          {
          _IsGridVisible = value;
          MyGrid.Visible = _IsGridVisible;
          }
          }
          }
          }


          --
          breitak67

          Comment

          Working...