C#中的复选框和文本之间自动中断 [英] Auto Break between Checkbox and Text in C#

查看:81
本文介绍了C#中的复选框和文本之间自动中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望您能帮助我解决我的问题.

I hope you can help me with my problem.

我想使用C#程序自动生成Word清单.

I want to automatically generate a Word checklist with a C# program.

但是我无法将其放在复选框后面.文本始终会在复选框下换行.

But I can't get it to put the text behind the checkbox. The text always lands in a new line under the checkbox.

我该如何解决这个问题?

How can I solve this problem?

您还知道其他一些功能吗?

You know some other functions?

    public void createChecklist()
    {
        Application app = new Application();
        app.Visible = true;
        Document doc = app.Documents.Add();
        Paragraph para = doc.Paragraphs.Add();

        ContentControl checkbox = para.Range.ContentControls.Add(WdContentControlType.wdContentControlCheckBox);
        para.Range.InsertAfter("sdjsakd");

        doc.SaveAs2("C:\\tmp\\checklist.docx");
        app.Quit();

    }

推荐答案

该行为的原因是因为文本的目标"是 entire 段落的范围.因此,当在其后插入某些内容时,它会在该段落之后插入.

The reason for the behavior is because the "target" for the text is the range of the entire paragraph. So when something is inserted after, it's inserted after the paragraph.

此操作的关键是将范围带回到段落内-不包括段落标记. (由于抑制了非打印字符的显示,因此在屏幕快照中不显示段落标记.单击功能区主页"选项卡中的向后P",该段落标记应该可见.)

The key to this is to bring the range back to within the paragraph - to not include the paragraph mark. (The paragraph mark is not visible in the screen shot because the display of non-printing characters is suppressed. Click the "backwards P" in the Home tab of the Ribbon and the paragraph mark should be visible.)

有多种方法可以解决此问题; Range.MoveEnd经常使用(在下面).

There are various ways to approach this; Range.MoveEndis used quite often (below).

注意:使用COM"interop"来清理对象是很危险的.这可能很快导致应用程序的孤立"实例保留在内存中.由于问题中的代码退出了Word应用程序,因此我将其添加到了代码示例中.

Note: It's dangerous when using COM "interop" to not clean up the objects. This could quickly lead to "orphaned" instances of the application staying in memory. I've added that to the code sample since the code in the question quits the Word application.

public void createChecklist()
{
    Application app = new Application();
    app.Visible = true;
    Document doc = app.Documents.Add();
    Paragraph para = doc.Paragraphs.Add();
    Range rng = para.Range;

    ContentControl checkbox = rng.ContentControls.Add(WdContentControlType.wdContentControlCheckBox);
    rng.MoveEnd(WdUnits.wdCharacter, -1);
    rng.InsertAfter(" sdjsakd");

    doc.SaveAs2("C:\\tmp\\checklist.docx");
  //Release the COM objects and clean up
    checkbox = null;
    rng = null;
    para = null;
    doc = null;
    app.Quit();
    app = null;
    GC.Collect(); GC.AwaitPendingFinalizers();
    GC.Collect(); GC.AwaitPendingFinalizers();

}

添加了注释以阐明为什么要运行两次垃圾回收的问题:

Added note to clarify question of why run the garbage collection twice:

此信息来自安德鲁·怀特查佩尔(Andrew Whitechapel)的".NET开发Microsoft Office",第2章,其摘录在MSDN上不再可用,因此在此处引用:

This information comes from Andrew Whitechapel's ".Net Development for Microsoft Office", Chapter 2, the extract of which is no longer available on MSDN, so quoted here:

请注意,我们正在重复调用CollectWaitForPendingFinalizers这是因为[Office 应用程序参考]虽然可以通过第一遍 然后将被标记为在下一个通行证上收集.所以我们会 进行第二次清理,以清除在第一遍中幸存的任何东西 但可以通过其他方式收集.

Note that we're repeating the calls to Collect and WaitForPendingFinalizersThis is because the memory for the [Office application reference] might have survived the first pass, although it will then have been marked for collection on the next pass. So we will make a second pass to clean up anything that survived the first pass but was otherwise available for collection.

这篇关于C#中的复选框和文本之间自动中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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