Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - User Control -> disappear when form.Click

Reply
 
Old 09-22-2008   #1 (permalink)
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

My System SpecsSystem Spec
Old 09-22-2008   #2 (permalink)


Vista Business x64
 
 

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
{
publicpartialclassUserControl1 : UserControl
{
bool _IsVisible = true;
publicdelegatevoidOnClickHandler(object sender, System.EventArgs e);
publiceventOnClickHandler ClickHandler;
public UserControl1()
{
InitializeComponent();
}
publicbool IsButtonVisible
{
get { return _IsVisible; }
set
{
_IsVisible =
value;
this.button1.Visible = _IsVisible;
}
}
privatevoid 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
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid Form1_Load(object sender, EventArgs e)
{
}
privatevoid Form1_Click(object sender, EventArgs e)
{
this.userControl11.IsButtonVisible = false;
}
privatevoid 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.
My System SpecsSystem Spec
Old 09-23-2008   #3 (permalink)
Mrozu


 
 

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
My System SpecsSystem Spec
Old 09-23-2008   #4 (permalink)


Vista Business x64
 
 

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
{
publicpartialclassUserControl1 : UserControl
{
bool _IsVisible = false;
publicdelegatevoidOnClickHandler(object sender, System.EventArgs e);
publiceventOnClickHandler ClickHandler;
public UserControl1()
{
InitializeComponent();
_IsVisible =
false;
this.dataGridView1.Visible = _IsVisible;
DataTable dt = newDataTable();
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;

}
publicbool IsGridVisible
{
get { return _IsVisible; }
set
{
_IsVisible =
value;
this.dataGridView1.Visible = _IsVisible;
}
}
privatevoid panel1_Click(object sender, EventArgs e)
{
if (ClickHandler != null)
ClickHandler(
this, e);
}
privatevoid linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.IsGridVisible = true;
}
privatevoid dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
this.textBox1.Text = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
this.IsGridVisible = false;
}
privatevoid 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
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid Form1_Click(object sender, EventArgs e)
{
this.userControl11.IsGridVisible = false;
}
privatevoid userControl11_Click(object sender, EventArgs e)
{
this.userControl11.IsGridVisible = true;
}
}
}

My System SpecsSystem Spec
Old 09-23-2008   #5 (permalink)


Vista Business x64
 
 

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
{
publicpartialclassForm1 : Form
{
privateExtraSpecialLinkLabel MyExtraSpecialLinkLabel = null;
public Form1()
{
InitializeComponent();
}
privatevoid Form1_Load(object sender, EventArgs e)
{
MyExtraSpecialLinkLabel =
newExtraSpecialLinkLabel();
this.Controls.Add(MyExtraSpecialLinkLabel);
MyExtraSpecialLinkLabel.Top = 20;
MyExtraSpecialLinkLabel.Left = 10;
MyExtraSpecialLinkLabel.Width = 300;
MyExtraSpecialLinkLabel.Height = 200;
MyExtraSpecialLinkLabel.Text =
"Click Me";
MyExtraSpecialLinkLabel.Visible =
true;
}
privatevoid Form1_Click(object sender, EventArgs e)
{
MyExtraSpecialLinkLabel.IsGridVisible =
false;
}
}
publicclassExtraSpecialLinkLabel : LinkLabel
{
privateDataGridView MyGrid = null;
privatebool _IsGridVisible = false;
public ExtraSpecialLinkLabel()
{
_IsGridVisible =
false;
this.BackColor = Color.LightGray;
MyGrid =
newDataGridView();
DataTable dt = newDataTable();
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 +=
newDataGridViewCellEventHandler(MyGrid_CellClick);
}
void MyGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.Text = MyGrid[e.ColumnIndex, e.RowIndex].Value.ToString();
MyGrid.Visible =
false;
}
protectedoverridevoid OnClick(EventArgs e)
{
this.IsGridVisible = false;
}
protectedoverridevoid OnLinkClicked(LinkLabelLinkClickedEventArgs e)
{
this.IsGridVisible = true;
}
publicbool IsGridVisible
{
get { return _IsGridVisible; }
set
{
_IsGridVisible =
value;
MyGrid.Visible = _IsGridVisible;
}
}
}
}

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Files disappear, Control Panel won't launch General Discussion
icons disappear and reappear on right-click or delete Vista General
Control Panel disappear Vista General
Is UAC causing my screen to disappear when I click manage my compu Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46