1

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

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 的右边界/边框中,项目的文本会被剪切。所以要看到它,我想以某种方式将它向下移动一行,或者需要多少行才能显示项目文本/名称的其余部分

所以它会算作一个项目,但如果需要在某些行上。

更新:

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

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 是列表框长度(宽度) 在这一行中,我尝试计算并获取标记 [1] 的文本“

string tt = tokens[1].Substring(t - tokens[1].Length);

但是我得到一个错误 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");
                        }

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

4

1 回答 1

1

您不能使用标准列表框执行此操作。您需要创建自己的多行列表框。请参阅此链接以获取一些指导。 http://www.codeproject.com/Articles/2695/An-editable-multi-line-listbox-for-NET

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

于 2013-02-19T22:38:00.547 回答