C# 如果列表框中的某个项目超出了右侧的边界,那么它将向下移动一行,我如何才能做到这一点?

C# 如果列表框中的某个项目超出了右侧的边界,那么它将向下移动一行,我如何才能做到这一点?,c#,C#,我有一个新表单中的代码,我将文本添加到文本框中,然后将其作为项目添加到列表框中: 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 {

我有一个新表单中的代码,我将文本添加到文本框中,然后将其作为项目添加到列表框中:

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;
            }
        }
问题是,如果文本很长,那么在列表框的右边界/边框中,该项的文本将被剪切。所以为了看到它,我想把它向下移动一行,或者多少行需要显示项目文本/名称的其余部分

因此,它将被视为一个项目,但如果在某些行上需要的话

更新:

这是我在运行应用程序时将项目加载到列表框的部分:

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;
        }
但我收到一个错误startIndex不能大于字符串的长度

现在我知道了列表框的长度(宽度),但我不想把文本标记[1]放在新行中,只要它超出了列表框宽度(长度)的界限

我如何修复并执行此操作

再次更新:

再次更改代码现在我尝试首先检查变量数据lngth中的每一行是否大于变量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");
                        }
private void ListBoxLoadKeys(字典、字符串文件名)
{
int t=列表框1.宽度;
使用(StreamReader sr=新StreamReader(文件名))
{
而((line=sr.ReadLine())!=null)
{
int i=line.Count();
tokens=line.Split(',');
dictionary.Add(tokens[0],tokens.Skip(1.ToList());
//字符串tt=tokens[1]。子字符串(t-tokens[1]。长度);
data.Add(“Url:“+tokens[0]+”--“+”Localy关键字:“+tokens[1]);
if(数据[i]。长度>t)
{
MessageBox.Show(“大”);
}
}
}
listBox1.DataSource=数据;
}
if(数据[i]。长度>t)
{
MessageBox.Show(“大”);
}

但我得到一个错误:索引超出范围。必须为非负数且小于集合的大小

使用标准列表框无法执行此操作。您需要创建自己的多行列表框。请参阅此链接以获取一些指导


我已经在工作中实现了这一点(忽略了作者糟糕的配色方案选择),而且效果很好。

标准列表框无法实现这一点。您需要创建自己的多行列表框。请参阅此链接以获取一些指导


我在工作中实现了这一点(忽略了作者糟糕的配色方案选择),效果很好。

也许可以为每个项目添加自动分组框或网格或类似的内容?如果您知道您指定的列表框长度,只需使用子字符串函数来包装文本。更新了我的问题!也许要为每个项目添加一个自动分组框或网格或类似的内容?如果您知道您指定的列表框长度,只需使用子字符串函数来包装文本。更新了我的问题!很高兴看到在两行XAML中完成的事情需要在winforms.Yep中进行可怕的黑客攻击。糟透了,不是吗。在过去的18个月里,我很不幸地参与了一个WinForms项目,这非常糟糕。很高兴看到一件可以在两行XAML中完成的事情需要在WinForms中进行可怕的黑客攻击。是的。糟透了,不是吗。在过去的18个月里,我不幸地参与了一个WinForms项目,情况非常糟糕。
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");
                        }