clipboardc++builderc++builder-10-seattle

Paste Certain Clipboard Text into TEdit in CBB 10


I wish that when the user clicks the button, ONLY TEXT THAT CONTAINS AN URL (beginning with http://) on the Clipboard is automatically pasted into the TEdit.

I've tried the following code but doesn't work at all.

#include <Clipbrd.hpp>

void __fastcall TForm1::Button1Click(TObject *Sender)
{
String Text = "http://";

  if (Clipboard()->HasFormat(CF_TEXT))
  {
    Edit->Text = ContainsText(Clipboard()->AsText, Text);
    // Clipboard()->Clear();
  }
}

Solution

  • ContainsText() returns a bool indicating whether the subtext was found or not. You are assigning that result directly to your TEdit instead of using it to make a decision whether or not to assign the clipboard text to the TEdit.

    Try this instead:

    #include <Clipbrd.hpp>
    #include <StrUtils.hpp>
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        if (Clipboard()->HasFormat(CF_TEXT))
        {
            String CBText = Clipboard()->AsText;
            if (ContainsText(CBText, "http://"))
            {
                Edit->Text = CBText;
                // Clipboard()->Clear();
            }
        }
    }
    

    BTW, http:// is not the only URL scheme widely used. At a minimum, consider also looking for https:// as well.