このコードは新しいフォームにあり、テキストをtextBoxに追加してから、リストボックスにアイテムとして追加します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GatherLinks
{
public partial class ChangeLink : Form
{
public ChangeLink()
{
InitializeComponent();
}
public string getText()
{
return textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
DialogResult = DialogResult.OK;
}
else
{
}
}
private void ChangeLink_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
}
}
}
そして、これは私がリストボックスにテキストを追加する方法です:
private void button2_Click(object sender, EventArgs e)
{
cl = new ChangeLink();
cl.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = cl.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
cl.Close();
}
else if (dr == DialogResult.OK)
{
label4.Text = cl.getText();
mainUrl = cl.getText();
if (!LocalyKeyWords.ContainsKey(mainUrl))
{
newUrl = true;
KeysValuesUpdate();
}
else
{
newUrl = false;
KeysValuesUpdate();
}
OptionsDB.set_changeWebSite(cl.getText());
cl.Close();
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
}
問題は、テキストが非常に長いため、listBoxの右境界/境界でアイテムのテキストが切り取られることです。それを確認するには、どういうわけかそれを1行下に移動するか、アイテムの残りのテキスト/名前を表示するために必要な行数を確認します
したがって、1つのアイテムとしてカウントされますが、必要に応じて一部の行でカウントされます。
アップデート:
これは、アプリケーションを実行しているときにアイテムをリストボックスにロードする部分です。
private void ListBoxLoadKeys(Dictionary<string, List<string>> dictionary, string FileName)
{
int t = listBox1.Width;
using (StreamReader sr = new StreamReader(FileName))
{
while ((line = sr.ReadLine()) != null)
{
int i = line.Count();
tokens = line.Split(',');
dictionary.Add(tokens[0], tokens.Skip(1).ToList());
string tt = tokens[1].Substring(t - tokens[1].Length);
data.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
}
}
listBox1.DataSource = data;
}
したがって、変数tはlistBox Length(width)です。この行で、tokens[1]のテキストを計算して取得しようとしました。"
string tt = tokens[1].Substring(t - tokens[1].Length);
しかし、エラーが発生した場合、startIndexは文字列の長さより大きくすることはできません
これでリストボックスの長さ(幅)がわかりましたが、テキストトークン[1]をリストボックスの範囲外の場合にのみ新しい行に入れたくありません幅(長さ)
どうすればこれを修正して実行できますか?
再度更新:
コードを再度変更し、最初に変数データの長さの各行が変数tよりも大きいかどうかを確認しようとしています。
private void ListBoxLoadKeys(Dictionary<string, List<string>> dictionary, string FileName)
{
int t = listBox1.Width;
using (StreamReader sr = new StreamReader(FileName))
{
while ((line = sr.ReadLine()) != null)
{
int i = line.Count();
tokens = line.Split(',');
dictionary.Add(tokens[0], tokens.Skip(1).ToList());
//string tt = tokens[1].Substring(t - tokens[1].Length);
data.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
if (data[i].Length > t)
{
MessageBox.Show("big");
}
}
}
listBox1.DataSource = data;
}
if (data[i].Length > t)
{
MessageBox.Show("big");
}
しかし、エラーが発生しました:インデックスが範囲外でした。負ではなく、コレクションのサイズ未満である必要があります