C#richTextBox的一个保存问题

茗香淡然 茗香淡然 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
实例化的richTextBox 不能存储内容吗??

控件:
richTextBox1 comboBox1 button1

把下面的 public static void BindCom(RichTextBox txt,ComboBox com, string path)

改为

public static void BindCom(ComboBox com, string path)

去掉注释
//RichTextBox txt = new RichTextBox(); ← 注意这实例化

这样的话,保存内容是什么都没有,断点后看了一下,明明有内容的,却为何保存不了呢?求解惑!!!

//绑定下拉框数据
public static void BindCom(RichTextBox txt,ComboBox com, string path)
{
//RichTextBox txt = new RichTextBox(); ← 注意这实例化
string[] typeBooks;
if (!File.Exists(path))
{
typeBooks = new string[]{"选择类型","type0","type1",
"type2","type3","type4","type5","type6"};
int count = typeBooks.Count();
int len = 1;
foreach (var str in typeBooks)
{
txt.Text += str;

if (len < typeBooks.Count())
{ txt.Text += "\n"; len++; }
}
txt.SaveFile(path, RichTextBoxStreamType.PlainText);
}
FileStream fs = new FileStream(path,
FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr = new StreamReader(fs, Encoding.Default);
txt.Text = sr.ReadToEnd();
sr.Close();
sr.Dispose();
typeBooks = txt.Lines;
foreach (var str in typeBooks)
{
if (str != "")
com.Items.Add(str);
}
com.SelectedIndex = 0;
}


private void button1_Click(object sender, EventArgs e)
{
//typeBooks = new string[]{"选择类型","type0","type1",
// "type2","type3","type4","type5","type6"};
//int count = typeBooks.Count();
//int len = 1;
//foreach (var str in typeBooks)
//{
// richTextBox1.Text += str;
// if (len < typeBooks.Count())
// { richTextBox1.Text += "\n"; len++; }
//}
//richTextBox1.SaveFile("Type.txt", RichTextBoxStreamType.TextTextOleObjs);

BindCom(richTextBox1 ,comboBox1, "Type.txt");
}
...全文
评论 6
收藏 已收藏
分享
返回顶部
茗香淡然
C# 111,129 成员 642,540 内容