在两列中为C#中的工具提示显示字符串 [英] display string in two columns for a tooltip in C#

查看:70
本文介绍了在两列中为C#中的工具提示显示字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组,如book-1,book-2,book-3,book-4,......

我想将上述字符串显示为datagridview的工具提示中的两列

书1书4
书2书5
书3书6


到目前为止,

我随地吐痰的字符串能够显示为单个列中

书1
图书2
书3
书4

任何帮助或提示,将不胜感激. :)

i have a array of strings as book-1,book-2,book-3,book-4,......

i want to show the above strings as in tooltip of datagridview in two columns

book-1 book-4
book-2 book-5
book-3 book-6


As so far,

i had spitted the strings was able to display as in a single columns

book-1
book-2
book-3
book-4

Any helps or hints would be appreciated. :)

推荐答案

为此,您必须进行一些格式化

空白不是很有用

为了在两列中实现文本,您需要计算出需要在每行的两列之间添加多少个选项卡.因为第一行的第一单元格可能太短,而第一行的第一单元格可能太长.因此,只要在每行的两列之间添加一个制表符,就可以得到这种格式的格式

b1 book1
gudbooks bok2
书籍勇敢的书籍
dontumindit myStuff
haythatscooliloveit真的

原因是每一行的第一列长度不同
要解决此问题,您需要确定要添加的最大文本长度是多少,对于每一行,您需要确定要添加多少个选项卡d

In order to this u have to do a bit of formatting

white spaces are not so helpful

in order to implement the text in two columns u need to work out how many tabs u need to add between two columns of each row. Because 1st cel of 1st row might be too short and same of 1nd row might b too long. So just adding one tab in between two columns of each row will end up some this kind of formatting

b1 book1
gudbooks bok2
Books braveBooks
dontumindit myStuff
haythatscooliloveit Really

the reason is that 1st column of each row differs in length
to over come this problem, u need to work out whats the maximum length of the text which is going to be added, and for each row u need to work out how many tabs to be addedd

private void Form1_Load(object sender, EventArgs e)
{
    //strings with different lengths
    string[] arr = { "b1", "book1", "gudbooks", "bok2", "Books", "braveBooks", "dontumindit", "myStuff", "haythatscool,iloveit", "Really" };
    int maxlen = 0;

    // calculating maximum length only for 1st cell items
    // that is items on all even places in array
    for (int i = 0; i < arr.Length; i++)
    {
        if (i % 2 == 0)
            if (maxlen < arr[i].Length)
                maxlen = arr[i].Length;
    }
    MessageBox.Show(maxlen.ToString());
    // Minimum No of Tabs which wil be added to maximum length cell
    int minimumTabSpace = 2;

    //getting Number of Tabs To overcome maxlength text
    int noOftabs = (maxlen / 8) + minimumTabSpace;
    string ToolTipText = "";

    for (int i = 0; i < arr.Length; i++)
    {
        ToolTipText += arr[i];
        if (i % 2 != 0)
        {
            //Insering New Line After every Two Cells
            ToolTipText += Environment.NewLine;
        }
        else
        {
            //Determining no. of Tabs for each Row
            //and inserting after 1st cell of each Row
            for (int j = 0; j < noOftabs - (arr[i].Length / 8); j++)
                ToolTipText += "\t";
        }
    }
    toolTip1.SetToolTip(button1, ToolTipText);
}



如果文本中的空格过多,那么您需要付出更多的努力才能获得适当的称赞.

祝您好运.



if u have too many white spaces in the text, then u need to work more to get proper allignment.

Best of Luck.


使用ToolTip.SetToolTip(Control, string).消除一些可能引起混淆的一个想法是:单个工具提示实例为多个Control的一个实例设置工具提示文本.
请参阅MDSN帮助页面上的代码示例:
http://msdn.microsoft.com/en-us/library/system. windows.forms.tooltip.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.settooltip.aspx [ ^ ].

—SA
Use ToolTip.SetToolTip(Control, string). One idea to remove some possible confusing is: a single tool tip instance set tool tip text for more than one instance of a Control.

See code samples on MDSN help page:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.settooltip.aspx[^].

—SA


我现在想不出更好的方法,但这肯定会有所帮助:

I can''t think of a better way now but this will sure help:

string ms = nyArray.ElementAt(0) + " " + nyArray.ElementAt(3) + Environment.NewLine +
                nyArray.ElementAt(1) + " " + nyArray.ElementAt(4) + Environment.NewLine +
                nyArray.ElementAt(2) + " " + nyArray.ElementAt(5);

ToolTip tt = new ToolTip();
tt.Show(ms, this.datagridview);



上面的代码将产生:



The code above will produce:

sanomama写道:
sanomama wrote:

book-1 book-4

book-2 book-5

book-3 book-6

book-1 book-4

book-2 book-5

book-3 book-6



注意:可能会有更好的方法来进行这种类型的格式化,仅谷歌"C#中的字符串格式"



Note: There may be a better way to do this type of formatting, just google "String Formatting in C#"


这篇关于在两列中为C#中的工具提示显示字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆