Member Avatar for Annieken

Hi

1)
I'm trying to display a list in a listbox. But I only see the last entered item in the listbox.
This is my code:

List<BurstKlasse> Burst = new List<BurstKlasse>();
                Burst.Add(new BurstKlasse(lengte,CPU_Burst));
                listBox2.DataSource = Burst;

2)
My second problem is that I can't uncheck the radiobuttons. If the user has checked it, I can not uncheck it when he presses a button.

3)
In one way or another I can't put a selected item from a combobox in a variable and then add it in the list. The code I have is:

int PNr = int.Parse(textBox1.Text);
                int PAankomst = int.Parse(textBox2.Text);
                String PNaam = textBox3.Text.ToString();
                //ProcesKlasse.PState Pstatus = comboBox1.SelectedValue.ToString();

                List<ProcesKlasse> Proces = new List<ProcesKlasse>();

                Proces.Add(new ProcesKlasse(PNr, PAankomst, PNaam/*, Pstatus*/));
                listBox1.DataSource = Proces;

The ProcesKlasse is a class not in the form but in the same solution.

I would very pleased with any answers!
Thank you

Annieken

Recommended Answers

All 12 Replies

1. Try this until I stay on a pc has Visual Studio

List<BurstKlasse> Burst = new List<BurstKlasse>();
Burst.Add(new BurstKlasse(lengte,CPU_Burst));
void AddIntoList(ListBox list, List<Burst> items)
{
//clear the list
list.Item.Clear();
//fill it
foreach(Burst b in items.ToArray())
{
list.items.Add(b);
}
}
Tip:
[B]To show Brust objects in a nice way (their names, their names and their values, etc) you must override its ToString method[/B]

2.
In button click handler:

[radiobutton].Checked = false;

3.

String PNaam = textBox3.Text.ToString();

don't use ToString you don't need it!

Say when user selects an item in the comboBox and presses on a button called MoveToList the item moves.
So in MoveToList button click event handler you need to get the item selected in the combobox and add it to the list so

comboBox.items.Add(list.SelectedItem);
//you can remove the item from the comboBox after that
comboBox.items.Remove(comboBox.SelectedItem);

P.S: I don't have VS right now I am just writting code and I hope I didn't do a lot of mistakes and I wish I could help you!

Member Avatar for Annieken

Hi

I'm glad that you answered.
I've tried your code but it doesn't work. The first question is a bit changed. I've different classes and a subclasses from the base class. The base class is BurstKlasse with a datamember int lengte. I've got two subclasses: IOBurst and CPUBurst with each a datamember soort (which means kind; sort) I've inherent both with the base class. So now in the Form1.cs I've entered this

List<BurstKlasse> Burst = new List<BurstKlasse>();
Burst.Add(new BurstKlasse(lengte));
foreach(Burst b in Burst.ToArray())
{
listBox1.items.Add(b);
}

But I've got many errors on this.

For the second question I thank you for the code, I've found it yesterdayevening.

For the next question: I don't think you really understand what I'm trying to do:

The user must fill one textbox in with lengte (integer) and has to check a radiobutton. I want to display the integer and the text of the radiobutton in the listbox2. But if the user wants to enter another integer and check another radiobutton it should show in the listbox2. But also the first entered data has to show in the listbox.
The user has to click on a button if he wants to add the data into the listbox2.

If you have any other idea's :)

I am not real sure on what you are trying to do.
However, maybe this will help.

Each time that you instantiate a new Burst collection variable, then assign it a value, and then set the datasource of the list box to the new Burst collection (discarding the old one). This means that the listbox will use the new Burst as its datasource which only has the one item.
If you want to have it add multiple items, then you have to move the instantiation of the Burst collection outside of the methods, and add to it from a method:

private List<BurstKlasse> Burst = new List<BurstKlasse>();

 private void button2_Click(object sender, EventArgs e)
        {
            
            int lengt = int.Parse(lengte.Text);
            string CPU_Burst;
            if (radioButton1.Checked)
                CPU_Burst = radioButton1.Text;
            else
                CPU_Burst = radioButton2.Text;

            Burst.Add(new BurstKlasse(lengt, CPU_Burst));
            listBox2.DataSource = null;
            listBox2.DataSource = Burst;
        }

Note that you have to set the DataSource to null before reassigning it. This forces the component to re-evaluate the schema.

I am sure you have a good reason for setting the listbox datasource that was not provided in your message. But, if you don't you could just as easily have added the data directly to the listbox without using the datasource method at all.

Hope this helps,
Jerry

Member Avatar for Annieken

Oke, thank you, the code works fine! But I've changed now some things. Burst is a base class and he has 2 subclasses: IO-Burst and CPU-Burst... In these classes they have to say it is a CPU-Burst or an IO-Burst. Only I can't refer to the classes.
To test I put the CPU_Burst in documentation. But now it has to go from the subclass to the form1.... Do you have any idea's?
I've already tried with BurstKlasse.IO-Burst.ToString(); but it doesn't work.

I think it would be best if you posted the 3 classes source code. To get ToString() from a class, I assume you have override'n the ToString() method ?

I am thinking that the two subclasses are simple classes inside your base class ? or do they inherit from the base class ?
If inherited, did you instantiate them or the base class ?

// Jerry

Member Avatar for Annieken

The first class (BurstKlasse) is the base class

using System;
using System.Collections.Generic;
using System.Text;

namespace eindwerk
{
    public class BurstKlasse 
    {
        public int lengte;
       
        public BurstKlasse(int l)
        {
            Id = l;  
        }

        public int Id
        {
            get
            {
                return lengte;
            }
            set
            {
                lengte = value;
            }
        }
    
        public override string ToString()
        {
            string uit;
            uit = "Lengte: " + lengte + " ";
            return uit;
        }
    }
}

The second class, the CPU-Burst

using System;
using System.Collections.Generic;
using System.Text;

namespace eindwerk
{
    /*
    class CPUBurst: BurstKlasse
    {
        private String soort = "CPU-Burst";

        public CPUBurst(String soort)
        {
            base.ToString();
            //super(lengte);
            Id = soort;
        }
        
        public String Id
        {
            get
            {
                return soort;
            }
            set
            {
                soort = value;
            }
        }

        public override string ToString()
        {
            //base(lengte);
            string uit;
            uit = "Soort: " + soort;
            return uit;
        }
    }
     * */
}

The last class, the IO-Burst

using System;
using System.Collections.Generic;
using System.Text;

namespace eindwerk
{
    /*
    public abstract class IOBurst: BurstKlasse
    {
        private String soort;

        public IOBurst(String soort)
        {
            base.ToString();
            Id = soort;
        }
        
        public String Id
        {
            get
            {
                return soort;
            }
            set
            {
                soort = value;
            }
        }

        public override string ToString()
        {
            //base(lengte);
            string uit;
            uit = "Soort: " + soort;
            return uit;
        }

        public String getSoort()
        {
            return soort;
        }

        public void setSoort(String soort)
        {
            this.soort = soort;
        }
    }
     */
}

Like you see, I'm inherited them from the base class. But I'm wondering how I can (in the Form1) force the Burst.Add(new BurstKlasse(lengte, CPU_Burst));
In the CPU_Burst I would like the name of the kind of the Burst (IO-Burst or CPU-Burst). But I don't really know how to bind this with the CPU_Burst in the Burst.Add(...)

k,

Did you really intend on abstracting the IOBurst class ? that should only be used on the base class, and causes it to not be a class that you can instantiate. You should remove it.

I don't see anything that indicates a reason for subclassing these two classes from BurstKlasse. There are no inherited methods, or properties being used (that I can tell).

To instantiate either of these "subclasses" you do not need to preface them with BurstKlasse.

IOBurst ib = new IOBurst(value);
textBox1.Text = ib.ToString();


It is hard to tell what you intend (sorry I am missing it). Maybe you want the BurstKlasse class to have two properties. One of CPUBurst and one of IOBurst, and want the BurstKlasse instance to tell you which one you are using.

public class BurstKlasse
    {
        public int lengte;
        private CPUBurst cpub = null;
        private IOBurst iob = null;

        public BurstKlasse(int l,CPUBurst cpuBurst)
        {
            Id = l;
            this.cpub = cpuBurst;
        }
        public BurstKlasse(int l, IOBurst ioBurst)
        {
            Id = l;
            this.iob = ioBurst;
        }


        public int Id
        {
            get
            {
                return lengte;
            }
            set
            {
                lengte = value;
            }
        }

        public override string ToString()
        {
            string uit = "Lengte: " + lengte + " ";
            if(cpub != null)
                uit += cpub.ToString();
            if(iob != null)
                uit += iob.ToString();
            return uit;
        }
    }

    public class CPUBurst
    {
        private String soort = "CPU-Burst";

        public CPUBurst(String soort)
        {
            base.ToString();
            //super(lengte);
            Id = soort;
        }

        public String Id
        {
            get
            {
                return soort;
            }
            set
            {
                soort = value;
            }
        }

        public override string ToString()
        {
            //base(lengte);
            string uit;
            uit = "Soort: " + soort;
            return uit;
        }
    }


    public class IOBurst
    {
        private String soort;

        public IOBurst(String soort)
        {
            base.ToString();
            Id = soort;
        }

        public String Id
        {
            get
            {
                return soort;
            }
            set
            {
                soort = value;
            }
        }

        public override string ToString()
        {
            //base(lengte);
            string uit;
            uit = "Soort: " + soort;
            return uit;
        }

        public String getSoort()
        {
            return soort;
        }

        public void setSoort(String soort)
        {
            this.soort = soort;
        }
    }

IOBurst ib = new IOBurst(value);
BurstKlasse bk = new BurstKlasse( 2, ib );
textBox1.Text = bk.ToString();


Something like that ??

// Jerry

Member Avatar for Annieken

Thank you jerry for the reply

I don't really understand this code

IOBurst ib = new IOBurst(value);
BurstKlasse bk = new BurstKlasse( 2, ib );
textBox1.Text = bk.ToString();

Because I can't give any arguments in new BurstKlasse (2,ib) because the user has to choose these things with the radiobutton (for the bursts) and the length (the '2'), he has to enter.

Can I write this instead?

BurstKlasse bk = new BurstKlasse( textBox4.Text, Radiobutton_checked );

Or maybe I have to set those values in a variable?

I guess I need to understand the objective.
If you want to determine which radio button was checked, then you can send the checked radio button or just its Text in as a param.

You will have to obviously change the class to accept the string or RadioButton variable. I will assume you are going to send the string.

private List<BurstKlasse> Burst = new List<BurstKlasse>();
.
.
.
private void button1_click(object sender, EventArgs e)
{
int i = Convert.ToInt32( textBox1.Text);
string s = "";
if (rb_CPUburst.Checked)
   s = rb_CPUburst.Text;
else if(rb_IOburst.Checked)
  s = rb_IOburst.Text;

Burst.Add( new BurstKlasse(i,s) );
listBox1.DataSource = null;
listBox1.DataSource = Burst;
}

// Jerry
bk = new BurstKlasse( i,s);

Member Avatar for Annieken

Thanks. But is it now using the classes? Am I not only copying the text of the radiobutton? It's just a question, but the code works.

Now I've got almost the same thing but with the combobox. I've got 3 possibilities (New,Ready,Wait).
I want also display the selected item in a listbox.

This is my code (it gives a NullReferenceException was unhandled)

String Pstatus = " ";
                if (comboBox1.SelectedValue.Equals(0))
                    Pstatus = "New";
                if (comboBox1.SelectedValue.Equals(1))
                    Pstatus = "Ready";
                if (comboBox1.SelectedValue.Equals(2))
                    Pstatus = "Wait";

Any advice?

Member Avatar for Annieken

I've found it myself:

String Pstatus = null;
            
                if (comboBox1.SelectedIndex.Equals(0))
                     Pstatus = "New";
                if (comboBox1.SelectedIndex.Equals(1))
                     Pstatus = "Ready";
                 if (comboBox1.SelectedIndex.Equals(2))
                     Pstatus = "Wait";
Member Avatar for Annieken

Another question:

I've got two listboxs: listbox3 and listbox4
In listbox3 there has to be a member of listbox1. But only one member. This listbox3 has also be sorted by another member (also of listbox1)

In listbox4 I have to display the properties of the selected item in listbox3.

Example:

Listbox1:
PNaam: First - PNr 2 - PAankomst 4 - Pstatus New
PNaam: Second - PNr 9 - PAankomst 8 - Pstatus Wait
PNaam: Last - PNr 2 - PAankomst 5- Pstatus New

In ListBox3 there has to be (according by PAankomst)
First
Last
Second

In listbox4 there has to be (when selecting Second)

PNr 9 - PAankomst 8 - Pstatus Wait

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.