【问题标题】:C# WinForms cannot set focusC# WinForms 无法设置焦点
【发布时间】:2012-07-02 13:28:47
【问题描述】:

我目前面临的问题是,当我尝试将焦点设置在某个控件(文本框)上时,什么都没有发生,也许我只是忽略了一些东西。(在某个地方我发现焦点是“低级”方法并且 select() 应该可以使用,但是效果不好)

从表单登录,我启动 EncryptPSW 表单的新实例

private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        EncryptPSW ePSW = new EncryptPSW();
        ePSW.setOsLog(false, this);
        ePSW.ShowDialog();        
    }

在按钮(位于 EncryptPSW 表单上)单击事件我调用填充方法

public void fill()
    {
        if (textBoxPSW.Text.Length == 8)//psw has to be 8 chars long
        {
            if (save)//determinating whether save or fetch of data should be done
            { login.launchSave(textBoxPSW.Text,this); }
            else { login.launchOpen(textBoxPSW.Text,this); }
        }
        else { MessageBox.Show("The password must contain 8 characters");}
    }

从登录启动保存或打开方法(我的问题只是打开,因为在保存期间我不需要对焦点做任何事情)

public void launchOpen(string psw,EncryptPSW ePSW)
    {

        ePSW.Close();
        Encryptor.DecryptFile("loggin.bin", psw, this); //decrypting data and setting textBoxes Text property into the fetched ones
        setFocus();

    }

所有工作完成后,应该调用 setFocus() 来设置焦点和其他属性。

public void setFocus()
    {
        textBoxDatabase.Focus();
        textBoxDatabase.SelectionStart = textBoxDatabase.TextLength - 1;
        textBoxDatabase.SelectionLength = 0;
    }

我尝试了很多不同的方法,比如:

从 EncryptPSW_FormClosed 中调用 setFocus() 在 EncryptPSW 关闭后调用整个打开过程(从 EncryptPSW_FormClosed 内部) 还有很多,不过我都不记得了。

在 Form_Closed 的情况下,奇怪的是,当我试图从那里显示一个消息框而不是设置焦点时(只是为了看看问题可能出在哪里),它会在 EncryptPSW 表单关闭之前显示。

我对此的唯一猜测是 EncryptPSW 的实例以某种方式阻止了登录表单及其控件

我希望我把我的问题描述得足够好,并且至少有点道理;]

提前致谢,

问候,

蕾丝

【问题讨论】:

  • 这不是问题。你的 textBoxDatabase 在哪里??
  • 调用 Focus() 时文本框是否可见?
  • 那个文本框位于登录表单上,是的,它是可见的

标签: c# winforms focus


【解决方案1】:

由于文本框在登录表单中,并且您正在从它作为对话框(子)打开 EcryptPWS,您的登录表单将无法将焦点设置为任何内容。关闭后您需要设置焦点。你可以这样做:

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    using(EncryptPSW ePSW = new EncryptPSW())
    {
       ePSW.setOsLog(false, this);
       if (ePSW.ShowDialog() == DialogResult.OK)
       {
           textBoxDatabase.Focus(); 
       }
    }
} 

public void launchOpen(string psw,EncryptPSW ePSW) 
{ 
    ePSW.DialogResult = DialogResult.OK;
    ePSW.Close(); 
    Encryptor.DecryptFile("loggin.bin", psw, this); //decrypting data and setting textBoxes Text property into the fetched ones 
} 

【讨论】:

  • 当用户关闭 EncryptPSW 表单时(他点击错误、改变主意或 w/e)我不想改变焦点
  • @Releis 对“改变主意或 w/e”的意思有点困惑
  • 用户点击他想打开存储的帐户信息,弹出psw请求表单,但是,他可能决定不这样做,或者他记得他点击错了,他实际上不想这样做或任何其他关闭该表单的原因。现在清楚了吗?;]
  • 抱歉,有一段时间没来了。也可以,tvym :]
【解决方案2】:

好吧,这可能是我看到的最丑的东西了。

使用

public void setFocus() 
    { 
        textBoxDatabase.Focus(); 
        textBoxDatabase.SelectionStart = textBoxDatabase.TextLength - 1; 
        textBoxDatabase.SelectionLength = 0; 
    } 

处更改您的代码
public void launchOpen(string psw,EncryptPSW ePSW)  
    {  

        ePSW.Close();  
        Encryptor.DecryptFile("loggin.bin", psw, this); //decrypting data and setting textBoxes Text property into the fetched ones  
        setFocus();  

    }  

delegate void settingfocus();
public void launchOpen(string psw,EncryptPSW ePSW)  
    {  

        ePSW.Close();  
        Encryptor.DecryptFile("loggin.bin", psw, this); //decrypting data and setting textBoxes Text property into the fetched ones  
    settingfocus sf = new settingfocus(setFocus);
    this.BeginInvoke(sf);

    }  

这对我有用

(对不起,显然认为在程序之前插入“this”,并将第 x 行更改为 this 是易读的)

【讨论】:

  • 也许我太笨了,但是你能用句子吗?我根本不明白你:x
  • 那里..如果现在没有意义,我放弃
  • 工作,谢谢。如果您不介意,请回答 2 个问题;] 1. 那里有什么难看的? 2. 你知道问题出在哪里吗?
  • 表单还没有焦点来完成这项工作,所以它需要排队。它很丑,因为我确定有更好的方法
  • 感谢您的解释。最后一个(希望它不会打扰你)你能指出或建议一些更好的方法吗?还是就像“我必须查看所有代码”?我喜欢人们告诉我做某事的更好方法..(这不是讽刺,我的意思是,我喜欢提高自己)
猜你喜欢
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多