更多帮助我很难找到答案. [英] more help I am having a hard time finding answers.

查看:94
本文介绍了更多帮助我很难找到答案.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我问了一个问题,希望能指出正确的方向.

Yesterday I asked a question with the hopes of being pointed in the right direction.

报价:

使用列表框作为对话框.:

using listbox as a dialog box.":



感谢Nishant Sivakumar和SAKryukov在此问题上的答复.
在搜索"Logging LIstbox"和将字符串发送到Listbox"后,我仍然找不到如何解决此问题的答案.
我的印象是我应该能够做到这一点:this-> listbox1->添加.
在代码中.结果告诉我,我缺少一些关键信息.

所以,
问题1:我该怎么做?
问题2:Nishant,您的书是否涉足这一领域?
问题3:除了基本形式以外还有其他资源吗?

我希望我的问题很清楚.



Thank you to both Nishant Sivakumar and SAKryukov for your response in the matter.
Upon googling "Logging LIstbox" and "send string to Listbox" I still have problems finding the answers as to how I am to do this.
I am under the impression that I should be able to do this: this -> listbox1 -> add.
in the code. The results tell me I am missing some key information.

So,
Question 1: How do I do this?
Question 2: Nishant, does your book delve into this area?
Question 3: any other resources that are beyond the basic forms?

I hope I my questions are clear enough.

推荐答案

类似的东西:
Something like that:
ListBox ListBoxLog = new ListBox();

//...

internal void Log(string message) {
    ListBoxLog.Items.Add(message);
    ListBoxLog.SelectedIndex = ListBoxLog.Items.Count - 1;
}



您可能还希望使其更加线程安全,以便可以从非UI线程添加消息.只是稍微复杂一点:



You may also want to make it thread-safe, more exactly, to make it possible to add message from non-UI thread. It''s just a bit more complex:

ListBox ListBoxLog = new ListBox();

//...

static void Log(ListBox listBox, string message) {
    listBox.Items.Add(message);
    listBox.SelectedIndex = listBox.Items.Count - 1;
}

internal void Log(string message) {
    if (ListBoxLog.InvokeRequred) //if called from any other thread...
        ListBoxLog.Invoke(new Action<ListBox, string>((listBox, msg)=>{
            Log(listBox, msg);
        }), ListBoxLog, message);
    else //if called from the same thread as the UI of ListBoxLog:
        Log(ListBoxLog, message);
}



最后,您可能希望将任何类型的项目存储在列表框中,而不仅仅是字符串.在项目中存储其他信息(例如消息类型,主题,异常信息等)可能非常有用.在列表框的选择事件中,您可以从项目中获取此数据并将其显示为采用主从视图的样式.

您可以做到,但是问题将是:什么将以视图项的文本形式显示?答案很简单:方法ToString返回的结果.为此,您只需要覆盖列表项类型中的object.ToString即可以一个短字符串的形式显示数据的相应部分.

—SA



Finally, you may want to store items of any type in a list box, not just strings. It could be very useful to store additional information in the items, such as type of message, topic, exception information, etc. On selection events of the list box, you could get this data from an item and show it in, say PropertyGrid in the style of master-detail view.

You can do it, but the problem will be: what will be presented as a text of view item? The answer is simple: whatever is returned by the method ToString. For this purpose, you just need to override object.ToString in the type of a list item to present appropriate part of data in the form of one short string.

—SA


这篇关于更多帮助我很难找到答案.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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