WinForm中有一个PictureBox控件,通过Panel控件来做滚动条,现在想根据图片上的某点坐标来动态生成一个Label到该坐标上,请问如何实现?谢谢

解决方案 »

  1.   


           private void AddLabelToPanle(string txt, Panel aPanel, int LblTop, int LblLeft)
            {
                Label lbl = new Label();
                lbl.Text = txt;
                lbl.Top = LblTop;
                lbl.Left = LblLeft;
                aPanel.Controls.Add(lbl);
            }
      

  2.   

    To dirt:那Label响应的事件呢?怎么添加?
      

  3.   

    另外,我按照你的代码添加了一个Label,可怎么看不到?难道是被图片盖住了?如何解决?谢谢
      

  4.   

    赋值可以这样
    private void AddLabelToPanle(string txt, Panel aPanel, int LblTop, int LblLeft)
            {
                Label lbl = new Label();
                lbl.Text = txt;
                lbl.Top = LblTop;
                lbl.Left = LblLeft;
                aPanel.Controls.Add(lbl);
    lbl.Click += new System.EventHandler(lbl_Click);//在楼上代码的基础上增加这句
            }
    //在同一个代码文件中实现响应
     private void lbl_Click(object sender, EventArgs e)
            {        }
      

  5.   

    还有一点,就是“Label   lbl   =   new   Label(); ”这里面的lbl是个临时变量,如果要响应事件,
    应当延长它的生存期,改用类的成员等方法会好些
      

  6.   

    To Gao_TF:谢谢,那这个问题是什么原因呢?我按照你的代码添加了一个Label,可怎么看不到?难道是被图片盖住了?如何解决?谢谢
      

  7.   

    那有没有可能通过改变控件的??属性来显示呢?网页中有一个Z-index,可以控件哪个层显示在上面,哪个在下面,不晓得winform中应该如何解决?
      

  8.   

    To   Gao_TF: 
    lbl.Click   +=   new   System.EventHandler(lbl_Click);//在楼上代码的基础上增加这句 
    错误 1 “lbl_Click”的重载均与委托“System.EventHandler”不匹配
      

  9.   

    我刚试过啊,没有报这样的错误阿!namespace WindowsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.button1 = new System.Windows.Forms.Button();
                this.panel1 = new System.Windows.Forms.Panel();
                this.SuspendLayout();
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(228, 51);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(41, 12);
                this.label1.TabIndex = 0;
                this.label1.Text = "label1";
                this.label1.Click += new System.EventHandler(this.label1_Click);
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(256, 95);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(38, 51);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // panel1
                // 
                this.panel1.Location = new System.Drawing.Point(12, 66);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(212, 167);
                this.panel1.TabIndex = 2;
                // 
                // Form1
                // 
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.panel1);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.label1);
                this.Name = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Panel panel1;
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Media;
    using System.Threading; namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            Label lbl = null;
            public Form1()
            {
                InitializeComponent();
              
            }
            private void label1_Click(object sender, EventArgs e)
            {        }
            private void lbl_Click(object sender, EventArgs e)//相应事件
            {        }
            private void button1_Click(object sender, EventArgs e)//动态添加
            {
                lbl = new Label();
                lbl.Text = "2222";
                lbl.Top = 10;
                lbl.Left = 20;
                
                panel1.Controls.Add(lbl);
                lbl.Click += new System.EventHandler(lbl_Click);//在楼上代码的基础上增加这句 
             
            }
        }
    }
    这是我试验的代码
      

  10.   

    是我弄错了,少写了object   sender,   EventArgs   e谢谢大家,问题解决,结贴