2016-03-06 5 views
9

मैं ListView तो मैं ऐसा किया की खोज बॉक्स में गतिशील रूप से एक TEditButton रखना चाहता हूँ:मैं सूचीदृश्य के खोज बॉक्स में स्पष्ट बटन कैसे हटा सकता हूं?

ListView       := TListView.Create(Self);  
ListView.Parent     := Self; 
ListView.Name     := 'hello'; 
ListView.Height     := 369; 
ListView.Width     := 369; 
ListView.Align     := TAlignLayout.Bottom; 
ListView.SearchVisible   := True; 
ListView.BeginUpdate;  

for i := 0 to ListView.controls.Count - 1 do 
begin 
    if ListView.controls[i].ClassType = TSearchBox then 
    begin  
    SearchBox := TSearchBox(ListView.controls[i]); 
    end; 
end; 

OKbtn := TEditButton.Create(SearchBox); 
OKbtn.Parent := SearchBox;  
OKbtn.Text := 'OK'; 
OKbtn.Width := 30; 

SearchBox.AddObject(OKbtn); 
ListView.EndUpdate;  

लेकिन समस्या clear button भी आ रहा है खोज बॉक्स संपादित करते समय है। मैं खोज बॉक्स के दाईं ओर उस स्पष्ट बटन (एक्स) को कैसे हटा सकता हूं?

उत्तर

11

enter image description here

SearchBox TEdit का वंशज है और FireMonkey नियंत्रण शैलियों का उपयोग करता है।

प्लेस अपने फार्म पर एक TEdit और उसके StyleLookup संपत्ति खोलें:

enter image description here

आप अलग अलग पहले से ही उपलब्ध stlyes देखते हैं कि देख सकते हैं। इसलिए हम अपने खोज बॉक्स के लिए स्टाइल लुकअप को बदलना चाहते हैं।

चूंकि SearchBox ListView नियंत्रण का एक निजी सदस्य (FSearchEdit) है, तो आपके पास इसका उपयोग करने का कोई प्रत्यक्ष तरीका नहीं है।

आप या तो अपना खुद का सूची दृश्य नियंत्रण बना सकते हैं जो TListView (TListViewBase) के वंशज हैं या कक्षा सहायक का उपयोग करते हैं। मैं बाद के लिए चुनते हैं।

TListViewHelper = class helper for TListViewBase 
private 
    function GetClearButton: Boolean; 
    procedure SetClearButton(const Value: Boolean); 
public 
    property ShowSearchEditClearButton: Boolean read GetClearButton write SetClearButton; 
end; 

{ TListViewHelper } 

function TListViewHelper.GetClearButton: Boolean; 
begin 
    Result := Self.FSearchEdit.StyleLookup = ''; // default style 
end; 

procedure TListViewHelper.SetClearButton(const Value: Boolean); 
begin 
    if Value then 
    Self.FSearchEdit.StyleLookup := '' // default style 
    else 
    Self.FSearchEdit.StyleLookup := 'editstyle'; 
end; 

FormCreate हम ListView1.ShowSearchEditClearButton := False; कॉल कर सकते हैं और स्पष्ट बटन चला गया है।

enter image description here

हालांकि ताल लेंस आइकन भी गायब हो गया है, क्योंकि यह editstyle शैली हम StyleLookup के रूप में स्थापित है कि का हिस्सा नहीं है।

आइकन वापस पाने के लिए हमें अपनी खुद की शैली बनाना होगा जिसमें एक आवर्धक ग्लास आइकन है लेकिन कोई स्पष्ट बटन नहीं है।

, फार्म पर एक TEdit ड्रॉप सही उस पर क्लिक करें और फिर edit customized style चुनें:

enter image description here

अब हम Stylebook संपादक में हैं और नियंत्रण लेआउट की पहुंच है।

स्ट्रक्चरल करने के लिए एक TActiveStyleObject जोड़ें, के लिए magnifierglass enter image description here

बदलें TActiveStyleObject के ActiveLink बिटमैप्स इसका नाम बदलें।

बिटमैपलिंक्स-संपादक में आवर्धक ग्लास आइकन का पता लगाएं और इसे चुनें (ActiveLink और SourceLink के लिए)।

enter image description here

आपका पाठ अब आइकन के साथ ओवरलैप होगा।

enter image description here

इसे ठीक करने के लिए आप 20.

enter image description here

enter image description here

आप कर सकते हैं की तरह उच्च कुछ करने के लिए सामग्री के बाईं मार्जिन (जो वर्तमान में 2px पर सेट है) बदलना होगा अब शैली के रूप में और आपके रूपों के स्टाइलबुक में आपके फॉर्म पर संपादन हटाएं।

स्टाइलबुक खोलें और searcheditstylenoclearbtn पर अपनी नई शैली के StyleName का नाम बदलें।

enter image description here

यह सहेजें और अपने classhelper कार्यों में

Self.FSearchEdit.StyleLookup := 'editstyle'; 

बदलने

Self.FSearchEdit.StyleLookup := 'searcheditstylenoclearbtn'; 

अब स्पष्ट बटन चला गया है करने के लिए।

enter image description here

आप अपने खुद के searcheditstylenoclearbtn आप searcheditstylenoclearbtn.style के रूप में निम्नलिखित कोड को बचाने और Stylebook संपादक में लोड कर सकते हैं बनाने की परेशानी के माध्यम से जाना नहीं चाहते हैं।

enter image description here

object TStyleContainer 
    object TLayout 
    StyleName = 'searcheditstylenoclearbtn' 
    Position.X = 530.000000000000000000 
    Position.Y = 399.000000000000000000 
    Size.Width = 100.000000000000000000 
    Size.Height = 22.000000000000000000 
    Size.PlatformDefault = False 
    Visible = False 
    TabOrder = 0 
    object TActiveStyleObject 
     StyleName = 'background' 
     Align = Contents 
     SourceLookup = 'Windows 10 Desktopstyle.png' 
     Size.Width = 100.000000000000000000 
     Size.Height = 22.000000000000000000 
     Size.PlatformDefault = False 
     ActiveTrigger = Focused 
     ActiveLink = < 
     item 
      CapInsets.Left = 7.000000000000000000 
      CapInsets.Top = 7.000000000000000000 
      CapInsets.Right = 7.000000000000000000 
      CapInsets.Bottom = 7.000000000000000000 
      SourceRect.Left = 266.000000000000000000 
      SourceRect.Top = 81.000000000000000000 
      SourceRect.Right = 305.000000000000000000 
      SourceRect.Bottom = 110.000000000000000000 
     end> 
     SourceLink = < 
     item 
      CapInsets.Left = 7.000000000000000000 
      CapInsets.Top = 7.000000000000000000 
      CapInsets.Right = 7.000000000000000000 
      CapInsets.Bottom = 7.000000000000000000 
      SourceRect.Left = 225.000000000000000000 
      SourceRect.Top = 81.000000000000000000 
      SourceRect.Right = 264.000000000000000000 
      SourceRect.Bottom = 110.000000000000000000 
     end> 
     TouchAnimation.Link = <> 
    end 
    object TLayout 
     StyleName = 'content' 
     Align = Client 
     Locked = True 
     Margins.Left = 20.000000000000000000 
     Margins.Top = 2.000000000000000000 
     Margins.Right = 2.000000000000000000 
     Margins.Bottom = 2.000000000000000000 
     Size.Width = 6.000000000000000000 
     Size.Height = 18.000000000000000000 
     Size.PlatformDefault = False 
    end 
    object TLayout 
     StyleName = 'buttons' 
     Align = Right 
     Locked = True 
     Margins.Top = 2.000000000000000000 
     Margins.Right = 2.000000000000000000 
     Margins.Bottom = 2.000000000000000000 
     Position.X = 48.000000000000000000 
     Position.Y = 2.000000000000000000 
     Size.Width = 50.000000000000000000 
     Size.Height = 18.000000000000000000 
     Size.PlatformDefault = False 
    end 
    object TBrushObject 
     StyleName = 'foreground' 
     Brush.Color = claBlack 
    end 
    object TBrushObject 
     StyleName = 'selection' 
     Brush.Color = x7F2A96FF 
    end 
    object TFontObject 
     StyleName = 'font' 
    end 
    object TLabel 
     StyleName = 'prompt' 
     Locked = True 
     Opacity = 0.500000000000000000 
     Visible = False 
    end 
    object TActiveStyleObject 
     StyleName = 'magnifierglass' 
     Align = Left 
     CapMode = Tile 
     Margins.Top = 1.000000000000000000 
     SourceLookup = 'Windows 10 Desktopstyle.png' 
     Position.Y = 1.000000000000000000 
     Size.Width = 20.000000000000000000 
     Size.Height = 21.000000000000000000 
     Size.PlatformDefault = False 
     WrapMode = Center 
     ActiveTrigger = Pressed 
     ActiveLink = < 
     item 
      SourceRect.Left = 4.000000000000000000 
      SourceRect.Top = 358.000000000000000000 
      SourceRect.Right = 20.000000000000000000 
      SourceRect.Bottom = 374.000000000000000000 
     end> 
     SourceLink = < 
     item 
      SourceRect.Left = 4.000000000000000000 
      SourceRect.Top = 358.000000000000000000 
      SourceRect.Right = 20.000000000000000000 
      SourceRect.Bottom = 374.000000000000000000 
     end> 
     TouchAnimation.Link = <> 
    end 
    end 
end 
5

आप अपने आवेदन में सभी खोज बॉक्स में Clearbutton देखना चाहते हैं, तो आप FMX.Searchbox.Style.pas संशोधित कर सकते हैं:

  1. (FMX फ़ोल्डर में FMX.Searchbox.Style.pas खोजें डिफ़ॉल्ट रूप से, C:\Program Files (x86)\Embarcadero\Studio\{your_version, e.g.17.0}\source\fmx\FMX.SearchBox.Style.pas और कॉपी अपने प्रोजेक्ट फ़ोल्डर में फ़ाइल करें (your_application.dpr फ़ाइल के पास)
  2. नई फ़ाइल में अगला खोजें और टिप्पणी करें लाइनों:

डेल्फी सिएटल के लिए:

procedure TStyledSearchBox.RealignButtons; 
begin 
    if (LeftLayout <> nil) and (FMagGlass <> nil) then 
    LeftLayout.Width := FMagGlass.Width; 
    if (ButtonsLayout <> nil) and (FClearButton <> nil) then 
    //if Model.Text.IsEmpty then 
     ButtonsLayout.Width := 0 
    //else 
    // ButtonsLayout.Width := FClearButton.Width; 
end; 

XE7 के लिए:

procedure TStyledSearchBox.DoChangeTracking; 
begin 
    inherited; 
    if (ButtonsLayout <> nil) and (FClearButton <> nil) then 
    //if Model.Text.IsEmpty then 
     ButtonsLayout.Width := 0 
    //else 
    // ButtonsLayout.Width := FClearButton.Width; 
end; 

आप देख सकते हैं, कोड डेल्फी संस्करण के आधार पर बहुत अलग नहीं है, अन्य संस्करणों के लिए आप इसे स्वयं पा सकते हैं।

  1. संकलन और एप्लिकेशन लॉन्च करें।

ये कोड परिवर्तन सभी प्लेटफ़ॉर्म के लिए मान्य हैं।

संबंधित मुद्दे