How to display content from selected WPF ComboBox item C#
i hope everyone is well I am building a small application in C# and WPF, I am trying to add a functionality where when the combo Box is clicked and a certain element is selected, I want to display the contents of the text, but I cant seem to get it right. I would love some input on how to solve this issue, please take a look at the C# code and if you have a solution to it please post it, thank you, NOTE as you might see from the code, I am a beginner coder. Thanks in advace for the help.
namespace WPFDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
textToDisplay.IsReadOnly = true;
SaveFileDialog saveFileDialog = new SaveFileDialog();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Import new template";
openFileDialog.Filter = "Text files (*.txt)|*.txt";
saveFileDialog.Filter = "Text files (*.txt)|*.txt";
loadTemplates();
}
// Load the templates in the directory
private void loadTemplates()
{
string[] templates = Directory.GetFiles(@"C:\Users\injanlee\source\repos\WPFDemo\WPFDemo\Snow Templates");
Array.Sort(Directory.GetFiles(@"C:\Users\injanlee\source\repos\WPFDemo\WPFDemo\Snow Templates"));
Array.Sort(templates);
foreach (string template in templates)
{
//string fileName = template.Split("\\")[5].Split(".")[0];
string fName = System.IO.Path.GetFileName(template);
selectOption.Items.Add(fName);
}
}
private async Task Sleep(int miliSeconds)
{
await Task.Delay(miliSeconds);
myLabel.Content = " ";
}
private async void Submit_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText(textToDisplay.Text);
myLabel.Content = "Copied to clipboard";
myLabel.Foreground = new SolidColorBrush(Colors.DarkGreen);
await Sleep(3000);
}
private void DisplayFileContent(string path)
{
textToDisplay.Clear();
try
{
string[] fileContent = File.ReadAllLines(path);
foreach (string line in fileContent)
{
textToDisplay.Text += line;
textToDisplay.Text += "\n";
}
}
catch
{
MessageBox.Show($"Unable to find the path for the specific selected File: {selectOption.Text}");
}
}
// Import and save new file template
public void importTemplate_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
OpenFileDialog openFileDialog = new OpenFileDialog();
saveFileDialog.Title = "Save To";
openFileDialog.Title = "Import new template";
openFileDialog.Filter = "Text files (*.txt)|*.txt";
saveFileDialog.Filter = "Text files (*.txt)|*.txt";
if (openFileDialog.ShowDialog() == true)
{
string[] fileName = openFileDialog.FileName.Split("\\");
string importedFileName = fileName[5].Split(".")[0];
saveFileDialog.FileName = importedFileName;
if (saveFileDialog.ShowDialog() == true)
{
string location = saveFileDialog.FileName;
File.Copy(openFileDialog.FileName, location, true);
}
selectOption.Items.Add(fileName[5].Split(".")[0]);
MessageBox.Show("Impor successfull");
}
}
private void selectOption_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (selectOption.SelectedIndex == 0)
{
DisplayFileContent(@"C:\Users\injanlee\source\repos\WPFDemo\WPFDemo\Snow Templates\Template.txt");
}
}
}
}
from Recent Questions - Stack Overflow https://ift.tt/3q9jtQ7
https://ift.tt/2VctYUH
Comments
Post a Comment