Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo,

ich spiele zurzeit bisschen mit der GUI in C# rum und möchte in einer ListBox mehrere Elemente auswählen und in eine andere ListBox verschieben können. (SelectionMode ist MultiSimple, wenn das wichtig ist...)

Mein erster Ansatz:


private void btnToRight(object sender, EventArgs e)

        {

            foreach (int i in lbLeft.SelectedIndices)

            {


                lbRight.Items.Add(lbLeft.Items[i]);

                lbLeft.Items.Remove(lbLeft.Items[i]);


            }

        }

Auf diese Art kann ich zwar mehrere Elemente auswählen, es wird aber immer nur eins verschoben, die restlichen bleiben markiert in der Start-ListBox. Deswegen dachte ich, dass mit einer Liste lösen zu können:

private void btnToLeft(object sender, EventArgs e)

        {

            List<object> picked = new List<object>();


            foreach (int i in lbLeft.SelectedIndices)

            {

                picked.Add(lbLeft.Items[i]);

                lbLeft.Items.Remove(lbLeft.Items[i]);


            }

            lbRight.Items.AddRange(picked);




        }

Hier bekomme ich schon beim Kompilieren einen Fehler:

"Kann nicht von "Generic.List<object>" in "Forms.ListBox.ObjectCollection" konvertieren"

Wie kann ich also mehrere Elemente zwischen den ListBoxen hin und herschieben?

Danke!

  • Autor

Hallo Gybrush,

danke, mit dem Hinweis hab ichs hinbekommen ^^.


private void btnToLeft(object sender, EventArgs e)

        {


            foreach(object o in lbLeft.SelectedItems)

            {

                int index = lbLeft.Items.IndexOf(o);

                lbRight.Items.Add(lbLeft.Items[index]);

            }


            foreach (object o in lbRight.Items)

            {

                if(lbLeft.Items.Contains(o)){

                lbLeft.Items.Remove(o);

                }

            }



  • Autor

Hi,

ich hab noch n anderes kleines Problem, möchte dafür aber keinen neuen Thread erstellen.

Ich habe eine Klasse Form, in dieser rufe ich über ein Menü das Fenster "NewPersonDialog" auf. In dem Fenster gibt es verschiedene Textboxen, in denen ich z.B. Name, Alter, etc. eingeben möchte. Sobald das Fenster mit OK geschlossen wird, soll die neu erstellte Person gespeichert und in die Liste "Family" eingefügt werden.

Problem: Es wird zwar eine neue Person angelegt, aber die Textbox-Werte werden nicht ausgelesen. In meiner Kontrollbox (tbCheck) stehen nur leere Einträge.

Ich bekomme keine Fehlermeldung.

MainForm:


namespace Stammbaum

{

    public partial class MainForm : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void neuePersonAnlegenToolStripMenuItem_Click(object sender, EventArgs e)

        {

            NewPersonDialog form = new NewPersonDialog();



            if (form.ShowDialog() == DialogResult.OK)

            {

                Person created = new Person();

                Person.AddPersonToFamily(created);


                this.tbCheck.Text = "Surname:" + created.Surname + "\r\n";

                this.tbCheck.Text += "Name: " + created.Name;

                this.tbCheck.Text += Person.FamilyToString();

            }



        }


    }

}



Klasse Person:

namespace Stammbaum

{

    class Person

    {

        private string name;

        private string surname;

        private int dateOfBirth;

        private int dateOfDeath;

        private string gender;

        private Person mother;

        private Person father;

        private List<Person> siblings = new List<Person>();

        private List<Person> children = new List<Person>();

        private static List<Person> family = new List<Person>();

        NewPersonDialog dialog = new NewPersonDialog();


        public Person()

        {

            this.name = dialog.tbName.Text;

            this.surname = dialog.tbSurname.Text;


        }



        public string Name

        {

            get { return this.name; }

            set { this.name = value; }

        }

        public string Surname

        {

            get { return this.surname; }

            set { this.surname = value; }

        }

        public int Born

        {

            get { return this.dateOfBirth; }

            set { this.dateOfBirth = value; }

        }

        public int Died

        {

            get { return this.dateOfDeath; }

            set { this.dateOfDeath = value; }

        }


        public string Gender

        {

            get { return this.gender; }

            set { this.gender = value; }

        }



        public static void AddPersonToFamily(Person created)

        {

            family.Add(created);

        }


        public static List<Person> GetFamily()

        {

            return family;

        }


        public static string FamilyToString()

        {

            string output = "";

            foreach (Person p in family)

            {

                output += p.Name + "\r\n";

                output += p.Surname + "\r\n";

                output += p.Born + "\r\n";

                output += p.Died + "\r\n";

                output += p.gender + "\r\n";



            }

            return output;

        }


    }

}

Die Klasse NewPersonDialog enthält nur die Methode InitializeComponent() und die notwendigen GUI-Elemente auf dem Formular, ist ansonsten aber leer.

Jede Person-Instanz befüllt sich aus einer eigenen neuen Instanz von NewPersonDialog (dialog), die, da sie erst mit der Person erstellt und nie angezeigt wurde, natürlich keine Daten enthält.

Die Werte des Dialogs, den du tatsächlich anzeigst (form), verwendest du nicht.

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.