lundi 25 novembre 2019

C# CheckBox List Sending only checked boxes to Word Document

I'm writing an application where the user can log what items are checked out to students, and print that information to a Word document for the student to sign. The WinForm has 6 CheckBoxes that can be chosen. I have some code that almost works, but still isn't quite giving me the formatted bulleted list of only checked items that I want. Here's the code I have so far:

// Add list of checked-out items to Document
Microsoft.Office.Interop.Word.Paragraph ul = doc.Content.Paragraphs.Add(ref missing);
ul.Range.ListFormat.ApplyBulletDefault();

if (listItem1CheckBx.Checked)
{
    ul.Range.ListFormat.ApplyBulletDefault();
    ul.Range.InsertBefore(listItem1CheckBx.Text + "\n");
}
if (listItem2CheckBx.Checked)
{
    ul.Range.ListFormat.ApplyBulletDefault();
    ul.Range.InsertBefore(listItem2CheckBx.Text + "\n");
}
if (listItem3CheckBx.Checked)
{
    ul.Range.ListFormat.ApplyBulletDefault();
    ul.Range.InsertBefore(listItem3CheckBx.Text + "\n");
}
if (listItem4CheckBx.Checked)
{
    ul.Range.ListFormat.ApplyBulletDefault();
    ul.Range.InsertBefore(listItem4CheckBx.Text + "\n");
}
if (listItem5CheckBx.Checked)
{
    ul.Range.ListFormat.ApplyBulletDefault();
    ul.Range.InsertBefore(listItem5CheckBx.Text + "\n");
}
if (otherChckBx.Checked)
{
    ul.Range.ListFormat.ApplyBulletDefault();
    ul.Range.InsertAfter(otherListItemTxtBx.Text);
}

How can I make this work better and possibly clean up the code at the same time?




Aucun commentaire:

Enregistrer un commentaire