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):
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)
{
var reader = new StLReader();
_modelGroup = reader.Read(dlg.FileName);
_modelGroup.Transform = _translate;
foreach (GeometryModel3D model in _modelGroup.Children)
{
model.SetValue(TagProperty, dlg.FileName);
}
_modelVisual = new ModelVisual3D();
_modelVisual.Content = _modelGroup;
viewPort3d.Children.Add(_modelVisual);
STL.removemodel = _modelVisual;
settingpanel.Children.Clear();
objectListBox.Items.Add(System.IO.Path.GetFileName(dlg.FileName));
STL.filename = dlg.FileName;
}
else
{
System.Windows.MessageBox.Show("Could not load object.");
}
if (!settingpanel.Children.Contains(myUserControl))
{
settingpanel.Children.Add(myUserControl);
}
}
this my code for select index(this my code in usercontrol):
private void objectListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = objectListBox.SelectedIndex;
if (selectedIndex >= 0 && selectedIndex < objectListBox.Items.Count)
{
string selectedObjectName = objectListBox.Items[selectedIndex].ToString();
GeometryModel3D selectedModel;
if (STL.ModelDictionary.TryGetValue(selectedObjectName, out selectedModel))
{
if (previouslySelectedIndex >= 0 && previouslySelectedIndex != selectedIndex)
{
if (previouslySelectedMaterial != null)
{
GeometryModel3D previouslySelectedModel;
if (STL.ModelDictionary.TryGetValue(objectListBox.Items[previouslySelectedIndex].ToString(), out previouslySelectedModel))
{
previouslySelectedModel.Material = previouslySelectedMaterial;
}
}
}
selectedModel.Material = new DiffuseMaterial(new SolidColorBrush(System.Windows.Media.Color.FromRgb(139, 139, 139)));
STL.ListboxSelectedObject = selectedModel.Material;
previouslySelectedIndex = selectedIndex;
previouslySelectedMaterial = selectedModel.BackMaterial;
}
}
}
this my code for color button (this my code in usercontrol):
private void part1color_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
dlg.ShowDialog();
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);
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;
}
}
}
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;
}
previouslySelectedIndex = objectListBox.SelectedIndex;
previouslySelectedMaterial = material;
}
}