Click here to Skip to main content
15,881,882 members
Home / Discussions / C#
   

C#

 
AnswerRe: Printing two panels on a card back and front Pin
OriginalGriff20-Jan-22 5:12
mveOriginalGriff20-Jan-22 5:12 
GeneralRe: Printing two panels on a card back and front Pin
Member 1509338120-Jan-22 8:05
Member 1509338120-Jan-22 8:05 
GeneralRe: Printing two panels on a card back and front Pin
OriginalGriff20-Jan-22 8:12
mveOriginalGriff20-Jan-22 8:12 
GeneralRe: Printing two panels on a card back and front Pin
Luc Pattyn20-Jan-22 9:05
sitebuilderLuc Pattyn20-Jan-22 9:05 
GeneralRe: Printing two panels on a card back and front Pin
RobertSF20-Jan-22 11:09
professionalRobertSF20-Jan-22 11:09 
Generalbtn event async await rtb and tb overlap and switch between them by tb.show and tb.hide Pin
Member 1356965020-Jan-22 4:31
Member 1356965020-Jan-22 4:31 
GeneralRe: btn event async await rtb and tb overlap and switch between them by tb.show and tb.hide Pin
Dave Kreskowiak20-Jan-22 5:09
mveDave Kreskowiak20-Jan-22 5:09 
GeneralRe: btn event async await rtb and tb overlap and switch between them by tb.show and tb.hide Pin
Member 1356965020-Jan-22 5:20
Member 1356965020-Jan-22 5:20 
private async void FindAllBTN_Click(object sender, EventArgs e)
{
  progressBar.Minimum = 0;
  if (!isRunning)
  {
    OutputTB.Hide();
    OutputRTB.Show();
    FlipNextAllCKB.Checked = true;
    OutputRTB.Text = OutputTB.Text;

    FindAllBTN.Text = "Cancel";
    FindAllBTN.ForeColor = Color.Red;

    Progress<int> prog = new Progress<int>(SetProgress);
    m_cancelTokenSource = new CancellationTokenSource();

    try
    {
      await FindAllMatches(searchTB.Text, prog, m_cancelTokenSource.Token);
    }
    catch (OperationCanceledException)
    {
      //
    }
    finally
    {
      FindAllBTN.Text = "Find All";
      FindAllBTN.ForeColor = Color.Black;
      isRunning = false;
      m_cancelTokenSource = null;
      progressBar.Value = 0;
      replaceLTB.Focus();
    }
  }
  else
  {
    m_cancelTokenSource.Cancel();
  }

  return;

}




public Task FindAllMatches(string searchString, IProgress<int> prog, CancellationToken ct)
{
  return Task.Run(() =>
  {
    try
    {
      bool yesno = false;
      string source = "";

      Regex re;
      if (searchString == "")
      {
        OutputTB.AppendText("\r\nNeed RegEx Expression Input\r\n");
        return;
      }

      try
      {
        re = new Regex(searchString);
      }
      catch (ArgumentException e)
      {
        OutputTB.AppendText("\r\nEx Err " + e.ToString() + "\r\n");
        return;
      }

      source = OutputTB.Text;
      if (source == "")
      {
        OutputTB.AppendText("Need Source Text Input");
        return;
      }

      OutputRTB.SelectionStart = 0;
      OutputRTB.SelectionLength = OutputRTB.Text.Length;
      OutputRTB.SelectionBackColor = Color.White;

      MatchCollection matches = Regex.Matches(OutputRTB.Text, searchString);

      progressBar.Maximum = matches.Count;
      for(int i = 0; i < matches.Count; i++)
      {
        prog.Report(i);

        if (ct.IsCancellationRequested)
        {
          OutputRTB.AppendText($"\r\nCanceled at Loop {i}");
          OutputRTB.AppendText("\r\n");
          throw new OperationCanceledException(ct);
        }

        OutputRTB.SelectionStart = matches[i].Index;
        OutputRTB.SelectionLength = matches[i].Length;
        if (yesno)
        {
            OutputRTB.SelectionBackColor = Color.GreenYellow;
            yesno = false;
        }
        else
        {
            OutputRTB.SelectionBackColor = Color.Turquoise;
            yesno = true;
        }

      }
    }
    catch (Exception ex)
    {
      if(ex.ToString().Contains("OperationCanceledException"))
      {
        //
      }
      else
      {
        //
      }
    }
    finally
    {
      //
    }
  }, ct);

}


The async await was taken from this sit

Handling long operations with cancel and progress in C# with async – Cowthulu[^]


************************************************************************

ee the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.RichTextBox.EditStreamProc(IntPtr dwCookie, IntPtr buf, Int32 cb, Int32& transferred)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
   at System.Windows.Forms.RichTextBox.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)



I Appreciate Your Willingness to Help me


Ok Thank you I got it.

modified 20-Jan-22 15:40pm.

GeneralRe: btn event async await rtb and tb overlap and switch between them by tb.show and tb.hide Pin
Dave Kreskowiak20-Jan-22 9:42
mveDave Kreskowiak20-Jan-22 9:42 
GeneralRe: btn event async await rtb and tb overlap and switch between them by tb.show and tb.hide Pin
Member 1356965020-Jan-22 10:00
Member 1356965020-Jan-22 10:00 
QuestionExceptions Pin
Simon_Whale17-Jan-22 2:30
Simon_Whale17-Jan-22 2:30 
AnswerRe: Exceptions Pin
CHill6017-Jan-22 3:08
mveCHill6017-Jan-22 3:08 
GeneralRe: Exceptions Pin
Simon_Whale17-Jan-22 3:53
Simon_Whale17-Jan-22 3:53 
AnswerRe: Exceptions Pin
OriginalGriff17-Jan-22 3:41
mveOriginalGriff17-Jan-22 3:41 
GeneralRe: Exceptions Pin
Simon_Whale17-Jan-22 3:53
Simon_Whale17-Jan-22 3:53 
AnswerRe: Exceptions Pin
Luc Pattyn17-Jan-22 3:59
sitebuilderLuc Pattyn17-Jan-22 3:59 
QuestionRe: Exceptions Pin
Simon_Whale18-Jan-22 5:05
Simon_Whale18-Jan-22 5:05 
AnswerRe: Exceptions Pin
Gerry Schmitz17-Jan-22 6:07
mveGerry Schmitz17-Jan-22 6:07 
GeneralRe: Exceptions Pin
Simon_Whale18-Jan-22 5:06
Simon_Whale18-Jan-22 5:06 
AnswerRe: Exceptions Pin
lmoelleb18-Jan-22 4:55
lmoelleb18-Jan-22 4:55 
QuestionRe: Exceptions Pin
Eddy Vluggen18-Jan-22 19:32
professionalEddy Vluggen18-Jan-22 19:32 
AnswerRe: Exceptions Pin
lmoelleb18-Jan-22 20:40
lmoelleb18-Jan-22 20:40 
GeneralRe: Exceptions Pin
Eddy Vluggen19-Jan-22 0:40
professionalEddy Vluggen19-Jan-22 0:40 
GeneralRe: Exceptions Pin
lmoelleb19-Jan-22 0:48
lmoelleb19-Jan-22 0:48 
GeneralRe: Exceptions Pin
Eddy Vluggen19-Jan-22 1:12
professionalEddy Vluggen19-Jan-22 1:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.