Connecting Tech Pros Worldwide Help | Site Map

User Control -> disappear when form.Click

Mrozu
Guest
 
Posts: n/a
#1: Sep 22 '08
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
Guest
 
Posts: n/a
#2: Sep 23 '08

re: User Control -> disappear when form.Click



I may be misunderstanding 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.Collections.Generic;
using System.ComponentModel;
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.EventArgs e);
public event OnClickHandler ClickHandler;
public UserControl1()
{
InitializeComponent();
}
public bool IsButtonVisible
{
get { return _IsVisible; }
set
{
_IsVisible = value;
this.button1.Visible = _IsVisible;
}
}
private void panel1_Click(object sender, EventArgs e)
{
if (ClickHandler != null)
ClickHandler(this, e);
}
}
}


Here's my app:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Form_Click_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Click(object sender, EventArgs e)
{
this.userControl11.IsButtonVisible = false;
}
private void userControl11_Click(object sender, EventArgs e)
{
this.userControl11.IsButtonVisible = true;
}
}
}

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


--
breitak67
Mrozu
Guest
 
Posts: n/a
#3: Sep 23 '08

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
breitak67
Guest
 
Posts: n/a
#4: Sep 23 '08

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.Collections.Generic;
using System.ComponentModel;
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.EventArgs e);
public event OnClickHandler ClickHandler;
public UserControl1()
{
InitializeComponent();
_IsVisible = false;
this.dataGridView1.Visible = _IsVisible;
DataTable dt = new DataTable();
dt.Columns.Add(\"ID\", System.Type.GetType(\"System.Int32\"));
dt.Columns.Add(\"Desc\", System.Type.GetType(\"System.String\"));

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.dataGridView1.DataSource = dt;

}
public bool IsGridVisible
{
get { return _IsVisible; }
set
{
_IsVisible = value;
this.dataGridView1.Visible = _IsVisible;
}
}
private void panel1_Click(object sender, EventArgs e)
{
if (ClickHandler != null)
ClickHandler(this, e);
}
private void linkLabel1_LinkClicked(object sender,
LinkLabelLinkClickedEventArgs e)
{
this.IsGridVisible = true;
}
private void dataGridView1_CellMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{
this.textBox1.Text = dataGridView1[e.ColumnIndex,
e.RowIndex].Value.ToString();
this.IsGridVisible = false;
}
private void UserControl1_Click(object sender, EventArgs e)
{
this.IsGridVisible = false;
}
}
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Form_Click_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Click(object sender, EventArgs e)
{
this.userControl11.IsGridVisible = false;
}
private void userControl11_Click(object sender, EventArgs e)
{
this.userControl11.IsGridVisible = true;
}
}
}


--
breitak67
breitak67
Guest
 
Posts: n/a
#5: Sep 23 '08

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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SubclassLinkLabelTest
{
public partial class Form1 : Form
{
private ExtraSpecialLinkLabel MyExtraSpecialLinkLabel = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MyExtraSpecialLinkLabel = new ExtraSpecialLinkLabel();
this.Controls.Add(MyExtraSpecialLinkLabel);
MyExtraSpecialLinkLabel.Top = 20;
MyExtraSpecialLinkLabel.Left = 10;
MyExtraSpecialLinkLabel.Width = 300;
MyExtraSpecialLinkLabel.Height = 200;
MyExtraSpecialLinkLabel.Text = \"Click Me\";
MyExtraSpecialLinkLabel.Visible = true;
}
private void Form1_Click(object sender, EventArgs e)
{
MyExtraSpecialLinkLabel.IsGridVisible = false;
}
}
public class ExtraSpecialLinkLabel : LinkLabel
{
private DataGridView MyGrid = null;
private bool _IsGridVisible = false;
public ExtraSpecialLinkLabel()
{
_IsGridVisible = false;
this.BackColor = Color.LightGray;
MyGrid = new DataGridView();
DataTable dt = new DataTable();
dt.Columns.Add(\"Desc\", System.Type.GetType(\"System.String\"));
DataRow dr = dt.NewRow();
dr[\"Desc\"] = \"Row 1\";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[\"Desc\"] = \"Row 2\";
dt.Rows.Add(dr);
MyGrid.DataSource = dt;
this.Controls.Add(MyGrid);
MyGrid.Top = 20;
MyGrid.Left = 10;
MyGrid.Visible = _IsGridVisible;
MyGrid.CellClick += new DataGridViewCellEventHandler(MyGrid_CellClick);
}
void MyGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.Text = MyGrid[e.ColumnIndex, e.RowIndex].Value.ToString();
MyGrid.Visible = false;
}
protected override void OnClick(EventArgs e)
{
this.IsGridVisible = false;
}
protected override void OnLinkClicked(LinkLabelLinkClickedEventArgs e)
{
this.IsGridVisible = true;
}
public bool IsGridVisible
{
get { return _IsGridVisible; }
set
{
_IsGridVisible = value;
MyGrid.Visible = _IsGridVisible;
}
}
}
}


--
breitak67
Closed Thread