还有个问题,请大家帮帮忙,谢谢了。大家都有分 我窗体上有40个groupbox,每个gb用得是相同的contextMenu,选择cm中得一项弹出个窗口,然后在窗口中的txt赋值后传回父窗体,再把值赋给gb中的lb,当我鼠标右击时,我怎么判断点击的是哪个gb呢,要知道是点击得哪个gb我才能把gb中的lb赋值啊。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 click中有个sender参数,就是你click中的gb 你把这个sender强制转换为gb,然后就可以使用了... 请问我要怎么强制转换呢private void menuItem1_Click(object sender, System.EventArgs e){ Form2 fr=new Form2(); if(fr.ShowDialog()==DialogResult.OK) { SortedList sl=fr.get_sl; foreach(Control c in this.Controls) { if( c is GroupBox ) { 就是在这里判断点击得是哪个gb,怎么判断啊 } } }} 我这样改了,可是转换错误,报“指定的转换无效”Form2 fr=new Form2();if(fr.ShowDialog()==DialogResult.OK){ SortedList sl=fr.get_sl; GroupBox gb=(GroupBox)sender;//这里错了 this.selectControl(gb,sl); } 用GroupBox gb=sender as GroupBox; 谢谢楼上几位朋友现在是转换成功了,但是无法用转换后gb的属性。我得界面上有40个gb,我是想右键点击一个得时候,能判断点击得是哪个gb,然后在把gb中得lb得text赋值,转换后又怎么做了,谢谢 GroupBox gb=(GroupBox)sender;switch(gb.name)不只可以不 GroupBox gb=(GroupBox)sender;这样转换错误转换后是新的gb,连名字都没有 谢谢大家,问题解决了this.contextMenu1.SourceControl;这样可以获得是哪个控件弹出的菜单。 楼上的做法统统不行,因为:System.Window.Forms.GroupBox gb = sender as GroupBox;这个时候得到的gb是个空类型,也就是说sender = MenuItem 。我的做法是这样的:在弹出右键菜单之前保存了当前鼠标的坐标,然后当另一个窗口关闭后,再用原来保存的坐标来判断是在哪个GroupBox中。再改掉它的Text。public string _strlb; //从另一个窗口传过来的TEXT [DllImport("user32.dll", EntryPoint="GetCursorPos")] public static extern int GetCursorPos ( ref System.Drawing.Point lpPoint ); [DllImport("user32.dll", EntryPoint="PtInRect")] public static extern bool PtInRect ( ref System.Drawing.Rectangle lpRect, int ptX, int ptY ); private void menuItem1_Click(object sender, System.EventArgs e) { Point mpt = new Point(); GetCursorPos(ref mpt); //保存当前鼠标的坐标 Form2 f2 = new Form2(this as Form1); f2.ShowDialog(); System.Drawing.Rectangle rect = new Rectangle();; foreach(System.Windows.Forms.Control ctl in this.Controls) { rect = System.Windows.Forms.Screen.GetWorkingArea(ctl); if (ctl is System.Windows.Forms.GroupBox) { if (PtInRect(ref rect,mpt.X,mpt.Y)) //判断当时弹出右键菜单时的坐标在哪个GroupBox里。 { ctl.Text = _strlb; break; } } } }但是这样做还有一个问题没有修改,就是永远只能改掉同一个GroupBox的Text。主要是判断点是否在控件内有问题,所以我把它提出来,也希望高手帮我改一下。 我已经解决了,就是用this.contextMenu1.SourceControl 楼主,我晕呀,我想尽了办法,你最后来一句cm.SourceControl解决了,哎无语。。 毕设的代码问题 谁帮我解答一下啊?每句话都什么意思啊 重绘一个button,如何保留此button的image和text啊? C# 怎么在sql数据库中插入一行数据 C#中,Application.Exit()和Close()有什么不同。 如何把DataGridView的某个单元格转化成Combox或时间控件等等 一个DLL包含多个函数,如何申明 C# 程序界面疑问 wcf服务怎么时时刷新每个客户端数据 在Tree中,如何实现动态对checkbox的使用,急呀,就等你们了............ 关于.NET区域性的问题 用C#如何访问有密码的Access 未能在类中找到资源
private void menuItem1_Click(object sender, System.EventArgs e)
{
Form2 fr=new Form2();
if(fr.ShowDialog()==DialogResult.OK)
{
SortedList sl=fr.get_sl;
foreach(Control c in this.Controls)
{
if( c is GroupBox )
{
就是在这里判断点击得是哪个gb,怎么判断啊
}
}
}
}
Form2 fr=new Form2();
if(fr.ShowDialog()==DialogResult.OK)
{
SortedList sl=fr.get_sl;
GroupBox gb=(GroupBox)sender;//这里错了
this.selectControl(gb,sl);
}
现在是转换成功了,但是无法用转换后gb的属性。
我得界面上有40个gb,我是想右键点击一个得时候,能判断点击得是哪个gb,然后在把gb中得lb得text赋值,转换后又怎么做了,谢谢
switch(gb.name)
不只可以不
this.contextMenu1.SourceControl;
这样可以获得是哪个控件弹出的菜单。
这个时候得到的gb是个空类型,也就是说sender = MenuItem 。
我的做法是这样的:在弹出右键菜单之前保存了当前鼠标的坐标,然后当另一个窗口关闭后,再用原来保存的坐标来判断是在哪个GroupBox中。再改掉它的Text。public string _strlb; //从另一个窗口传过来的TEXT [DllImport("user32.dll", EntryPoint="GetCursorPos")]
public static extern int GetCursorPos (
ref System.Drawing.Point lpPoint
); [DllImport("user32.dll", EntryPoint="PtInRect")]
public static extern bool PtInRect (
ref System.Drawing.Rectangle lpRect,
int ptX,
int ptY
); private void menuItem1_Click(object sender, System.EventArgs e)
{
Point mpt = new Point();
GetCursorPos(ref mpt); //保存当前鼠标的坐标 Form2 f2 = new Form2(this as Form1);
f2.ShowDialog(); System.Drawing.Rectangle rect = new Rectangle();;
foreach(System.Windows.Forms.Control ctl in this.Controls)
{
rect = System.Windows.Forms.Screen.GetWorkingArea(ctl); if (ctl is System.Windows.Forms.GroupBox)
{
if (PtInRect(ref rect,mpt.X,mpt.Y)) //判断当时弹出右键菜单时的坐标在哪个GroupBox里。
{
ctl.Text = _strlb;
break;
}
}
}
}但是这样做还有一个问题没有修改,就是永远只能改掉同一个GroupBox的Text。主要是判断点是否在控件内有问题,所以我把它提出来,也希望高手帮我改一下。