Argument Exception - Parameter Invalid (LinkLabel on WinForm)

I wrote a Win Forms application to test how the LinkLabel class works. Everything seems to be in order until I click on the LinkLabel link. Form1.cs code is below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace LinkLabelTest
{
// Editor: code not fully indented - laziness!
public partial class Form1 : Form
{

    public Form1()
    {  
            InitializeComponent();
    }

    private void btnLinkColor_Click(object sender, EventArgs e)
    {
        colorDialog1.ShowDialog();
        llblinkLabel1.LinkColor = colorDialog1.Color;
    }

    private void btnActiveLinkColor_Click(object sender, EventArgs e)
    {
        colorDialog1.ShowDialog();
        llblinkLabel1.ActiveLinkColor = colorDialog1.Color;
    }

    private void btnVisitedLinkColor_Click(object sender, EventArgs e)
    {
        colorDialog1.ShowDialog();
        llblinkLabel1.VisitedLinkColor = colorDialog1.Color;
    }

    private void llblinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {

        try
        {

            if (!(e.Link.Description == null))
            {
                if (e.Button == MouseButtons.Right)
                {
                    PopulateLinkDetails(e.Link);
                }

                if (e.Button == MouseButtons.Left)
                {   
                    e.Link.Visited = true;
                    Debugger.Break();
                }
            }
        }
        catch (Exception exc)
        {
            Debugger.Break();
            MessageBox.Show(exc.Message.ToString());
        }   
    }

    private void PopulateLinkDetails(LinkLabel.Link link)
    {
        textDescription.Text = link.Description;
        textLinkData.Text = (string)link.LinkData;
        textName.Text = link.Name;
        checkBoxEnabled.Checked = link.Enabled;
        checkBoxVisited.Checked = link.Visited;
        nudLinkAreaStart.Value = link.Start;
        nudLinkAreaEnd.Value = link.Length;
    }


    private void Form1_Load(object sender, EventArgs e)
    {
            cmbLinkBehaviour.DataSource = Enum.GetValues(typeof(LinkBehavior));
    }

    private void cmbLinkBehaviour_SelectedIndexChanged(object sender, EventArgs e)
    {
        llblinkLabel1.LinkBehavior = (LinkBehavior)cmbLinkBehaviour.SelectedItem;
    }

    private void AddLink_Click(object sender, EventArgs e)
    {
            LinkLabel.Link link = new LinkLabel.Link();

            link.Description = textDescription.Text.ToString();
            link.LinkData = textLinkData.Text.ToString();
            link.Name = textName.Text.ToString();
            link.Enabled = checkBoxEnabled.Checked;
            link.Visited = checkBoxVisited.Checked;
            link.Start = (int)nudLinkAreaStart.Value;
            link.Length = (int)nudLinkAreaEnd.Value;

            llblinkLabel1.Links.Add(link);
    }

}
}

      

The stack trace is below:

at System.Drawing.Region.GetHrgn (Graphics g) at System.Windows.Forms.Control.Invalidate (Region region, Boolean invalidateChildren) at System.Windows.Forms.LinkLabel.InvalidateLink (link link) to System.Windows.Forms. LinkLabel.OnGotFocus (EventArgs e) at System.Windows.Forms.Control.WmSetFocus (Message & m) at System.Windows.Forms.Control.WndProc (Message & m) at System.Windows.Forms.Label.WndProc (Message & m) at System.Windows.Forms.LinkLabel.WndProc (Message & msg) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m ) at System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.SetFocus (HandleRef hWnd) at System.Windows.Forms.Control.FocusInternal () at System.Windows.Forms.LinkLabel.OnMouseDown (MouseEventArgs e) at System.Windows.Forms.Control.WmMouseDown (Message & m, MouseButtons, Int32 clicks) at System.Windows.Forms.Control.WndProc (Message & m) at System.Windows.Forms.Label.WndProc (Message & m) at System.Windows.Forms.LinkLabel.WndProc (Message & msg) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m) at System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) (at System.Windows.Forms.Unsafe.DNispativeMethoess MSG & msg) in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (Int32 dwComponentID, Int32 reason,Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32 reason, ApplicationContext context) at System.Windows.Forms. Run (Form mainForm) in LinkLabelTest.Program.Main () in C: \ Users \ Tony \ Documents \ Visual Studio 2008 \ Projects \ LinkLabelTest \ LinkLabelTest \ Program.cs: line 22

Below is the code generated by the constructor:

using System.Diagnostics;

namespace LinkLabelTest
{
// Editor: code not fully indented - laziness again!
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
            if (disposing && (components != null))
            {
                components.Dispose();
            }

            base.Dispose(disposing);
    }

    #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>
    private void InitializeComponent()
    {

        this.llblinkLabel1 = new System.Windows.Forms.LinkLabel();
        this.colorDialog1 = new System.Windows.Forms.ColorDialog();
        this.btnLinkColor = new System.Windows.Forms.Button();
        this.btnActiveLinkColor = new System.Windows.Forms.Button();
        this.btnVisitedLinkColor = new System.Windows.Forms.Button();
        this.nudLinkAreaStart = new System.Windows.Forms.NumericUpDown();
        this.lbllabel1 = new System.Windows.Forms.Label();
        this.nudLinkAreaEnd = new System.Windows.Forms.NumericUpDown();
        this.lbllabel2 = new System.Windows.Forms.Label();
        this.cmbLinkBehaviour = new System.Windows.Forms.ComboBox();
        this.lbllabel3 = new System.Windows.Forms.Label();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.textDescription = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.textName = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.textLinkData = new System.Windows.Forms.TextBox();
        this.label4 = new System.Windows.Forms.Label();
        this.checkBoxEnabled = new System.Windows.Forms.CheckBox();
        this.AddLink = new System.Windows.Forms.Button();
        this.btnAmendLink = new System.Windows.Forms.Button();
        this.checkBoxVisited = new System.Windows.Forms.CheckBox();
        this.panel1 = new System.Windows.Forms.Panel();
        this.textBox1 = new System.Windows.Forms.TextBox();
        ((System.ComponentModel.ISupportInitialize)(this.nudLinkAreaStart)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.nudLinkAreaEnd)).BeginInit();
        this.groupBox1.SuspendLayout();
        this.panel1.SuspendLayout();
        this.SuspendLayout();
        // 
        // llblinkLabel1
        // 
        this.llblinkLabel1.AutoSize = true;
        this.llblinkLabel1.Location = new System.Drawing.Point(13, 27);
        this.llblinkLabel1.Name = "llblinkLabel1";
        this.llblinkLabel1.Size = new System.Drawing.Size(96, 13);
        this.llblinkLabel1.TabIndex = 0;
        this.llblinkLabel1.TabStop = true;
        this.llblinkLabel1.Text = "This is a link label !";
        this.llblinkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llblinkLabel1_LinkClicked);
        // 
        // btnLinkColor
        // 
        this.btnLinkColor.Location = new System.Drawing.Point(0, 19);
        this.btnLinkColor.Name = "btnLinkColor";
        this.btnLinkColor.Size = new System.Drawing.Size(93, 23);
        this.btnLinkColor.TabIndex = 2;
        this.btnLinkColor.Text = "LinkColor";
        this.btnLinkColor.UseVisualStyleBackColor = true;
        this.btnLinkColor.Click += new System.EventHandler(this.btnLinkColor_Click);
        // 
        // btnActiveLinkColor
        // 
        this.btnActiveLinkColor.Location = new System.Drawing.Point(0, 48);
        this.btnActiveLinkColor.Name = "btnActiveLinkColor";
        this.btnActiveLinkColor.Size = new System.Drawing.Size(93, 23);
        this.btnActiveLinkColor.TabIndex = 3;
        this.btnActiveLinkColor.Text = "ActiveLinkColor";
        this.btnActiveLinkColor.UseVisualStyleBackColor = true;
        this.btnActiveLinkColor.Click += new System.EventHandler(this.btnActiveLinkColor_Click);
        // 
        // btnVisitedLinkColor
        // 
        this.btnVisitedLinkColor.Location = new System.Drawing.Point(0, 77);
        this.btnVisitedLinkColor.Name = "btnVisitedLinkColor";
        this.btnVisitedLinkColor.Size = new System.Drawing.Size(93, 23);
        this.btnVisitedLinkColor.TabIndex = 4;
        this.btnVisitedLinkColor.Text = "VisitedLinkColor";
        this.btnVisitedLinkColor.UseVisualStyleBackColor = true;
        this.btnVisitedLinkColor.Click += new System.EventHandler(this.btnVisitedLinkColor_Click);
        // 
        // nudLinkAreaStart
        // 
        this.nudLinkAreaStart.Location = new System.Drawing.Point(152, 64);
        this.nudLinkAreaStart.Maximum = new decimal(new int[] {
        21,
        0,
        0,
        0});
        this.nudLinkAreaStart.Name = "nudLinkAreaStart";
        this.nudLinkAreaStart.Size = new System.Drawing.Size(66, 20);
        this.nudLinkAreaStart.TabIndex = 6;
        // 
        // lbllabel1
        // 
        this.lbllabel1.AutoSize = true;
        this.lbllabel1.Location = new System.Drawing.Point(224, 71);
        this.lbllabel1.Name = "lbllabel1";
        this.lbllabel1.Size = new System.Drawing.Size(74, 13);
        this.lbllabel1.TabIndex = 7;
        this.lbllabel1.Text = "Link area start";
        // 
        // nudLinkAreaEnd
        // 
        this.nudLinkAreaEnd.Location = new System.Drawing.Point(152, 99);
        this.nudLinkAreaEnd.Maximum = new decimal(new int[] {
        21,
        0,
        0,
        0});
        this.nudLinkAreaEnd.Name = "nudLinkAreaEnd";
        this.nudLinkAreaEnd.Size = new System.Drawing.Size(67, 20);
        this.nudLinkAreaEnd.TabIndex = 8;
        // 
        // lbllabel2
        // 
        this.lbllabel2.AutoSize = true;
        this.lbllabel2.Location = new System.Drawing.Point(224, 105);
        this.lbllabel2.Name = "lbllabel2";
        this.lbllabel2.Size = new System.Drawing.Size(83, 13);
        this.lbllabel2.TabIndex = 9;
        this.lbllabel2.Text = "Link area length";
        // 
        // cmbLinkBehaviour
        // 
        this.cmbLinkBehaviour.FormattingEnabled = true;
        this.cmbLinkBehaviour.Location = new System.Drawing.Point(0, 106);
        this.cmbLinkBehaviour.Name = "cmbLinkBehaviour";
        this.cmbLinkBehaviour.Size = new System.Drawing.Size(93, 21);
        this.cmbLinkBehaviour.TabIndex = 10;
        this.cmbLinkBehaviour.SelectedIndexChanged += new System.EventHandler(this.cmbLinkBehaviour_SelectedIndexChanged);
        // 
        // lbllabel3
        // 
        this.lbllabel3.AutoSize = true;
        this.lbllabel3.Location = new System.Drawing.Point(-3, 130);
        this.lbllabel3.Name = "lbllabel3";
        this.lbllabel3.Size = new System.Drawing.Size(78, 13);
        this.lbllabel3.TabIndex = 11;
        this.lbllabel3.Text = "Link Behaviour";
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.btnLinkColor);
        this.groupBox1.Controls.Add(this.btnActiveLinkColor);
        this.groupBox1.Controls.Add(this.lbllabel3);
        this.groupBox1.Controls.Add(this.btnVisitedLinkColor);
        this.groupBox1.Controls.Add(this.cmbLinkBehaviour);
        this.groupBox1.Location = new System.Drawing.Point(12, 57);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(124, 155);
        this.groupBox1.TabIndex = 13;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "THE link label";
        // 
        // textDescription
        // 
        this.textDescription.Location = new System.Drawing.Point(152, 136);
        this.textDescription.Name = "textDescription";
        this.textDescription.Size = new System.Drawing.Size(100, 20);
        this.textDescription.TabIndex = 14;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(259, 143);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(60, 13);
        this.label1.TabIndex = 15;
        this.label1.Text = "Description";
        // 
        // textName
        // 
        this.textName.Location = new System.Drawing.Point(152, 163);
        this.textName.Name = "textName";
        this.textName.Size = new System.Drawing.Size(100, 20);
        this.textName.TabIndex = 16;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(262, 170);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(35, 13);
        this.label2.TabIndex = 17;
        this.label2.Text = "Name";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(16, 11);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(283, 13);
        this.label3.TabIndex = 18;
        this.label3.Text = "Right Click on a link to retrieve properties. Left Click to visit";
        // 
        // textLinkData
        // 
        this.textLinkData.Location = new System.Drawing.Point(152, 191);
        this.textLinkData.Name = "textLinkData";
        this.textLinkData.Size = new System.Drawing.Size(100, 20);
        this.textLinkData.TabIndex = 19;
        // 
        // label4
        // 
        this.label4.AutoSize = true;

        this.label4.Location = new System.Drawing.Point(262, 198);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(53, 13);
        this.label4.TabIndex = 20;
        this.label4.Text = "Link Data";
        // 
        // checkBoxEnabled
        // 
        this.checkBoxEnabled.AutoSize = true;
        this.checkBoxEnabled.Location = new System.Drawing.Point(151, 218);
        this.checkBoxEnabled.Name = "checkBoxEnabled";
        this.checkBoxEnabled.Size = new System.Drawing.Size(65, 17);
        this.checkBoxEnabled.TabIndex = 21;
        this.checkBoxEnabled.Text = "Enabled";
        this.checkBoxEnabled.UseVisualStyleBackColor = true;
        // 
        // AddLink
        // 
        this.AddLink.Location = new System.Drawing.Point(385, 64);
        this.AddLink.Name = "AddLink";
        this.AddLink.Size = new System.Drawing.Size(75, 23);
        this.AddLink.TabIndex = 22;
        this.AddLink.Text = "Add Link";
        this.AddLink.UseVisualStyleBackColor = true;
        this.AddLink.Click += new System.EventHandler(this.AddLink_Click);
        // 
        // btnAmendLink
        // 
        this.btnAmendLink.Enabled = false;
        this.btnAmendLink.Location = new System.Drawing.Point(385, 99);
        this.btnAmendLink.Name = "btnAmendLink";
        this.btnAmendLink.Size = new System.Drawing.Size(75, 23);
        this.btnAmendLink.TabIndex = 23;
        this.btnAmendLink.Text = "AmendLink";
        this.btnAmendLink.UseVisualStyleBackColor = true;
        // 
        // checkBoxVisited
        // 
        this.checkBoxVisited.AutoSize = true;
        this.checkBoxVisited.Location = new System.Drawing.Point(151, 241);
        this.checkBoxVisited.Name = "checkBoxVisited";
        this.checkBoxVisited.Size = new System.Drawing.Size(57, 17);
        this.checkBoxVisited.TabIndex = 24;
        this.checkBoxVisited.Text = "Visited";
        this.checkBoxVisited.UseVisualStyleBackColor = true;
        // 
        // panel1
        // 
        this.panel1.Controls.Add(this.textBox1);
        this.panel1.Location = new System.Drawing.Point(474, 13);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(200, 239);
        this.panel1.TabIndex = 25;
        // 
        // textBox1
        // 
        this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.textBox1.Location = new System.Drawing.Point(0, 0);
        this.textBox1.Multiline = true;
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(200, 239);
        this.textBox1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(704, 264);
        this.Controls.Add(this.panel1);
        this.Controls.Add(this.checkBoxVisited);
        this.Controls.Add(this.btnAmendLink);
        this.Controls.Add(this.AddLink);
        this.Controls.Add(this.checkBoxEnabled);
        this.Controls.Add(this.label4);
        this.Controls.Add(this.textLinkData);
        this.Controls.Add(this.label3);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.textName);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.textDescription);
        this.Controls.Add(this.groupBox1);
        this.Controls.Add(this.lbllabel2);
        this.Controls.Add(this.nudLinkAreaEnd);
        this.Controls.Add(this.lbllabel1);
        this.Controls.Add(this.nudLinkAreaStart);
        this.Controls.Add(this.llblinkLabel1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.nudLinkAreaStart)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.nudLinkAreaEnd)).EndInit();
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.panel1.ResumeLayout(false);
        this.panel1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.LinkLabel llblinkLabel1;
    private System.Windows.Forms.ColorDialog colorDialog1;
    private System.Windows.Forms.Button btnLinkColor;
    private System.Windows.Forms.Button btnActiveLinkColor;
    private System.Windows.Forms.Button btnVisitedLinkColor;
    private System.Windows.Forms.NumericUpDown nudLinkAreaStart;
    private System.Windows.Forms.Label lbllabel1;
    private System.Windows.Forms.NumericUpDown nudLinkAreaEnd;
    private System.Windows.Forms.Label lbllabel2;
    private System.Windows.Forms.ComboBox cmbLinkBehaviour;
    private System.Windows.Forms.Label lbllabel3;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.TextBox textDescription;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textName;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textLinkData;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.CheckBox checkBoxEnabled;
    private System.Windows.Forms.Button AddLink;
    private System.Windows.Forms.Button btnAmendLink;
    private System.Windows.Forms.CheckBox checkBoxVisited;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.TextBox textBox1;
}
}

      

Please someone can advise. The problem is that there is no window handle for the LinkLabel when its Paint event occurs.

Thank you very much in advance

Tony

0


source to share


2 answers


The following code example shows how you can set the color of the label from the label ColorDialog

:



using (var colorDlg = new ColorDialog())
{
    if (colorDlg.ShowDialog() == DialogResult.OK)
    {
       llblinkLabel1.LinkColor = colorDlg.Color;
    }
}

      

+1


source


    private void AddLink_Click(object sender, EventArgs e)
    {
        // do proper validation and add only proper links
        // this will help you to avoid the exception 
        if (!string.IsNullOrEmpty(textLinkData.Text) && 
            nudLinkAreaEnd.Value > 0 && 
            nudLinkAreaStart.Value >= 0 && 
            nudLinkAreaStart.Value < llblinkLabel1.Text.Length)
       {
           LinkLabel.Link link = new LinkLabel.Link();
           link.Description = textDescription.Text.ToString();
           link.LinkData = textLinkData.Text.ToString();
           link.Name = textName.Text.ToString();
           link.Enabled = checkBoxEnabled.Checked;
           link.Visited = checkBoxVisited.Checked;
           link.Start = (int)nudLinkAreaStart.Value;
           link.Length = (int)nudLinkAreaEnd.Value;
           try
           {
               llblinkLabel1.Links.Add(link); 
           }
           catch (InvalidOperationException exception) // links can't overlap 
           {
               MessageBox.Show("Links are overlaping");
           }

       }

      



0


source







All Articles