CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2007
    Posts
    22

    listbox not showing up

    the listbox isn't showing up here for some reason. I want to have a pull down menu between Ontario and Quebec which outcome is affected by the user's choice.

    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;

    namespace distancecalc
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
    Application.Exit();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void CalculateButton_Click(object sender, EventArgs e)
    {
    if (textBox1.Text != "")
    {
    string customers;
    double costperyear; // will be affected by whether user chooses Ontario or Quebec
    if (listBox1.SelectedItem == "Ontario") //assuming you put strings into the list box
    costperyear = 0.1001;
    else
    costperyear = 0.0663; // rate dependint on the choice user choses on their listbox

    double KwhRating = 0.0;
    double years = 0.0;
    double.TryParse(textBox1.Text, out KwhRating);
    double.TryParse(textBox2.Text, out years);

    double kwcostOntario = 0.1001;
    double kwcostQuebec = 0.0663;
    double totalcost = KwhRating * years * costperyear;

    textBox3.Text = totalcost.ToString();
    }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    listBox1.Items.Add("Ontario");
    listBox1.Items.Add("Quebec");
    //kwh prices in Ontario are $0.1001
    //kwh prices in Quebec $0.0663
    // I am tring to make it so that if the user selecs "Ontario
    //then "costperyear" would = 0.1001 the list box also isnt showing up
    //but the problem is the listbox isnt even showing up, it is blank


    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }
    }
    }

  2. #2
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Re: listbox not showing up

    Quote Originally Posted by chipninja
    the listbox isn't showing up here for some reason. I want to have a pull down menu between Ontario and Quebec which outcome is affected by the user's choice.
    What do you mean by this ? listbox isn't showing up here ? do u mean you do not see the listbox on the designer ? or you dont get the reference of listbox when you type ListBox1 ?
    Regards,
    MMH
    Rate my post if you find it usefull.

  3. #3
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: listbox not showing up

    As you added items into the listBox when SelectedIndexChanged events is triggerred, that is not going to trigger until you have items in listbox and select these items.

  4. #4
    Join Date
    Dec 2007
    Posts
    22

    Re: listbox not showing up

    what I mean is that the lsitbox options are not showing up in my GUI program when I run it. Instead I get a blank space. Should I put the ".Add" in the "Calculate" button??

  5. #5
    Join Date
    Feb 2007
    Location
    Cat Square (near Charlotte)
    Posts
    16

    Re: listbox not showing up

    Whenever I use VS to put entries into a listbox, it does it for me in the InitializeComponent() code that is at the top of your listing.

    If anything, put your

    Code:
    listBox1.Items.Add("Ontario");
    listBox1.Items.Add("Quebec");
    items just after the InitializeComponent() call that should make it work.
    C# - Visual Studio 2005
    Frameworks 2.0

  6. #6
    Join Date
    Dec 2007
    Posts
    22

    Re: listbox not showing up

    omg thank you
    that was so simple, yet I couldn't find the solution anywhere. I guess the initialize is used whenver you have a user driven type of tool in the GUI that needs to be selected

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured