1、有两个窗体F1和F2
2、F1有一个按钮,单击后打开F2.
3、F2中有一条滚动条,当我用MOUSE点击或拖动滚动条时,要使到F1保持焦点,而且F2不能获得焦点.其实我就是想做一个自定义的COMBOBOX.可以这样做吗?
2、F1有一个按钮,单击后打开F2.
3、F2中有一条滚动条,当我用MOUSE点击或拖动滚动条时,要使到F1保持焦点,而且F2不能获得焦点.其实我就是想做一个自定义的COMBOBOX.可以这样做吗?
解决方案 »
- SQL Server查询语句
- asp.net2008(C#)如何设置团队开发?
- 如何程序控制打开一个excel文件,并定位到某一行
- 一个超级 textbox 的 Bug
- 这个函数中的参数是什么类型的?参数如何传递的?
- 关于用ole_db连接oracle出现中文为???的问题?
- 请教线程问题
- DataGrid样式的问题,在线等!
- 想用XML保存某一个treeview的所有内容,但是xml里又不允许一些特殊字符,如何解决?
- (发表看法者一律有分)一个2002年毕业的程序员的半年的工作经历,和他的疑惑,请有工作经验的兄弟指点迷津
- C#中如何用钩子取得键盘消息
- ***讨论方案:如何对更新的内容作历史纪录?***
Button1_Click事件:
if(this.Panel1.Visible == false)//避免重复处理
{
this.Panel1.Location = new Point(x,y);//初始化显示位置
this.Panel1.Visible = true;
this.Panel1.Focus();
//....
}Panel1_Leave事件:
this.Panel1.Visible = false;//移除焦点后自动隐藏我有此类似功能就是这样做的。
我有一些类似的东西也是这样做的
我记得在VB6的解决方法是用一个picture控件,要显示时调用下面代码:
当中setwindowlog和 setparent是API函数 SetWindowLong pic.hwnd, GWL_EXSTYLE, WS_EX_TOOLWINDOW
SetParent pic.hwnd, 0 ClientToScreen text.hwnd, i
pic.Left = Screen.TwipsPerPixelX * i.X
pic.Top = Screen.TwipsPerPixelY * i.Y + text.Height
pic.Visible = True不知道.net可不可以这样去做?但我不想用API,最好能用.net中现成的类函数.
class F1:Form
{
public static void Main()
{
F2.ShowWin(this);//把F1实例传过去
}
}class F2: Form
{
private F2 frm_F1;
//注意这里是静态方法
public static void ShowWin(F1 frm)
{
F2 theForm = new F2();
theForm.TopMost = true;
theForm.Show();
frm_F1 = frm;
}//下面定义你的滚动条被点击事件
private void OnScroll(....)
{
frm_f1.Focus();
}
}
代码简单,理解抽象。
下面详细代码可以直接利用:
1、有两个窗体Form1和Form2
2、Form1有一个按钮,单击后打开Form2.
3、Form2中有一条滚动条,当你用MOUSE点击或拖动滚动条时,要使到Form1保持焦点,而且Form2不能获得焦点.Form1代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication1
{
public delegate void SetFocus(object sender,System.EventArgs e); /// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1:System.Windows.Forms.Form
{ #region Windows 窗体设计器生成的代码 private System.Windows.Forms.Button button1;
private static System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "启动Form2";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
textBox1.Location = new System.Drawing.Point(80, 16);
textBox1.Name = "textBox1";
textBox1.TabIndex = 2;
textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(280, 94);
this.Controls.Add(textBox1);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
Application.Run(new Form1());
} #endregion public static event SetFocus OnSetFocus; private void Form1_Load(object sender, System.EventArgs e)
{
OnSetFocus +=new WindowsApplication1.SetFocus(Form1_OnSetFocus);
} public void button1_Click(object sender, System.EventArgs e)
{
try
{
Form f = new Form2();
f.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} public static void Form1_OnSetFocus(object sender, EventArgs e)
{
try
{
textBox1.Text = "Choose";
textBox1.Focus();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}Form2代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace WindowsApplication1
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
#region Windows 窗体设计器生成的代码
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.trackBar1 = new System.Windows.Forms.TrackBar();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(8, 48);
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(248, 45);
this.trackBar1.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(264, 17);
this.label1.TabIndex = 2;
this.label1.Text = "触发Form1.TextBox1,并且使之获得焦点!!!";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(272, 126);
this.Controls.Add(this.label1);
this.Controls.Add(this.trackBar1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false); }
#endregion private void Form2_Load(object sender, System.EventArgs e)
{
this.trackBar1.Scroll += new System.EventHandler(Form1.Form1_OnSetFocus);
}
}
}上面的我刚调适过的,做程序要严谨!!!