0
我想在我的Windows窗体应用程序中创建一个控件。该控件包含一些数据作为datagridview控件。但我的要求是将此控件显示为弹出控件。下面是这个的屏幕截图。使用close(X)按钮创建自定义datagridview弹出控件
请帮我解决这个问题。任何帮助赞赏。
注意: - 我希望我的表单与上面的屏幕截图相同意味着我只希望我的datagridview可见,并且我不希望表单标题及其边框。
我想在我的Windows窗体应用程序中创建一个控件。该控件包含一些数据作为datagridview控件。但我的要求是将此控件显示为弹出控件。下面是这个的屏幕截图。使用close(X)按钮创建自定义datagridview弹出控件
请帮我解决这个问题。任何帮助赞赏。
注意: - 我希望我的表单与上面的屏幕截图相同意味着我只希望我的datagridview可见,并且我不希望表单标题及其边框。
您可以使用以下代码创建自己的PopupForm。
要删除边框使用FormBorderStyle
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
然后把你的DataGridView和你的按钮那样:
使用的DataGridView的Dock属性来填写表格:
yourDataGridViewControl.Dock = DockStyle.Fill;
将您的按钮放在右上角并创建一个EventHandler以捕捉Click-Event:
button_close.Click += button_close_Click;
private void button_close_Click(object sender, EventArgs e)
{
this.Close();
}
在你的MainForm: 创建以下两个领域:
PopupForm popup; //PopupForm is the name of your Form
Point lastPos; //Needed to move popup with mainform
使用下面的代码,以显示你在按钮的位置弹出:
void button_Click(object sender, EventArgs e)
{
if(popup != null)
popup.Close(); //Closes the last open popup
popup = new PopupForm();
Point location = button.PointToScreen(Point.Empty); //Catches the position of the button
location.X -= (popup.Width - button.Width); //Set the popups X-Coordinate left of the button
location.Y += button.Height; //Sets the location beneath the button
popup.Show();
popup.TopMost = true; //Is always on top of all windows
popup.Location = location; //Sets the location
if (popup.Location.X < 0) //To avoid that the popup
popup.Location = new Point(0, location.Y); //is out of sight
}
创建一个事件处理程序捕捉MainForm的Move-Event并使用以下方法将您的弹出窗口移动到MainForm(Credit goes to Hans Passant):
private void Form1_LocationChanged(object sender, EventArgs e)
{
try
{
popup.Location = new Point(popup.Location.X + this.Left - lastPos.X,
popup.Location.Y + this.Top - lastPos.Y);
if (popup.Location.X < 0)
popup.Location = new Point(0, popup.Location.Y);
}
catch (NullReferenceException)
{
}
lastPos = this.Location;
}
在这里你可以得到Demoproject:LINK
凡到底是什么问题?你可以用'form.Show()'显示你的表单,然后设置它的位置。 –
@Jens Kloster我想弹出与上面的屏幕截图相同的内容。如果我将这个datagridview添加到窗体窗体中,它会向我显示窗体的标题栏和窗体的边框,所以我希望弹出窗口与屏幕截图完全相同。 –