简体   繁体   English

强制用户仅粘贴有效文件夹目录,并稍后使用C#在TextBox控件中禁用编辑目录

[英]Force user to paste only valid folder directory and disable editing directory later in TextBox control using C#

I am working on Windows Form Application and have created a TextBox control and a Browse button control, so that user can select a folder through Button and show directory in TextBox. 我正在Windows窗体应用程序上工作,并创建了一个TextBox控件和一个Browse按钮控件,以便用户可以通过Button选择一个文件夹并在TextBox中显示目录。

I want to give a freedom to the user to paste a directory path directly into the TextBox. 我想让用户自由将目录路径直接粘贴到TextBox中。 However, at the same, user must provide only a valid directory as a string/text in TextBox. 但是,与此同时,用户必须仅在TextBox中提供有效目录作为字符串/文本。

In addition, I want to disable editing this directory either by keyboard or any other possible way. 另外,我想通过键盘或任何其他可能的方式禁用编辑此目录。 The user will be able to paste a new valid directory anytime but can't edit it in the TextBox. 用户将可以随时粘贴一个新的有效目录,但不能在TextBox中对其进行编辑。

Is there any way to do this using C# at runtime? 有什么方法可以在运行时使用C#做到这一点吗?

Thanks. 谢谢。

If you set the textbox to ReadOnly then that stops editing the TextBox . 如果将文本框设置为ReadOnly则停止编辑TextBox

Then add an event for KeyDown you can capture if Ctrl + V is pressed and then action based on that using the Clipbboard class and if the directory is valid set the TextBox.Text . 然后为KeyDown添加一个事件,如果Ctrl + V则可以捕获该事件,然后使用Clipbboard类基于该事件进行操作,并且如果目录有效,则设置TextBox.Text

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
  if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
  {
    var clipboard = Clipboard.GetText();
    if (Directory.Exists(clipboard))
      textBox1.Text = clipboard;
  }
}

I want to give a freedom to the user to paste a directory path directly into the TextBox. 我想让用户自由将目录路径直接粘贴到TextBox中。 However, at the same, user must provide only a valid directory as a string/text in TextBox. 但是,与此同时,用户必须仅在TextBox中提供有效目录作为字符串/文本。

In addition, I want to disable editing this directory either by keyboard or any other possible way. 另外, 我想通过键盘或任何其他可能的方式禁用编辑此目录 The user will be able to paste a new valid directory anytime but can't edit it in the TextBox. 用户将可以随时粘贴一个新的有效目录,但不能在TextBox中对其进行编辑。

Isn't it contradictory ? 这不是矛盾的吗? Why don't you use two text boxes then ? 那为什么不使用两个文本框呢? First for user to input/paste, with onchange event catching that calls a method that checks the directory's path is correct. 首先供用户输入/粘贴,然后通过onchange事件捕获调用调用检查目录路径是否正确的方法。 If it is, the method copies it to the second text box, not editable, that is also linked to your directory browser. 如果是,该方法会将其复制到第二个不可编辑的文本框,该文本框也链接到您的目录浏览器。

You could handle the TextChanged event and check to see if the folder is valid and accessible there; 您可以处理TextChanged事件,并检查该文件夹是否有效并且可以在其中访问; if it's not, undo their change. 如果不是,请撤消其更改。 This might not be practical if they can edit the textbox text, as it'll check to see if the folder exists after every keystroke and so typing anything new will be impossible. 如果他们可以编辑文本框文本,这可能不切实际,因为它将在每次击键后检查文件夹是否存在,因此将不可能输入任何新内容。 This might be how you want it to behave however! 但是,这可能就是您希望其表现的方式!

To do the checking if the path is valid, I'd use Path.GetFullPath - pass it your textbox text and it'll throw an exception if the path doesn't exist/isn't valid/you don't have permission to access it. 要检查路径是否有效,我将使用Path.GetFullPath将其传递给您的文本框文本,如果路径不存在/无效/您没有权限访问该路径,则会抛出异常访问它。

If this feels a bit messy to you, instead of giving them the ability to edit the textbox, you could have a button that sets the text from the clipboard using Clipboard.GetText() and then performs your checks. 如果您觉得这有点麻烦,您可以使用按钮Clipboard.GetText()设置剪贴板中的文本,然后执行检查,而不是让他们能够编辑文本框。

Thanks all for suggestions and answers. 感谢所有的建议和答案。 Here is something that I did and worked like a charm for me. 这是我做过的事情,对我来说就像是一种魅力。 I hope, this will help later for someone if face the same problem. 我希望这对以后遇到相同问题的人有所帮助。

It is the modified version of the original code posted here by LukeHennerley. 它是LukeHennerley在此处发布的原始代码的修改版本。 Thanks LukeHennerley for that. 感谢LukeHennerley。

private void txtBoxTargetDir_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
        {
            var clipboard = Clipboard.GetText();
            if (Directory.Exists(clipboard))
            {
                txtBoxTargetDir.Clear();
                txtBoxTargetDir.Text = clipboard;
                txtBoxTargetDir.ReadOnly = true;
            }
            else
            {
                txtBoxTargetDir.Clear();
                txtBoxTargetDir.Text = "It's not a valid directory. Please provide a valid directory.";
            }
        }
    }

You will have to add this event in Form.Designer.cs first. 您必须首先在Form.Designer.cs中添加此事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM