第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

復(fù)選框刪除并在列表中創(chuàng)建一段文本

復(fù)選框刪除并在列表中創(chuàng)建一段文本

C#
慕森王 2022-12-24 09:48:26
在我的 C# 腳本中,有一個復(fù)選框列表,勾選后會向文本文件添加一行文本。取消選中時,從文件中刪除該段文本。我的問題是,我不知道代碼將如何識別要刪除的行,因為它們可以按任何順序排列。
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經(jīng)驗 獲得超8個贊

我寫了一個小的測試表格,可以做你想做的事。您可以用自己的字符串替換 SNIPPET1 和 SNIPPET2。我的表單上有 2 個復(fù)選框,每個復(fù)選框都會根據(jù)是否選中來添加或刪除片段。您可以修改代碼以滿足您的需要。


請注意,正如上面提到的評論者,您將需要使用 String.Replace() 函數(shù)通過用空白字符串替換它來從文件中刪除文本


public partial class Form1 : Form

    {

        private const string SNIPPET1 = "Hello world";

        private const string SNIPPET2 = "I love Stack";

        private const string FILENAME = "output.txt";


        private string OutputFile

        {

            get

            {

                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME);

            }

        }


        public Form1()

        {

            InitializeComponent();

        }


        private void checkBox1_CheckedChanged(object sender, EventArgs e)

        {

            if (checkBox1.Checked)

            {

                AddSnippet(SNIPPET1);

            }

            else

            {

                RemoveSnippet(SNIPPET1);

            }

        }


        private void checkBox2_CheckedChanged(object sender, EventArgs e)

        {

            if (checkBox2.Checked)

            {

                AddSnippet(SNIPPET2);

            }

            else

            {

                RemoveSnippet(SNIPPET2);

            }

        }


        private void AddSnippet(string snippet)

        {

            File.AppendAllText(OutputFile, snippet);

        }


        private void RemoveSnippet(string snippet)

        {

            // Read in the file

            var fileContents = File.ReadAllText(OutputFile);


            // Remove the snippet by replacing it with a blank string

            fileContents = fileContents.Replace(snippet, String.Empty);


            // Write file contents

            File.WriteAllText(OutputFile, fileContents);

        }

    }



查看完整回答
反對 回復(fù) 2022-12-24
  • 1 回答
  • 0 關(guān)注
  • 93 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號