首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将文本字段作为string[]

如何将文本字段作为string[]
EN

Stack Overflow用户
提问于 2022-04-13 16:16:59
回答 1查看 74关注 0票数 -1

因此,我正在编写一个c#程序,它将返回在文本框中选择的文本文件string[]。

但是,当尝试这样做时,它会出现一个错误,即“静态本地函数不能包含对此或基的引用”。

下面是收集文本框字符串的代码

代码语言:javascript
复制
         string[] words = { File.ReadAllText(dicuploadname.Text)

这是出现错误的地方。所涉文本框的代码如下:

代码语言:javascript
复制
Private void uploaddict_Click(object sender, EventArgs e)

    {

        OpenFileDialog fdlg = new OpenFileDialog();

        MessageBox.Show("The dictionary will need to be a .txt file.");

        fdlg.Title = "Select your dictionary file";

        fdlg.InitialDirectory = @"c:\";

        fdlg.Filter = "Text files (*.txt)|*.txt";

        fdlg.FilterIndex = 2;

        fdlg.RestoreDirectory = true;

        if (fdlg.ShowDialog() == DialogResult.OK)

        {

            dicuploadname.Text = fdlg.FileName;

        }

    }



   



    public static void dicuploadname_TextChanged(object sender, EventArgs e)

    {



    }

    public string text = dicuploadname.Text;

}

}

还包括浏览按钮的代码,该按钮选择文本文件并将其位置粘贴到文本框中。

不确定如何将文本文件的内容作为string[]返回?谢谢。

最新代码:

代码语言:javascript
复制
else
        {
            var myInt = int.Parse(min.Text);
            int seconds = myInt * 60000;

            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(TimerMethod);
            aTimer.Interval = seconds;
            aTimer.Enabled = true;
            // Do something with t...
            string uriString = "https://open.spotify.com/search/";

            static void TimerMethod(object source, ElapsedEventArgs e)
            {
                string uriString = "https://open.spotify.com/search/";



                WebClient webClient = new WebClient();

                Random r = new Random();

                string words;

                words = File.ReadAllText(dicuploadname.Text);

                Console.WriteLine(words[r.Next(0, words.Length)]);


                string word = words[r.Next(0, words.Length)];

                NameValueCollection nameValueCollection = new NameValueCollection();
                nameValueCollection.Add(uriString, word);

                webClient.QueryString.Add(nameValueCollection);

                var spotifysearch = new ProcessStartInfo
                {
                    FileName = "https://open.spotify.com/search/" + word,
                    UseShellExecute = true
                };
                Process.Start(spotifysearch);
            }
        }
    }

    private void HiveMind_Click_1(object sender, EventArgs e)
    {
   
        var HiveMind= new HiveMind();
        HiveMind.Show();
    }

    private void uploaddict_Click(object sender, EventArgs e)
    {
        string[] words;
        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "Select your dictionary file";
        fdlg.InitialDirectory = @"c:\";
        fdlg.Filter = "Text files (*.txt)|*.txt";
        fdlg.RestoreDirectory = true;

        if (fdlg.ShowDialog() == DialogResult.OK)
            words = File.ReadAllText(fdlg.FileName).Split(' ');
    }
    

    public string words;

    private void dicuploadname_TextChanged(object sender, EventArgs e)
    {

    }

}
}
EN

回答 1

Stack Overflow用户

发布于 2022-04-13 20:13:24

查看发布的代码,dicuploadname不是一个变量,而是一个事件处理程序。将其更改为此,通过读取用户指定的文本文件并在空格字符上将其拆分,从而分配变量单词。

代码语言:javascript
复制
private void uploaddict_Click(object sender, EventArgs e)
    {
        string[] words;
        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "Select your dictionary file";
        fdlg.InitialDirectory = @"c:\";
        fdlg.Filter = "Text files (*.txt)|*.txt";
        fdlg.RestoreDirectory = true;

        if (fdlg.ShowDialog() == DialogResult.OK)
            words = File.ReadAllText(fdlg.FileName).Split(' ');
    }

在这段代码中,用户选择的文件在空格字符上被分割。如果您只想要一个字符串的代码,而不是每个单词都将单词声明更改为

代码语言:javascript
复制
string words;

和变量分配给的行。

代码语言:javascript
复制
words = File.ReadAllText(fdlg.Filename);

希望这能帮上忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71860708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档