列表框宽度大小取决于文本长度

发布于 2024-08-13 22:23:29 字数 188 浏览 11 评论 0原文

我的应用程序有一个带有列表框的窗口,其中填充了随时间变化的文本,因此列表框条目可以有多个长度。

我想让窗口和列表框宽度根据列表框条目长度(以字符数为单位)动态更改。

举个例子,如果我的列表框有多个条目,并且最大长度为 30 个字符,我想让窗口及其列表框的宽度大于最大长度为 20 个字符的窗口。

最好的方法是什么?

My application has a window with a ListBox inside which is filled with text that changes over time, therefore Listbox entries can have several length.

I'd like to make the window and the listbox width to change dynamically dependent on the listbox entries length (in number of characters).

As an example, if my listbox has several entries and the maximum lenght is 30 characters i want to make the window and its listbox larger in width than one window that which maixum lenght is 20 characters.

What is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一口甜 2024-08-20 22:23:29

尝试这样的事情:

// find the longest item
CString longest;
for (int i = 0; i < m_list.GetCount(); ++i)
{
    CString temp;
    m_list.GetText(i, temp);
    if (temp.GetLength() > longest.GetLength())
        longest = temp;
}

// get the with of the longest item
CSize size = GetWindowDC()->GetTextExtent(longest);

// you need this to keep the current height
RECT rect;
m_list.GetWindowRect(&rect);

// change only width
int width = size.cx;
int height = rect.bottom - rect.top;
m_list.SetWindowPos(NULL, 0, 0, width, height, SWP_NOZORDER | SWP_NOMOVE);

Try something like this:

// find the longest item
CString longest;
for (int i = 0; i < m_list.GetCount(); ++i)
{
    CString temp;
    m_list.GetText(i, temp);
    if (temp.GetLength() > longest.GetLength())
        longest = temp;
}

// get the with of the longest item
CSize size = GetWindowDC()->GetTextExtent(longest);

// you need this to keep the current height
RECT rect;
m_list.GetWindowRect(&rect);

// change only width
int width = size.cx;
int height = rect.bottom - rect.top;
m_list.SetWindowPos(NULL, 0, 0, width, height, SWP_NOZORDER | SWP_NOMOVE);
瑾兮 2024-08-20 22:23:29

您使用什么编程平台?我猜是.NET 和VB。

放入一个方法来检查列表的内容并根据需要更改框和窗口的大小:

Dim intMaxLength As Integer = 20
For Each myItem As String In ListBox1.Items
    If Len(myItem) > intMaxLength Then  
       'Number of characters times number of pixels per character  
        ListBox1.Width = Len(myItem) * 10  
        'Me refers back to the form object  
        'Add a few extra pixels to give space around your listbox  
        Me.Width = Len(myItem) * 10 + 30  
    End If  
Next  

希望这为您提供了一个不错的起点。

What programming platform are you using? I'm guessing .NET and VB.

Put in a method to examine the contents of the list and change the size of the box and window as required:

Dim intMaxLength As Integer = 20
For Each myItem As String In ListBox1.Items
    If Len(myItem) > intMaxLength Then  
       'Number of characters times number of pixels per character  
        ListBox1.Width = Len(myItem) * 10  
        'Me refers back to the form object  
        'Add a few extra pixels to give space around your listbox  
        Me.Width = Len(myItem) * 10 + 30  
    End If  
Next  

Hope this gives you a decent starting point.

时光倒影 2024-08-20 22:23:29

试试这个:

int maxcol = ((CHeaderCtrl*)(listctrl.GetDlgItem(0)))->GetItemCount()-1;
for (int col = 0; col <= maxcol; col++)
{
    listctrl.SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
}

Try this:

int maxcol = ((CHeaderCtrl*)(listctrl.GetDlgItem(0)))->GetItemCount()-1;
for (int col = 0; col <= maxcol; col++)
{
    listctrl.SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文