5 Replies - 755 Views - Last Post: 12 October 2012 - 03:53 PM Rate Topic: -----

#1 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

where to place the eventHandler of a form object

Posted 12 October 2012 - 11:28 AM

oks

first of all i'm using microsoft visual C# 2010 express

this is about a form
i don't want to dynamically generate a form
but for some textBoxes to dynamically display the result of me messing around with the controls

so i have a listBox

http://msdn.microsof...ms.listbox.aspx

filled up with n quantity of double numbers

and i want to simply know what has the user selected from the list and display it on a textBox

i used this SelectedIndexChanged event and GetSelected method like this


private void SelectedIndexChanged(object sender, EventArgs e)
        {
            c = double.Parse(k.GetSelected(k.SelectedIndex).ToString());
            W.Text = c.ToString();
        }
}



this compiles, but i'm not quite sure it makes sense:
k is my listBox object, it contains an array of strings
so k.SelectedIndex is the index of whatever element of the strings array the user chose
i'm giving it to the k.GetSelected so i get the element, and them i'm changing it to string so i can make it a double...?
anyways i have my global double with a value i can somewhere else mess with
but in order to display it i have to change it back to string so the textBox recognizes it?

i hope i got this right

but anyways, this won't do a thing if i run it, because i know i have to place an eventHandler somewhere...
the point is where? the visual express makes a lot of files

so i have one "file" called Form1.Designer.cs where there's all the stuff about the form


namespace BS
{
    partial class BS
    {
        /// <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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BattSelect));
         
//[... lots of labels, boxes and stuff in here ...]

            this.k = new System.Windows.Forms.ListBox();
            this.W = new System.Windows.Forms.TextBox();

//[... lots of those labels, boxes and stuff parameters in here ...]

            // kVA
            // 
            this.k.FormattingEnabled = true;
            this.k.Items.AddRange(new object[] {
            "1",
            "2.7",
            "3.5",
            "6",
            "10.3",
            "15.8",
            "20.4",
            "30.6",
            "40.5",
            "50.4",
            "60.3",
            "80.7"});
            this.k.Location = new System.Drawing.Point(117, 3);
            this.k.Name = "k";
            this.k.Size = new System.Drawing.Size(53, 17);
            this.k.TabIndex = 8;
            // 
            // W
            // 
            this.W.Location = new System.Drawing.Point(117, 135);
            this.W.Name = "W";
            this.W.Size = new System.Drawing.Size(59, 20);
            this.W.TabIndex = 10;
            this.W.Text = "-";
        
//[... things about the layout...]

        #endregion

//[... declarations ...]

        private System.Windows.Forms.ListBox k;
        private System.Windows.Forms.TextBox W;
    }
}




and that's it

so according to this site

http://csharpdotnetf...t-handling.html

i believe i need to place

EventHandler(this.k.SelectedIndexChanged); //with or without () at the end is the same



in this file, next to all other stuff about k, but if i do that, this happens

Error	1	The name 'EventHandler' does not exist in the current context	
Error	2	The event 'System.Windows.Forms.ListBox.SelectedIndexChanged' can only appear on the left hand side of += or -=



the first error makes me think i need to add some headers to the file
which ones and to which of all my files, i have no idea

the second error...
i found this about someone having more or less the same problem somewhere, but i lost the link, sorry

"To reference static fields you need to use the class name not the name of an instance.
e.g. int i = Pay.Click; .
The error you are getting is because ppay.Click refers to the Click event that can only be set."

and that info just leaves me the same as before. i don't understand what that means
at any case that there is not the way to link the k listbox with the event handler, but then how do i do that properly?

please, help me out
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: where to place the eventHandler of a form object

#2 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: where to place the eventHandler of a form object

Posted 12 October 2012 - 11:38 AM

Long post...short answer.

Attached Image

In that event list if you were to double click in the empty space it will automatically generate a handler for you and name it based on the controls name. Then you just fill in the method. But you did it backwards and made the method first. So just select it from the dropdown.

This post has been edited by CodeGrappler: 12 October 2012 - 11:39 AM

Was This Post Helpful? 2
  • +
  • -

#3 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: where to place the eventHandler of a form object

Posted 12 October 2012 - 01:35 PM

oks, done...
thanks for your quick replay

well, now it does compile but i got other error
so i have this


private void SelectedIndexChanged(object sender, EventArgs e)
        {
            c = double.Parse(k.GetSelected(k.SelectedIndex).ToString());
            W.Text = c.ToString();
        }
}



but so it happens that the double parse fails

to find out i did this


private void SelectedIndexChanged(object sender, EventArgs e)
        {
            String tempS = k.GetSelected(k.SelectedIndex).ToString();
            //c = Double.Parse(tempS);
            W.Text = " "+tempS;
         }




as i found out that getSelected doesn't returns what is selected, only if something is selected
the parse to double fails because getSelected returned True and that's what W showed

so i'm now looking for a way to get the selected value from the list box

regards
Was This Post Helpful? 0
  • +
  • -

#4 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: where to place the eventHandler of a form object

Posted 12 October 2012 - 01:51 PM

so now i did this

private void SelectedValueChanged(object sender, EventArgs e)
        {
            if (k.SelectedIndex != -1)
            {
                 W.Text = kVA.SelectedValue.ToString();
            }
        }




and it shows a NullReferenceException
reference to object not stablished as instance of an object


...
Was This Post Helpful? 0
  • +
  • -

#5 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: where to place the eventHandler of a form object

Posted 12 October 2012 - 03:12 PM

nevermind

it's done!

thanks a lot
private void kVA_SelectedValueChanged(object sender, EventArgs e)
        {
            if (kVA.SelectedIndex > -1)
            {
                W.Text = kVA.SelectedItem.ToString();
            }
        }


Was This Post Helpful? 0
  • +
  • -

#6 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: where to place the eventHandler of a form object

Posted 12 October 2012 - 03:53 PM

Just came to check on this thread. Glad you got it figured out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1