Added new logic to find next cell in a table

Added Button for Curent Cell or Paragraph
This commit is contained in:
Rich
2019-05-16 10:54:12 +00:00
parent b43f4c711e
commit b766f9c868
2 changed files with 220 additions and 51 deletions

View File

@@ -92,8 +92,10 @@ namespace Volian.Controls.Library
}
private void btnPrev_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0);
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 1);
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0); // Move to previous paragraph
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 1); // Select paragraph
//_WordApp.Selection.MoveEnd(LBWdUnits.wdCharacter, -1); // Move 1 Character Back
try
{
// C2019-021 Use generic function CopyWordText
@@ -106,19 +108,79 @@ namespace Volian.Controls.Library
catch
{ }
}
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
/// <summary>
/// MoveToNextCell
/// Find the next Cell including merged cells
/// Loop through columns then rows
/// </summary>
/// <returns>
/// Yes - if Processed as a cell
/// No - Not Processed
/// </returns>
private bool MoveToNextCell()
{
// Get the row and column for the current selection within the table.
int rowIndex = _WordApp.Selection.Cells[1].RowIndex;
int columnIndex = _WordApp.Selection.Cells[1].ColumnIndex;
// Only do this for tables
if (_WordApp.Selection.Tables != null && _WordApp.Selection.Tables.Count > 0)
{
LBTable tbl = _WordApp.Selection.Tables[1];
if (tbl.Range.Cells != null && tbl.Range.Cells.Count > 0)
{
// Make sure the entire cell is selected
_WordApp.Selection.SelectCell();
// Loop through the cells of the table
for (int i = 1; i < tbl.Range.Cells.Count; i++)
{
LBCell myCell = tbl.Range.Cells[i];
// if this cell has matching indices, then move to the next cell
if (myCell.RowIndex == rowIndex && myCell.ColumnIndex == columnIndex)
{
// Move the Start to the start of the next cell
_WordApp.Selection.Start = tbl.Range.Cells[i + 1].Range.Start;
// Select the entire cell
_WordApp.Selection.SelectCell();
return true; // Cell Found and moved
}
}
}
}
return false; // Cell not found
}
private void btnNext_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 0);
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);
// C2019-021 Either move by cells or paragraphs
if (_WordApp.Selection.Cells != null && _WordApp.Selection.Cells.Count > 0)
{
if (!MoveToNextCell())
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 2, 0);// Move beyond the table
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);// Select a Paragraph
}
}
else // not a table
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 0); // Move to the next paragraph
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1); // Select a Paragraph
}
try
{
// C2019-021 Use Generic CopyWordText
CopyWordText();
// C2019-021If the word text only contains whitespace skip to next
if (txbWrdText.Text.TrimEnd("\f\r\n".ToCharArray()) == "")
btnNext_Click(sender, e);
else
if (_WordApp.Selection.Cells != null && _WordApp.Selection.Cells.Count > 0)
{
Clipboard.SetText(txbWrdText.Text);
}
else
{
// C2019-021 If the word text only contains whitespace skip to next
if (txbWrdText.Text.TrimEnd("\f\r\n".ToCharArray()) == "")
btnNext_Click(sender, e);
else
Clipboard.SetText(txbWrdText.Text);
}
}
catch
{ }
@@ -129,17 +191,33 @@ namespace Volian.Controls.Library
txvStyle.Text = _WordApp.Selection.Style.NameLocal;
txbLevel.Text = _WordApp.Selection.Style.ListLevelNumber.ToString();
tbxNumber.Text = _WordApp.Selection.Range.ListFormat.ListString;
int idx = -1;
if (_WordApp.Selection.Text.EndsWith("\t(\r") || _WordApp.Selection.Text.EndsWith("\t___\r"))
idx = _WordApp.Selection.Text.LastIndexOf("\t");
if (idx > 0)
txbWrdText.Text = _WordApp.Selection.Text.Substring(0, idx);
else
txbWrdText.Text = _WordApp.Selection.Text;
txbWrdText.Text = txbWrdText.Text.Replace('\xA0', ' '); // replace hard space with regular space
txbWrdText.Text = txbWrdText.Text.Replace('\x0B', ' '); // replace hard return with regular space
// C2019-021 Don't replace tabs
//txbWrdText.Text = txbWrdText.Text.Replace("\t", " "); // replace tabs with 5 regular spaces
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
try
{
//Console.WriteLine("Cell Count = {0}", _WordApp.Selection.Cells.Count);
lblTable.Text = "Table";
lblTable.FontBold = true;
}
catch
{
lblTable.Text = "Not Table";
lblTable.FontBold = false;
}
txbWrdText.Text = ShowText(_WordApp.Selection.Text.TrimEnd("\r\a".ToCharArray()));
_WordApp.Activate();
}
// C2019-021 RHM 5/15/2019 Added newe methods and properties for Import Word Content
private string ShowText(string txt)
{
// This was for debugging
//StringBuilder sb = new StringBuilder();
//foreach (char c in txt)
// if (((int)c) >= ' ' && ((int)c) < '\x7F')
// sb.Append(c);
// else
// sb.Append(string.Format("\\u{0:X4} ", ((int)c)));
//return sb.ToString();
return txt;
}
private void txbWordFile_TextChanged(object sender, EventArgs e)
{
@@ -185,12 +263,20 @@ namespace Volian.Controls.Library
// }
// this.Focus(); // set focus back to this form
//}
// C2019-021 New function to replace the PROMS text with the word text and move to the next paragraph in word.
// C2019-021 New function to replace the PROMS text with the word text and move to the next paragraph in word.
private void btnReplaceNext_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
if (MyStepRTB.MyItemInfo.IsSection && tbxNumber.Text != "")
if (MyStepRTB.Parent is VlnFlexGrid)//Replace text in a Grid (Table cell)
{
VlnFlexGrid vg = MyStepRTB.Parent as VlnFlexGrid;
vg.StartEditing();
MyStepRTB.Text = txbWrdText.Text;
vg[vg.Row, vg.Col] = MyStepRTB.Rtf;
vg.FinishEditing(false);
}
else if (MyStepRTB.MyItemInfo.IsSection && tbxNumber.Text != "")
{
using (VEPROMS.CSLA.Library.Item ii = MyStepRTB.MyItemInfo.Get())
{
@@ -227,10 +313,19 @@ namespace Volian.Controls.Library
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei != null)
if (MyStepRTB.Parent is VlnFlexGrid)
{
ei.AddSiblingAfter();
VlnFlexGrid vg = MyStepRTB.Parent as VlnFlexGrid;
//vg.FinishEditing(false);
vg.SelectNextCell();
}
else
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
}
@@ -247,10 +342,10 @@ namespace Volian.Controls.Library
return;
}
while (ei != null && !ei.MyItemInfo.IsSection)
if(ei.MyParentEditItem != null)
if (ei.MyParentEditItem != null)
ei = ei.MyParentEditItem;
else
ei=ei.MyPreviousEditItem;
ei = ei.MyPreviousEditItem;
if (ei != null)
{
ei.AddSiblingAfter();
@@ -324,5 +419,34 @@ namespace Volian.Controls.Library
MyStepRTB.SelectedText = Clipboard.GetText();
}
}
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
private void btnCurrent_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0); // Select paragraph
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1); // Select paragraph
_WordApp.Selection.MoveEnd(LBWdUnits.wdCharacter, -1); // Exclude the last character
try
{
// C2019-021 Use Generic CopyWordText
CopyWordText();
// C2019-021If the word text only contains whitespace skip to next
//if (txbWrdText.Text.TrimEnd("\f\r\n\a".ToCharArray()) == "")
// btnNext_Click(sender, e);
//else
Clipboard.SetText(txbWrdText.Text);
}
catch
{ }
}
private void btnTest_Click(object sender, EventArgs e)
{
if (!MoveToNextCell())
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 2, 0);
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);
}
_WordApp.Activate();
//int cols = _WordApp.Selection.Tables[1].Columns.Count;
}
}
}