avocadoman

How Do I Display Names from a ListBox onto a TextBox?

Apr 10th, 2021
845
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.IO;
  16.  
  17. namespace DogAdoption_Form
  18. {
  19.     /// <summary>
  20.     /// Interaction logic for MainWindow.xaml
  21.     /// </summary>
  22.     public partial class MainWindow : Window
  23.     {
  24.         const string DISPLAY_NAME = "Name will display here...";
  25.  
  26.         public MainWindow()
  27.         {
  28.             InitializeComponent();
  29.  
  30.             //DisplayLstBoxDogNames();    
  31.         }
  32.  
  33.         public void DisplayLstBoxDogNames()
  34.         {
  35.             lstBoxDogNames.Items.Add("Chloe");
  36.             lstBoxDogNames.Items.Add("Daisy");
  37.             lstBoxDogNames.Items.Add("Emmett");
  38.             lstBoxDogNames.Items.Add("Jack");
  39.             lstBoxDogNames.Items.Add("Lucy");
  40.             lstBoxDogNames.Items.Add("Nala");
  41.             lstBoxDogNames.Items.Add("Peach");
  42.             lstBoxDogNames.Items.Add("Rocky");
  43.             lstBoxDogNames.Items.Add("Shiloh");
  44.             lstBoxDogNames.Items.Add("Toby");
  45.  
  46.             foreach (string dogName in lstBoxDogNames.Items)
  47.             {
  48.                 if (txtBoxName.Text == DISPLAY_NAME)
  49.                 {
  50.                     //txtBoxName.Text.Replace(DISPLAY_NAME, dogName);
  51.                     txtBoxName.Text = dogName;
  52.                     txtBoxName.Foreground = Brushes.Black;
  53.                 }
  54.  
  55.                 else if ((txtBoxName.Text != DISPLAY_NAME) || (txtBoxName.Text == ""))
  56.                 {
  57.                     //txtBoxName.Text.Replace(txtBoxName.Text, dogName);
  58.                     txtBoxName.Text = dogName;
  59.                     txtBoxName.Foreground = Brushes.Black;
  60.                 }
  61.             }
  62.        
  63.         }
  64.  
  65.         private void txtBoxName_GotFocus(object sender, RoutedEventArgs e)
  66.         {
  67.             DisplayLstBoxDogNames();
  68.         }
  69.  
  70.         private void txtBoxName_LostFocus(object sender, RoutedEventArgs e)
  71.         {
  72.             if (txtBoxName.Text == DISPLAY_NAME)
  73.             {
  74.                 txtBoxName.Foreground = Brushes.Gray;
  75.             }
  76.  
  77.             else if (txtBoxName.Text != DISPLAY_NAME)
  78.             {
  79.                 txtBoxName.Foreground = Brushes.Gray;
  80.             }
  81.         }
  82.     }
  83. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×