2016-03-24 21 views
0

Winform, C++ içinde dinamik olarak bir linklabels/Button kuyruğu oluşturmak istiyorum. Bir düğmeyi tıkladığımda linklabel, bağlantı1 olarak dinamik olarak görünür. Eğer butona ikinci kez tıklarsam link1 aşağıya inmeli ve link2 link1 üstünde görünmelidir. Düğmeye üçüncü kez tıklandığında link1 aşağıya inmeli, link2 link1 pozisyonunu almalı ve link3 link2 pozisyonunu almalı ve böyle devam etmelidir. Bir kuyruk gibi görebilirsiniz. Sıra beş maddeye sahip olmalı ve düğmeyi altıncı kez tıkladığında, bağlantı 1'in kaybolacağı ve 6 numaralı bağlantının üstte olması gerektiği. Bu devam ediyor ve devam ediyor.C++ formunda dinamik olarak bir linklabels/Düğme kuyruğu oluşturma Formlar

Bazı görevlerini tamamladım. Dinamik olarak linklabels oluşturdum. Düğme tıklandığında her linklab'i silme ve tıklama sayısına göre pozisyonlarını yeniden sıralayarak yeniden oluşturmayı düşündüm. Ancak, bağlantı etiketlerini silemedi. Hatta başka bir linklabelin üzerine yeni bir linklabelin üzerine yazmayı denedim, ancak önceki link etiketini kapatmayacağım ve üzerine yazılan linklabel'i tıklatırsam bazı istisna hatalarını gösterir.

+0

biz minimal, tam kod görebilir mi? – surajsn

+0

@psyco, aşağıdaki işlev tıklamalardan hiçbirine dayanarak yukarıdan aşağıya doğru seri olarak linklabels oluşturur. Tıklama sayısı = 6 değilse ve bağlantı 6 bağlantının 1 üstündedir. Tıklama olmaması durumunda = 7 numaralı bağlantı 7, bağlantı 2'nin üst kısmında ve o sırada açılır. –

cevap

0
`void History_Links() 
{ 
       string name=record[0]; 
       LinkLabel^ lbl=gcnew LinkLabel(); 
       lbl->Parent=this; 
       lbl->Location=System::Drawing::Point(10,300+(no_of_clicks-1)*15); 
       lbl->Size=System::Drawing::Size(55,15); 
       lbl->Name=gcnew String(link_names[no_of_clicks-1].c_str()); 
       lbl->Text= gcnew String(name.c_str()); 
       lbl->AutoSize = true; 


       switch(no_of_clicks) 
       { 
        case 1 : 
         lbl->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::history1_LinkClicked); 
         break; 
        case 2 : 
         //lbl->Clear(); 
         lbl->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::history2_LinkClicked); 
         break; 
        case 3 : 
         lbl->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::history3_LinkClicked); 
         break; 
        case 4 : 
         lbl->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::history4_LinkClicked); 
         break; 
        case 5 : 
         lbl->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::history5_LinkClicked); 
         break; 
        } 

}` 
0
#pragma once 
#include "stdafx.h" 
#include <sstream> 
#include <fstream> 
#include <vcclr.h> 
//#include <msclr\marshal_cppstd.h> 
//#include <chrono> 
//using namespace msclr::interop; 

namespace HistoryTracker { 

    using namespace System; 
    using namespace System::ComponentModel; 
    using namespace System::Collections; 
    using namespace System::Windows::Forms; 
    using namespace System::Data; 
    using namespace System::Drawing; 
    using namespace System::IO; 
    using namespace std; 
    using namespace Runtime::InteropServices; 

    string history[1000],record[50]; 
    //string link_names[5]={"history1","history2","history3","history4","history5"}; 
    int no_of_clicks=0; 

    /// <summary> 
    /// Summary for Form1 
    /// </summary> 
    public ref class Form1 : public System::Windows::Forms::Form 
    { 
    public: 
     Form1(void) 
     { 
      InitializeComponent(); 
      // 
      //TODO: Add the constructor code here 
      // 
     } 

    protected: 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     ~Form1() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 
    private: System::Windows::Forms::Button^ button1; 
    private: AxAcroPDFLib::AxAcroPDF^ axAcroPDF1; 
    private: System::Windows::Forms::Label^ label1; 
    private: System::Windows::Forms::Button^ button2; 
    private: System::Windows::Forms::LinkLabel^ linkLabel1; 
    private: System::Windows::Forms::LinkLabel^ linkLabel2; 
    private: System::Windows::Forms::LinkLabel^ linkLabel3; 
    private: System::Windows::Forms::LinkLabel^ linkLabel4; 
    private: System::Windows::Forms::LinkLabel^ linkLabel5; 



    protected: 

    private: 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     System::ComponentModel::Container ^components; 

#pragma region Windows Form Designer generated code 
     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     void InitializeComponent(void) 
     { 
      System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid)); 
      this->button1 = (gcnew System::Windows::Forms::Button()); 
      this->axAcroPDF1 = (gcnew AxAcroPDFLib::AxAcroPDF()); 
      this->label1 = (gcnew System::Windows::Forms::Label()); 
      this->button2 = (gcnew System::Windows::Forms::Button()); 
      this->linkLabel1 = (gcnew System::Windows::Forms::LinkLabel()); 
      this->linkLabel2 = (gcnew System::Windows::Forms::LinkLabel()); 
      this->linkLabel3 = (gcnew System::Windows::Forms::LinkLabel()); 
      this->linkLabel4 = (gcnew System::Windows::Forms::LinkLabel()); 
      this->linkLabel5 = (gcnew System::Windows::Forms::LinkLabel()); 
      (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->axAcroPDF1))->BeginInit(); 
      this->SuspendLayout(); 
      // 
      // button1 
      // 
      this->button1->Location = System::Drawing::Point(909, 11); 
      this->button1->Name = L"button1"; 
      this->button1->Size = System::Drawing::Size(122, 37); 
      this->button1->TabIndex = 0; 
      this->button1->Text = L"Choose Files"; 
      this->button1->UseVisualStyleBackColor = true; 
      this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); 
      // 
      // axAcroPDF1 
      // 
      this->axAcroPDF1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
       | System::Windows::Forms::AnchorStyles::Left) 
       | System::Windows::Forms::AnchorStyles::Right)); 
      this->axAcroPDF1->Enabled = true; 
      this->axAcroPDF1->Location = System::Drawing::Point(623, 55); 
      this->axAcroPDF1->Name = L"axAcroPDF1"; 
      this->axAcroPDF1->OcxState = (cli::safe_cast<System::Windows::Forms::AxHost::State^ >(resources->GetObject(L"axAcroPDF1.OcxState"))); 
      this->axAcroPDF1->Size = System::Drawing::Size(515, 580); 
      this->axAcroPDF1->TabIndex = 1; 
      // 
      // label1 
      // 
      this->label1->AutoSize = true; 
      this->label1->Location = System::Drawing::Point(16, 11); 
      this->label1->Name = L"label1"; 
      this->label1->Size = System::Drawing::Size(35, 13); 
      this->label1->TabIndex = 2; 
      this->label1->Text = L"label1"; 
      this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click); 
      // 
      // button2 
      // 
      this->button2->Location = System::Drawing::Point(1037, 11); 
      this->button2->Name = L"button2"; 
      this->button2->Size = System::Drawing::Size(101, 37); 
      this->button2->TabIndex = 3; 
      this->button2->Text = L"Exit"; 
      this->button2->UseVisualStyleBackColor = true; 
      this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); 
      // 
      // linkLabel1 
      // 
      this->linkLabel1->AutoSize = true; 
      this->linkLabel1->Location = System::Drawing::Point(15, 300); 
      this->linkLabel1->Name = L"linkLabel1"; 
      this->linkLabel1->Size = System::Drawing::Size(13, 13); 
      this->linkLabel1->TabIndex = 4; 
      this->linkLabel1->TabStop = true; 
      this->linkLabel1->Text = L""; 
      this->linkLabel1->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::linkLabel1_LinkClicked); 
      // 
      // linkLabel2 
      // 
      this->linkLabel2->AutoSize = true; 
      this->linkLabel2->Location = System::Drawing::Point(15, 315); 
      this->linkLabel2->Name = L"linkLabel2"; 
      this->linkLabel2->Size = System::Drawing::Size(13, 13); 
      this->linkLabel2->TabIndex = 5; 
      this->linkLabel2->TabStop = true; 
      this->linkLabel2->Text = L""; 
      this->linkLabel2->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::linkLabel2_LinkClicked); 
      // 
      // linkLabel3 
      // 
      this->linkLabel3->AutoSize = true; 
      this->linkLabel3->Location = System::Drawing::Point(15, 330); 
      this->linkLabel3->Name = L"linkLabel3"; 
      this->linkLabel3->Size = System::Drawing::Size(13, 13); 
      this->linkLabel3->TabIndex = 6; 
      this->linkLabel3->TabStop = true; 
      this->linkLabel3->Text = L""; 
      this->linkLabel3->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::linkLabel3_LinkClicked); 
      // 
      // linkLabel4 
      // 
      this->linkLabel4->AutoSize = true; 
      this->linkLabel4->Location = System::Drawing::Point(15, 345); 
      this->linkLabel4->Name = L"linkLabel4"; 
      this->linkLabel4->Size = System::Drawing::Size(13, 13); 
      this->linkLabel4->TabIndex = 7; 
      this->linkLabel4->TabStop = true; 
      this->linkLabel4->Text = L""; 
      this->linkLabel4->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::linkLabel4_LinkClicked); 
      // 
      // linkLabel5 
      // 
      this->linkLabel5->AutoSize = true; 
      this->linkLabel5->Location = System::Drawing::Point(15, 360); 
      this->linkLabel5->Name = L"linkLabel5"; 
      this->linkLabel5->Size = System::Drawing::Size(13, 13); 
      this->linkLabel5->TabIndex = 8; 
      this->linkLabel5->TabStop = true; 
      this->linkLabel5->Text = L""; 
      this->linkLabel5->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::linkLabel5_LinkClicked); 
      // 
      // Form1 
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
      this->ClientSize = System::Drawing::Size(1150, 647); 
      this->Controls->Add(this->linkLabel5); 
      this->Controls->Add(this->linkLabel4); 
      this->Controls->Add(this->linkLabel3); 
      this->Controls->Add(this->linkLabel2); 
      this->Controls->Add(this->linkLabel1); 
      this->Controls->Add(this->button2); 
      this->Controls->Add(this->label1); 
      this->Controls->Add(this->axAcroPDF1); 
      this->Controls->Add(this->button1); 
      this->Name = L"Form1"; 
      this->Text = L"Form1"; 
      (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->axAcroPDF1))->EndInit(); 
      this->ResumeLayout(false); 
      this->PerformLayout(); 

     } 

#pragma endregion 
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 
       OpenFileDialog^ opendialog =gcnew OpenFileDialog; 
       opendialog-> ShowDialog(); 
       axAcroPDF1->src=opendialog->FileName; 

       String^ file_name=opendialog->FileName; 
       std::string stan_string ; 
       //std::string stan_string = marshal_as<string>(file_name); 
       MarshalString(opendialog->FileName->ToString(),stan_string); 
       //const __wchar_t __pin * stan_string = PtrToStringChars(file_name); 
       ofstream outfile; 
       outfile.open("Test.txt", ios::out | ios::app); 
       outfile<<stan_string<<endl; 
       outfile.close(); 

       int number_of_lines = 0; 
       string line,tempLine; 
       ifstream myfile("Test.txt"); 

       while (std::getline(myfile, line)) 
        ++number_of_lines; 
        myfile.close(); 
       ifstream my_file("Test.txt"); 
        for(int i=number_of_lines;i>0;i--) 
       { 
        std::getline(my_file,line); 
        history[i] = line; 

       } 
       my_file.close(); 
       ofstream out_file; 
       string fn="history.txt"; 
       out_file.open(fn.c_str()); 
       for(int i=1;i<=number_of_lines;i++) 
       { 
        out_file<<history[i]<<endl; 
       } 
       out_file.close(); 

       String^ ReadedFile=File::ReadAllText("history.txt"); 
       label1->Text = ReadedFile; 

       ifstream inFile; 
       ofstream oFile; 
       inFile.open("history.txt"); 
       oFile.open("names.txt"); 

       while (getline (inFile,line)){ 
       // cout << line << endl; 
       int pos = line.find_last_of("\\"); 

       if (string::npos != pos){ 
        tempLine = line.erase(0, pos + 1); 
       } 

       oFile << tempLine << endl; 

       } 

       inFile.close(); 
       oFile.close(); 
       no_of_clicks++; 
       ifstream in_file("names.txt"); 
        for(int i=0;i<number_of_lines;i++) 
       { 
        std::getline(in_file,line); 
        record[i] = line; 

       } 
       in_file.close(); 

       if (no_of_clicks>5){ 
        no_of_clicks=no_of_clicks-5; 

       } 
        History_Links(); 
} 
void MarshalString (String^s, string& os) { 
    using namespace Runtime::InteropServices; 
    System::IntPtr pointer = Marshal::StringToHGlobalAnsi(s); 
     char* charPointer = reinterpret_cast<char*>(pointer.ToPointer()); 
    std::string returnString(charPointer, s->Length); 
    Marshal::FreeHGlobal(pointer); 
    os = returnString; 

    } 

public: void History_Links() 
{ 
       string name0=record[0]; 
       string name1=record[1]; 
       string name2=record[2]; 
       string name3=record[3];    
       string name4=record[4]; 
       this->linkLabel1->Text=gcnew String(name0.c_str()); 
       this->linkLabel2->Text=gcnew String(name1.c_str()); 
       this->linkLabel3->Text=gcnew String(name2.c_str()); 
       this->linkLabel4->Text=gcnew String(name3.c_str()); 
       this->linkLabel5->Text=gcnew String(name4.c_str()); 


} 

int history_reader() 
{ 
    int number_of_lines = 0; 
      string line; 
      ifstream myfile("Test.txt"); 

      while (std::getline(myfile, line)) 
       ++number_of_lines; 
       myfile.close(); 
      ifstream my_file("Test.txt"); 
       for(int i=number_of_lines;i>0;i--) 
      { 
       std::getline(my_file,line); 
       history[i] = line; 
      } 
       my_file.close(); 
       return number_of_lines; 
} 


private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) { 
       // Application::Exit(); 
} 

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { 
      std::ofstream oftest; 
      oftest.open("Test.txt", std::ofstream::out | std::ofstream::trunc); 
      oftest.close(); 
      std::ofstream ofhistory; 
      ofhistory.open("history.txt", std::ofstream::out | std::ofstream::trunc); 
      ofhistory.close(); 
      std::ofstream ofnames; 
      ofnames.open("names.txt", std::ofstream::out | std::ofstream::trunc); 
      ofnames.close(); 
      Application::Exit(); 
} 
private: System::Void linkLabel1_LinkClicked(System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e) { 
      history_reader();  
      string str1=history[1]; 
      String^ str2 = gcnew String(str1.c_str()); 
      //axAcroPDF1->src=str2; 
      OpenFileDialog^ opendialog =gcnew OpenFileDialog; 
      opendialog->FileName=str2; 
      //opendialog-> ShowDialog(); 
      axAcroPDF1->src=opendialog->FileName; 
     } 
private: System::Void linkLabel2_LinkClicked(System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e) { 
      history_reader();  
      string str1=history[2]; 
      String^ str2 = gcnew String(str1.c_str()); 
      //axAcroPDF1->src=str2; 
      OpenFileDialog^ opendialog =gcnew OpenFileDialog; 
      opendialog->FileName=str2; 
      //opendialog-> ShowDialog(); 
      axAcroPDF1->src=opendialog->FileName; 
     } 
private: System::Void linkLabel3_LinkClicked(System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e) { 
      history_reader();  
      string str1=history[3]; 
      String^ str2 = gcnew String(str1.c_str()); 
      //axAcroPDF1->src=str2; 
      OpenFileDialog^ opendialog =gcnew OpenFileDialog; 
      opendialog->FileName=str2; 
      //opendialog-> ShowDialog(); 
      axAcroPDF1->src=opendialog->FileName; 
     } 
private: System::Void linkLabel4_LinkClicked(System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e) { 
      history_reader();  
      string str1=history[4]; 
      String^ str2 = gcnew String(str1.c_str()); 
      //axAcroPDF1->src=str2; 
      OpenFileDialog^ opendialog =gcnew OpenFileDialog; 
      opendialog->FileName=str2; 
      //opendialog-> ShowDialog(); 
      axAcroPDF1->src=opendialog->FileName; 
     } 
private: System::Void linkLabel5_LinkClicked(System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e) { 
      history_reader();  
      string str1=history[5]; 
      String^ str2 = gcnew String(str1.c_str()); 
      //axAcroPDF1->src=str2; 
      OpenFileDialog^ opendialog =gcnew OpenFileDialog; 
      opendialog->FileName=str2; 
      //opendialog-> ShowDialog(); 
      axAcroPDF1->src=opendialog->FileName; 
     } 
}; 
} 
+0

Ben sorunu çözdüm, linklabel oluşturulması aslında dinamik değil, dinamik davranıyor ve günlerimi aldı önceki yaklaşımdan çok daha kolay ... –