简体   繁体   English

c#将字符串从公共Form1传递给Private void

[英]c# Pass string from public Form1 to Private void

I have always used invisible textboxes to pass data in my applications. 我一直使用不可见的文本框在应用程序中传递数据。 Now I wanna stop this. 现在我想停止这个。 So I need to learn how to do this. 所以我需要学习如何做到这一点。 And right now I'm stuck at the problem passing a string from public Form1() to a private void richTextBox1_KeyDown. 现在,我陷入了将字符串从公共Form1()传递到私有void richTextBox1_KeyDown的问题。 I'm not sure how the mechanism works for passing strings. 我不确定传递字符串的机制。

Here is my code: 这是我的代码:

public Form1(string file)
{
    InitializeComponent();
    StreamReader sr = new StreamReader(file);
    string filetext = sr.ReadToEnd();
    richTextBox1.Text = filetext;
    string TITLE = file.Split(new string[] { @"\" }, StringSplitOptions.None).Last();
    this.Text = TITLE;
} 

String file will contain something like this if you do: "open with (application)": C:\\Users\\Administrator\\Downloads\\text.tyx If opened the raw .exe file, the string will be empty. 如果执行此操作,字符串文件将包含以下内容:“使用(应用程序)打开”:C:\\ Users \\ Administrator \\ Downloads \\ text.tyx如果打开了原始.exe文件,则该字符串将为空。

Here is the part I want to catch if string file is empty or not: 如果字符串文件为空,这是我要捕获的部分:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.S && e.Control)
    {
        if (Form1(???) != "")
        {
            richTextBox1.SaveFile(Form1(???), RichTextBoxStreamType.PlainText);
        }
    }
}

I put "???" 我放 ”???” where I have no clue on what to do. 我不知道该怎么办。 Please help 请帮忙

Save fileName as member variable and use it everywhere. 将fileName保存为成员变量,并在各处使用它。 Read file using File.ReadAllText(_fileName) : 使用File.ReadAllText(_fileName)读取文件:

private readonly string _fileName;

public Form1(string fileName)
{
  InitializeComponent();
  _fileName = fileName;
  richTextBox1.Text = File.ReadAllText(_fileName);
  Text = _fileName.Split(new[] {@"\"}, StringSplitOptions.None).Last();
}

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.S && e.Control)
  {
    if (_fileName == "")
    {
      richTextBox1.SaveFile(_fileName, RichTextBoxStreamType.PlainText);
    }
  }
}

As you show it, your Form1 has a constructor that get a string as parameter. 如您所显示的,您的Form1具有一个构造函数,该构造函数将字符串作为参数。 Therefore the code that instantiate your Form1 may look something like: 因此,实例化Form1的代码可能类似于:

Form1 form1 = new Form1("Some String");
form1.Show();  //or form1.ShowDialog();

In order to use the string file along your Form1 code, you can define a member variable to the Form1 class as following: 为了在Form1代码中使用字符串文件,可以为Form1类定义一个成员变量,如下所示:

class Form1:Form
{
    private string _file;

    public Form1(string file)
    {
        InitializeComponent();
        _file= file;

        // Your code
    }

} }

Now _file is known all along you Form1 and you can use it: 现在,_file在您的Form1中一直是众所周知的,您可以使用它:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.S && e.Control)
    {
        if (!string.IsNullOrEmpty(_file))
        {
            richTextBox1.SaveFile(_file, RichTextBoxStreamType.PlainText);
        }
    }
}

Regarding the part about wanting to stop using invisible text boxes, I suggest you look into data binding and decoupling business logic and form logic. 关于希望停止使用不可见文本框的部分,建议您研究数据绑定以及将业务逻辑和表单逻辑分离。

But with minimal changes, as the others have stated, you can just store state (the name of the file) on Form1 so it can be accessed in the KeyDown event. 但是,正如其他人所言,只需进行最小的更改,您就可以将状态(文件名)存储在Form1上,以便可以在KeyDown事件中对其进行访问。

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

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