locked
how can i display what the user clicks on in the listbox and have the detailed information show up in the textbox RRS feed

  • Question

  • THIS IS JUST A LITTLE DESCRIPTION ON WHAT IM TRYING TO DO WITH THIS PROGRAM

    THANK YOU FOR YOUR TIME. =)

    Instructions/Description The primary (main) form of the application will contain a list box, a picture box, a multi-lined textbox, and three buttons, as follows:  
    Given a list of words, the user may click on any of these words, and upon selection, the application will automatically:  Display an image related to the word  Display the definition of the word in the multi-line text box
    Additionally, there are three buttons:  Add Word  Delete Word  Edit Word
    Add Word When the user clicks on Add Word, the application will display a separate modal form that will allow the user to enter the information related to the new word he/she wishes to add.   
    The user will enter the word, an image name, and a definition.  If they are satisfied with what they’ve entered and want to commit the information to the database, they hit the Add Word button near the bottom of this form.
    The images can be copied into an Images directory (folder) under the bin/Debug/ directory (the directory the executable lives in if you’re compiling/building in Debug mode.)  The images are copied into this directory manually (the program isn’t responsible for putting the images into this directory.
    However, the name you put under Image Name must match exactly the name of the image in the Images directory.  For example, if the word the user is entering is sausage, then the image may be “sausage.png”.  Therefore, you must ensure there is an image loaded into the Images directory named sausage.png.
    The application should prevent the user from adding the word if they leave any of the three textboxes empty.  In other words, communication with the database will only occur if all three of the textboxes have non-empty content in them.
    If the user decides they don’t want to add the word, they can just hit the X in the corner and close this form, without adding the item to the database.  
    Delete Word The Delete Word option simply pops up a Message Box asking if the user is sure they want to delete the selected word.  If the user clicks OK or Yes, then the word will be deleted from the database (and the dataset and list box.)  If he/she clicks Cancel or No, the word will remain in the database (and the dataset, and the list box.)
    Edit Word When the user presses the Edit Word button (with a particular selection of word made), the Edit Word form will be displayed and look almost identical to the Add Word option shown above.  The difference will be that the word will not be editable (the textbox’s Enabled property will be set to false) – and the image location and definition will load with whatever the database contains as their current data. 
    Upon changing the data, the user may press Edit Word to commit the changes to the database.
    Deliverables

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                //wordBox.SelectedIndex = 0;
                //Dog.SelectedIndex;
                pictureBox1.Image = Properties.Resources.white;
            }
                  /IM HAVING ALOT OF PROBLEMS JUST TRYING TO DISPLAY INFORMATION THAT I WANT TO BE DISPLAYED IN THE TEXTBOX(displayBox) listBox(wordBox)
            private void wordBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                int index = wordBox.SelectedIndex;
                wordBox.Text = displayBox.Text;
    
                int count = wordBox.SelectedItems.Count;
    
                if(index == 0)
                {
                   pictureBox1.Image = Properties.Resources.white;
    
                    displayBox.SelectedItem(0);
                }
    
                else if (index == 1)
                {
                    pictureBox1.Image = Properties.Resources.atom;
                    
                }
    
                else if (index == 2)
                {
                    pictureBox1.Image = Properties.Resources.fire;
                }
    
                else if (index == 3)
                {
                    pictureBox1.Image = Properties.Resources.sandal;
                }
            }
        }
    }
    

    Sunday, April 10, 2016 10:06 PM

Answers

  • Hi DR.Doctor,
    >>how would you go about adding another window/designer  that pops up when you you click on the Button addword and still have it linked to a dataBase?
    I use a txt file as the database and haven't use another form(I use the OpenFileDialog). You could add a form to operate. By the way, I have modified my code, perhaps you could refer to it:
      string Filepath = Application.StartupPath + "\\list.txt";
            string ImageFolder = Application.StartupPath + "\\ImageFolder";
            //use to edit
            object currectItem = null;
    
            #region CustomMethod
            private void LoadImage(string imageName)
            {
                imageName += ".jpg";
                if (File.Exists(ImageFolder + "\\" + imageName))
                {
                    using (FileStream fs = new FileStream(ImageFolder + "\\" + imageName, FileMode.Open))
                        pbDict.BackgroundImage = Image.FromStream(fs);
                }
            }
            private void SaveImage(string path, string name)
            {
                File.Copy(path, ImageFolder + "\\" + name + ".jpg", true);
            }
            private void DeleteImage(string imageName)
            {
                imageName += ".jpg";
                if (File.Exists(imageName))
                {
                    File.Delete(ImageFolder + "\\" + imageName);
                }
            }
            private void MoveOrRename(string old, string target)
            {
                File.Move(old, target);
            }
            private void SaveFile()
            {
                string contect = string.Empty;
                foreach (object obj in lbDict.Items)
                    contect += obj.ToString() + ";";
                contect = contect.Length > 0 ? contect.Substring(0, contect.Length - 1) : "";
                if (File.Exists(Filepath))
                {
                    using (FileStream fs = new FileStream(Filepath, FileMode.Create))
                    {
                        byte[] buff = System.Text.Encoding.Unicode.GetBytes(contect);
                        fs.Write(buff, 0, buff.Length);
                    }
                }
            }
            private void LoadFile()
            {
                string temp = string.Empty;
                if (File.Exists(Filepath))
                {
                    using (FileStream fs = new FileStream(Filepath, FileMode.OpenOrCreate))
                    {
                        byte[] buff = new byte[fs.Length];
                        fs.Read(buff, 0, (int)fs.Length);
                        temp = System.Text.Encoding.Unicode.GetString(buff);
                        foreach (string str in temp.Split(';'))
                        {
                            if (!lbDict.Items.Contains(str) && str.Trim() != "")
                                lbDict.Items.Add(str);
                        }
                    }
                }
            }
            private void Add()
            {
                if (!lbDict.Items.Contains(txtName.Text.Trim()))
                {
                    lbDict.Items.Add(txtName.Text.Trim());
                    OpenFileDialog open = new OpenFileDialog();
                    open.Filter = "JPG|*.jpg";
                    if (open.ShowDialog() == DialogResult.OK)
                    {
                        SaveImage(open.FileName, txtName.Text.Trim());
                    }
                }
            }
            private void Edit()
            {
                if (!lbDict.Items.Contains(txtName.Text.Trim()))
                {
                    if (lbDict.SelectedItem == null)
                    {
                        if (currectItem != null)
                            lbDict.SelectedItem = currectItem;
                        else
                            return;
                    }
    
                    if (File.Exists(ImageFolder + "\\" + lbDict.SelectedItem.ToString() + ".jpg"))
                    {
                        MoveOrRename(ImageFolder + "\\" + lbDict.SelectedItem.ToString() + ".jpg", ImageFolder + "\\" + txtName.Text + ".jpg");
                    }
                    lbDict.Items.Remove(currectItem);
                    lbDict.Items.Add(txtName.Text);
                }
            }
            private void Remove()
            {
                if (MessageBox.Show("Delete?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    File.Delete(ImageFolder + "\\" + lbDict.SelectedItem.ToString());
                    lbDict.Items.Remove(lbDict.SelectedItem);
                }
            }
            #endregion
    
            #region Event
            private void lbDict_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (lbDict.Items.Count > 0 && lbDict.SelectedIndex != -1)
                {
                    txtName.Text = lbDict.SelectedItem.ToString();
                    LoadImage(txtName.Text);
                    currectItem = txtName.Text.Trim();
                }
            }
            //add
            private void btnAdd_Click(object sender, EventArgs e)
            {
                Add();
            }
    
            //remove
            private void btnRemove_Click(object sender, EventArgs e)
            {
                Remove();
            }
    
            //edit
            private void btnEdit_Click(object sender, EventArgs e)
            {
                Edit();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                LoadFile();
                pbDict.BackgroundImageLayout = ImageLayout.Stretch;
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                SaveFile();
            }
            #endregion

    Regards,
    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, April 14, 2016 7:18 AM

All replies

  • Hello,

     You have another posting about the same issue, except it has less details.

    Please do not start a new posting about the same issue. Ask the Mods to

    merge or delete the unused/old posting.

     As for your project, seems like a well thought out plan. I like that! :) Now, its time

    for the nitty-gritty. Show us what code you have for Events, DB bindings, etc. I

    would like to see a pic the controls layout. So, I can duplicate and test control

    interactions.  Also, include details about the target dotNet platform. Rarely, but

    does happen, that not using the correct dotNet leads to unusable suggestions.

     Thanks :)

    Monday, April 11, 2016 1:29 AM
  • Hi DR.Doctor,

    You could try the following code:

      string Filepath = Application.StartupPath + "\\list.txt";
            string ImageFolder = Application.StartupPath + "\\ImageFolder";
    
            private void LoadImage(string imageName)
            {
                imageName += ".jpg";
                if (File.Exists(ImageFolder + "\\" + imageName))
                {
                    pbDict.BackgroundImage = Image.FromStream(File.OpenRead(ImageFolder + "\\" + imageName));
                }
            }
            private void SaveImage(string path, string name)
            {
                File.Copy(path, ImageFolder + "\\" + name + ".jpg", true);
            }
            private void DeleteImage(string imageName)
            {
                imageName += ".jpg";
                if (File.Exists(imageName))
                {
                    File.Delete(ImageFolder + "\\" + imageName);
                }
            }
            private void SaveFile()
            {
                string contect = string.Empty;
                foreach (object obj in lbDict.Items)
                    contect += obj.ToString() + ";";
                contect = contect.Length > 0 ? contect.Substring(0, contect.Length - 1) : "";
                if (File.Exists(Filepath))
                {
                    File.WriteAllText(Filepath, contect, Encoding.Unicode);
                }
            }
            private void LoadFile()
            {
                string temp = string.Empty;
                if (File.Exists(Filepath))
                    temp = File.ReadAllText(Filepath);
                foreach (string str in temp.Split(';'))
                {
                    if (!lbDict.Items.Contains(str))
                        lbDict.Items.Add(str);
                }
            }
            private void lbDict_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (lbDict.Items.Count > 0 && lbDict.SelectedIndex != -1)
                {
                    txtName.Text = lbDict.SelectedItem.ToString();
                    LoadImage(txtName.Text);
                }
            }
            private void btnAdd_Click(object sender, EventArgs e)
            {
                if (!lbDict.Items.Contains(txtName.Text.Trim()))
                {
                    lbDict.Items.Add(txtName.Text.Trim());
                    OpenFileDialog open = new OpenFileDialog();
                    open.Filter = "JPG|*.jpg";
                    if (open.ShowDialog() == DialogResult.OK)
                    {
                        SaveImage(open.FileName, txtName.Text.Trim());
                    }
                }
    
            }
            private void btnRemove_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("Delete?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    lbDict.Items.Remove(lbDict.SelectedItem);
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                LoadFile();
                pbDict.BackgroundImageLayout = ImageLayout.Stretch;
            }

    Notice: lbDict is listbox, pbDict is picturebox.

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, April 12, 2016 10:06 AM
  • I like your Code. But I'm sorry I cant upload a picture of the designer. because of my account not being verified yet? I have it set up from left I have a listBox then a picture box then a multi line text box and beneath the listbox I have my buttons beneath the listBox

    how would you go about adding another window/designer  that pops up when you you click on the Button addword and still have it linked to a dataBase? 

    I look forward to your reply back =)


    • Edited by DR.Doctor Tuesday, April 12, 2016 11:55 AM
    Tuesday, April 12, 2016 11:46 AM
  • Hi DR.Doctor,
    >>how would you go about adding another window/designer  that pops up when you you click on the Button addword and still have it linked to a dataBase?
    I use a txt file as the database and haven't use another form(I use the OpenFileDialog). You could add a form to operate. By the way, I have modified my code, perhaps you could refer to it:
      string Filepath = Application.StartupPath + "\\list.txt";
            string ImageFolder = Application.StartupPath + "\\ImageFolder";
            //use to edit
            object currectItem = null;
    
            #region CustomMethod
            private void LoadImage(string imageName)
            {
                imageName += ".jpg";
                if (File.Exists(ImageFolder + "\\" + imageName))
                {
                    using (FileStream fs = new FileStream(ImageFolder + "\\" + imageName, FileMode.Open))
                        pbDict.BackgroundImage = Image.FromStream(fs);
                }
            }
            private void SaveImage(string path, string name)
            {
                File.Copy(path, ImageFolder + "\\" + name + ".jpg", true);
            }
            private void DeleteImage(string imageName)
            {
                imageName += ".jpg";
                if (File.Exists(imageName))
                {
                    File.Delete(ImageFolder + "\\" + imageName);
                }
            }
            private void MoveOrRename(string old, string target)
            {
                File.Move(old, target);
            }
            private void SaveFile()
            {
                string contect = string.Empty;
                foreach (object obj in lbDict.Items)
                    contect += obj.ToString() + ";";
                contect = contect.Length > 0 ? contect.Substring(0, contect.Length - 1) : "";
                if (File.Exists(Filepath))
                {
                    using (FileStream fs = new FileStream(Filepath, FileMode.Create))
                    {
                        byte[] buff = System.Text.Encoding.Unicode.GetBytes(contect);
                        fs.Write(buff, 0, buff.Length);
                    }
                }
            }
            private void LoadFile()
            {
                string temp = string.Empty;
                if (File.Exists(Filepath))
                {
                    using (FileStream fs = new FileStream(Filepath, FileMode.OpenOrCreate))
                    {
                        byte[] buff = new byte[fs.Length];
                        fs.Read(buff, 0, (int)fs.Length);
                        temp = System.Text.Encoding.Unicode.GetString(buff);
                        foreach (string str in temp.Split(';'))
                        {
                            if (!lbDict.Items.Contains(str) && str.Trim() != "")
                                lbDict.Items.Add(str);
                        }
                    }
                }
            }
            private void Add()
            {
                if (!lbDict.Items.Contains(txtName.Text.Trim()))
                {
                    lbDict.Items.Add(txtName.Text.Trim());
                    OpenFileDialog open = new OpenFileDialog();
                    open.Filter = "JPG|*.jpg";
                    if (open.ShowDialog() == DialogResult.OK)
                    {
                        SaveImage(open.FileName, txtName.Text.Trim());
                    }
                }
            }
            private void Edit()
            {
                if (!lbDict.Items.Contains(txtName.Text.Trim()))
                {
                    if (lbDict.SelectedItem == null)
                    {
                        if (currectItem != null)
                            lbDict.SelectedItem = currectItem;
                        else
                            return;
                    }
    
                    if (File.Exists(ImageFolder + "\\" + lbDict.SelectedItem.ToString() + ".jpg"))
                    {
                        MoveOrRename(ImageFolder + "\\" + lbDict.SelectedItem.ToString() + ".jpg", ImageFolder + "\\" + txtName.Text + ".jpg");
                    }
                    lbDict.Items.Remove(currectItem);
                    lbDict.Items.Add(txtName.Text);
                }
            }
            private void Remove()
            {
                if (MessageBox.Show("Delete?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    File.Delete(ImageFolder + "\\" + lbDict.SelectedItem.ToString());
                    lbDict.Items.Remove(lbDict.SelectedItem);
                }
            }
            #endregion
    
            #region Event
            private void lbDict_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (lbDict.Items.Count > 0 && lbDict.SelectedIndex != -1)
                {
                    txtName.Text = lbDict.SelectedItem.ToString();
                    LoadImage(txtName.Text);
                    currectItem = txtName.Text.Trim();
                }
            }
            //add
            private void btnAdd_Click(object sender, EventArgs e)
            {
                Add();
            }
    
            //remove
            private void btnRemove_Click(object sender, EventArgs e)
            {
                Remove();
            }
    
            //edit
            private void btnEdit_Click(object sender, EventArgs e)
            {
                Edit();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                LoadFile();
                pbDict.BackgroundImageLayout = ImageLayout.Stretch;
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                SaveFile();
            }
            #endregion

    Regards,
    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, April 14, 2016 7:18 AM