【问题标题】:Set focus on winform after start启动后将焦点设置在winform上
【发布时间】:2012-04-19 08:44:09
【问题描述】:

我发现了一些关于此的主题,但这些都不能帮助我解决我的问题,我想在新创建的 winform 窗口启动后将焦点设置在它上。

我在自己的新线程中开始表单:

application.Run(new InvisibleForm());

并出现窗体,但焦点仍设置在 windows 中最后一个选定的窗口上。这个表单没有标题栏,不在任务面板中看到,它还有一个 TransparencyKey 集:

this.AutoScaleDimensions = new SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.BackColor = SystemColors.AppWorkspace;
this.ClientSize = new Size(992, 992);
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InvisibleForm";
this.Opacity = 0.5;
this.ShowInTaskbar = false;
this.TransparencyKey = SystemColors.AppWorkspace;
this.Load += new EventHandler(this.InvisibleForm_Load);

现在我尝试了一些方法,但这些方法都没有让我专注于表单,或者可以将表单设置在所有其他窗口的前景/顶部:

this.TopMost = true;
this.Focus();
this.BringToFront();
this.Activate();

有没有办法以编程方式将点击/焦点事件触发到表单,以便它将此事件设置为焦点?

【问题讨论】:

  • 您在何处/何时设置所有这些属性?在 Shown 事件中尝试this.Focus()
  • 旁白:在自己的线程上运行表单是可能的,但很少是一个好主意。
  • 有趣,它对我有用。你在 this.InvisibleForm_Load 中做什么?
  • 我只看到了 this.show 并没有注意到我们也有 this.shown ...所以它还在工作 ;) 谢谢 dood

标签: c# .net winforms focus setfocus


【解决方案1】:

在表单的“显示”状态下使用Activate() 方法很重要,所以create a listener for the shown event 并在那里使用你的焦点/前端方法

【讨论】:

    【解决方案2】:

    你可以试试这个代码

    this.ShowDialog();
    

    上面的代码与 MessageBox 相同,注意。所以最后出现的表单必须关闭,才能再次访问另一个表单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      相关资源
      最近更新 更多