1、有两个窗体F1和F2
2、F1有一个按钮,单击后打开F2.
3、F2中有一条滚动条,当我用MOUSE点击或拖动滚动条时,要使到F1保持焦点,而且F2不能获得焦点.其实我就是想做一个自定义的COMBOBOX.可以这样做吗?

解决方案 »

  1.   

    不要这样处理,你可以在F1窗体中放个Panel,里面再放滚动条,并设置初始Visable = false
    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;//移除焦点后自动隐藏我有此类似功能就是这样做的。
      

  2.   

    3tzjq(永不言弃) 的方法不错
    我有一些类似的东西也是这样做的
      

  3.   

    对了,可能会用到panel.BringToFront()方法
      

  4.   

    TO:3tzjq(永不言弃)  不行不行.大家有没有注意到ComboBox,如果你把它放到窗体下边,你单击button后,Panel是可以超出窗体边界显示的.所以这个Panel.Parent不会是这个窗体.
    我记得在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中现成的类函数.
      

  5.   

    楼主,这问题容易解决啊。F1打开F2,当F2被点击后,马上设置F1.Focus(),不过,要设置F2.TopMost = true;伪代码
    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();
    }
    }
      

  6.   

    http://img117.photo.163.com/sezjq/13984622/326966135.jpg
      

  7.   

    明显的委托处理问题么? 搂主赶紧补充这方面的技能~~~~   
    代码简单,理解抽象。
    下面详细代码可以直接利用:
    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);
    }
    }
    }上面的我刚调适过的,做程序要严谨!!!
      

  8.   

    TO: 3tzjq(永不言弃) 我的panel为什么不能超出窗体边界?你的就行? 好象是不太可能的吧.panel.parent是不是要设为其它??