在C+中动态创建链接标签/按钮队列+;赢表格 < >我想在Winform C++中动态创建一个Link标签/按钮队列。单击按钮时,链接标签将动态显示为link1。如果我第二次单击该按钮,link1应下降到下面,link2应出现在link1的顶部。第三次单击按钮时,link1应下拉,link2应占据link1位置,link3应占据link2位置,依此类推。你可以看到它像一个队列。队列应该有五个项目,第六次单击按钮时,链接1应该消失,链接6应该位于顶部。这种情况持续不断

在C+中动态创建链接标签/按钮队列+;赢表格 < >我想在Winform C++中动态创建一个Link标签/按钮队列。单击按钮时,链接标签将动态显示为link1。如果我第二次单击该按钮,link1应下降到下面,link2应出现在link1的顶部。第三次单击按钮时,link1应下拉,link2应占据link1位置,link3应占据link2位置,依此类推。你可以看到它像一个队列。队列应该有五个项目,第六次单击按钮时,链接1应该消失,链接6应该位于顶部。这种情况持续不断,c++,C++,我已经完成了一些任务。我已经动态创建了链接标签。我想在每次点击按钮时删除所有链接标签,并根据点击次数重新排列它们的位置,从而重新创建它们。但无法删除链接标签。我甚至尝试在另一个linklabel上覆盖一个新的linklabel,但如果我单击被覆盖的linklabel,以前的linklabel不会关闭并显示一些异常错误 `void History_Links() { string name=record[0]; LinkLabel^

我已经完成了一些任务。我已经动态创建了链接标签。我想在每次点击按钮时删除所有链接标签,并根据点击次数重新排列它们的位置,从而重新创建它们。但无法删除链接标签。我甚至尝试在另一个linklabel上覆盖一个新的linklabel,但如果我单击被覆盖的linklabel,以前的linklabel不会关闭并显示一些异常错误
`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;
                  }

}`
#包括“stdafx.h” #包括 #包括 #包括 //#包括 //#包括 //使用名称空间msclr::interop; 名称空间历史跟踪器{ 使用名称空间系统; 使用名称空间System::ComponentModel; 使用名称空间系统::集合; 使用命名空间System::Windows::Forms; 使用名称空间System::Data; 使用名称空间系统::绘图; 使用名称空间系统::IO; 使用名称空间std; 使用命名空间运行时::InteropServices; 字符串历史[1000],记录[50]; //字符串链接_名称[5]={“history1”、“history2”、“history3”、“history4”、“history5”}; int no_of_clicks=0; /// ///表格1摘要 /// public ref类Form1:public System::Windows::Forms::Form { 公众: 表格1(无效) { 初始化组件(); // //TODO:在此处添加构造函数代码 // } 受保护的: /// ///清理所有正在使用的资源。 /// ~Form1() { if(组件) { 删除组件; } } 私有:系统::Windows::窗体::按钮^button1; 私有:AxAcroPDFLib::AxAcroPDF^axAcroPDF1; 私有:系统::Windows::窗体::标签^label1; 私有:系统::Windows::窗体::按钮^button2; 私有:系统::Windows::窗体::LinkLabel ^linkLabel1; 私有:系统::Windows::窗体::链接标签^linkLabel2; 私有:系统::Windows::窗体::链接标签^linkLabel3; 私有:系统::Windows::窗体::链接标签^linkLabel4; 私有:系统::Windows::窗体::链接标签^linkLabel5; 受保护的: 私人: /// ///必需的设计器变量。 /// 系统::组件模型::容器^组件; #pragma区域Windows窗体设计器生成的代码 /// ///设计器支持所需的方法-不修改 ///此方法的内容与代码编辑器一起使用。 /// void初始化组件(void) { System::ComponentModel::ComponentResourceManager^resources=(gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid)); 此->按钮1=(gcnew System::Windows::Forms::Button()); this->axAcroPDF1=(gcnew-AxAcroPDFLib::AxAcroPDF()); 这->标签1=(gcnewsystem::Windows::Forms::Label()); 此->按钮2=(gcnew System::Windows::Forms::Button()); 这->linkLabel1=(gcnew System::Windows::Forms::LinkLabel()); 这->linkLabel2=(gcnew System::Windows::Forms::LinkLabel()); 这->linkLabel3=(gcnew System::Windows::Forms::LinkLabel()); 这->linkLabel4=(gcnew System::Windows::Forms::LinkLabel()); 这->linkLabel5=(gcnew System::Windows::Forms::LinkLabel()); (cli::safe_cast(this->axAcroPDF1))->BeginInit(); 此->SuspendLayout(); // //按钮1 // 此->按钮1->位置=系统::图纸::点(909,11); 此->按钮1->名称=L“按钮1”; 该->按钮1->尺寸=系统::图纸::尺寸(122,37); 此->按钮1->选项卡索引=0; 此->按钮1->Text=L“选择文件”; 此->按钮1->UseVisualStyleBackColor=true; 此->按钮1->Click+=gcnewsystem::EventHandler(此,&Form1::按钮1\u Click); // //axAcroPDF1 // this->axAcroPDF1->Anchor=static_cast(((系统::窗口::窗体::主播样式::顶部|系统::窗口::窗体::主播样式::底部) |系统::Windows::窗体::主持人样式::左) |系统::Windows::窗体::主播样式::右侧); 此->axAcroPDF1->Enabled=true; 此->axAcroPDF1->位置=系统::绘图::点(623,55); 此->axAcroPDF1->名称=L“axAcroPDF1”; this->axAcroPDF1->OcxState=(cli::safe_cast(resources->GetObject(L“axAcroPDF1.OcxState”)); 此->axAcroPDF1->尺寸=系统::图纸::尺寸(515580); 此->axAcroPDF1->TabIndex=1; // //标签1 // 此->标签1->自动调整大小=真; 此->标签1->位置=系统::图纸::点(16,11); 此->标签1->名称=L“标签1”; 该->标签1->尺寸=系统::图纸::尺寸(35,13); 此->标签1->选项卡索引=2; 此->标签1->文本=L“标签1”; this->label1->Click+=gcnewsystem::EventHandler(this,&Form1::label1\u Click); // //按钮2 // 此->按钮2->位置=系统::图纸::点(1037,11); 此->按钮2->名称=L“按钮2”; 该->按钮2->尺寸=系统::图纸::尺寸(101,37); 此->按钮2->选项卡索引=3; 此->按钮
#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;
         }
};
}