Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to delete the object in the same way that in the given code the color of the object changes after selecting the index of the listbox and then clicking on the button.
What I want to do is select the index of the listbox and delete the object instead of changing the colour of the object by clicking on the button.

What I have tried:

this my code for load object (this my cod in main window):
C#
private List<string> _loadedObjectPaths = new List<string>();

private void loadbtn_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".stl";
            dlg.Filter = "STL files (*.stl)|*.stl";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                // Load the STL file using the Helix Toolkit's STL reader
                var reader = new StLReader();
                _modelGroup = reader.Read(dlg.FileName);
                _modelGroup.Transform = _translate;

                // Set the Tag property of each GeometryModel3D to the file name
                foreach (GeometryModel3D model in _modelGroup.Children)
                {
                    model.SetValue(TagProperty, dlg.FileName);
                }

                // Create a ModelVisual3D object and add the _modelGroup to it
                _modelVisual = new ModelVisual3D();
                _modelVisual.Content = _modelGroup;

                // Add the model to the viewport's 3D model collection
                viewPort3d.Children.Add(_modelVisual);
                STL.removemodel = _modelVisual;
                settingpanel.Children.Clear();

                // Add the object name to the existing list box
               objectListBox.Items.Add(System.IO.Path.GetFileName(dlg.FileName));
                STL.filename = dlg.FileName;
            }
            else
            {
                System.Windows.MessageBox.Show("Could not load object.");
            }

            // Add the UserControl1 instance to the settingpanel if it hasn't been added already
            if (!settingpanel.Children.Contains(myUserControl))
            {
                settingpanel.Children.Add(myUserControl);
            }
        }


this my code for select index(this my code in usercontrol):
C#
private void objectListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int selectedIndex = objectListBox.SelectedIndex;
            if (selectedIndex >= 0 && selectedIndex < objectListBox.Items.Count)
            {
                // Get the name of the selected object
                string selectedObjectName = objectListBox.Items[selectedIndex].ToString();
                GeometryModel3D selectedModel;
                if (STL.ModelDictionary.TryGetValue(selectedObjectName, out selectedModel))
                {
                    // Restore the color of the previously selected model
                    if (previouslySelectedIndex >= 0 && previouslySelectedIndex != selectedIndex)
                    {
                        if (previouslySelectedMaterial != null)
                        {
                            GeometryModel3D previouslySelectedModel;
                            if (STL.ModelDictionary.TryGetValue(objectListBox.Items[previouslySelectedIndex].ToString(), out previouslySelectedModel))
                            {
                                previouslySelectedModel.Material = previouslySelectedMaterial;
                            }
                        }
                    }

                    // Set the material of the stored object to dark gray
                    selectedModel.Material = new DiffuseMaterial(new SolidColorBrush(System.Windows.Media.Color.FromRgb(139, 139, 139)));
                    STL.ListboxSelectedObject = selectedModel.Material;

                    // Update the previously selected index and material
                    previouslySelectedIndex = selectedIndex;
                    previouslySelectedMaterial = selectedModel.BackMaterial;
                }
            }
        }


this my code for color button (this my code in usercontrol):

C#
private void part1color_Click(object sender, RoutedEventArgs e)
        {
            // Open a color dialog to select a color
            System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
            dlg.ShowDialog();

            // Check if an object is selected
            if (objectListBox.SelectedItem != null)
            {
                System.Windows.Media.Color selectedColor = System.Windows.Media.Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B);
                var brush = new SolidColorBrush(selectedColor);
                (sender as System.Windows.Controls.Button).Background = brush;
                Material material = MaterialHelper.CreateMaterial(brush);

                // Restore the color of the previously selected object
                if (previouslySelectedIndex >= 0 && previouslySelectedIndex != objectListBox.SelectedIndex)
                {
                    if (previouslySelectedMaterial != null)
                    {
                        GeometryModel3D previouslySelectedModel;
                        if (STL.ModelDictionary.TryGetValue(objectListBox.Items[previouslySelectedIndex].ToString(), out previouslySelectedModel))
                        {
                            previouslySelectedModel.Material = previouslySelectedMaterial;
                            previouslySelectedModel.BackMaterial = previouslySelectedMaterial;
                        }
                    }
                }

                // Get the selected object
                string selectedObjectName = objectListBox.SelectedItem.ToString();
                GeometryModel3D selectedModel;
                if (STL.ModelDictionary.TryGetValue(selectedObjectName, out selectedModel))
                {
                    if (selectedModel is GeometryModel3D geometryModel)
                    {
                        geometryModel.Material = material;
                        geometryModel.BackMaterial = material;
                    }

                    STL.UpdateMaterial = material; // update original material
                }

                // Update the previously selected index and material
                previouslySelectedIndex = objectListBox.SelectedIndex;
                previouslySelectedMaterial = material;
            }
        }
Posted
Updated 14-Mar-23 5:19am
v2
Comments
[no name] 14-Mar-23 20:12pm    
If you added an object to a list with ".Add(xx)", then that same object is typically removed with ".Remove(xx)".
Member 15746979 15-Mar-23 5:23am    
By selecting the index and then clicking on the button to remove
[no name] 15-Mar-23 12:45pm    
The index points to the object. The object confirms it's the right object. My point was, who but you know what your "Adds" are doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900