Home > database >  How to make a program that will open a certain picture in the picturebox, depending on the text ente
How to make a program that will open a certain picture in the picturebox, depending on the text ente

Time:12-06

I don't understand what to do

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.Windows.Forms;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1 = "France")
            {
                pictureBox1.Image = Image.FromFile(@"C:\user\proga ot alejandro\1.jpg");
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
           
        }


    }
}

I should make programm when i text a country name in textbox it will open a picture in picture box

I need help please. I would be kicked from the university if i wont make that programm.

CodePudding user response:

Try this code in your Text Changed event - then build on all your other countries that are needed.

if (textBox1.Text.ToLower() == "france")
            {
                pictureBox1.Image = Image.FromFile(@"C:\path to picture");
            }
            else if (textBox1.Text.ToLower() == "us")
            {

                pictureBox1.Image = Image.FromFile(@"C:\path to picture");

            }else if(continue on...){}

CodePudding user response:

Here are a few suggestions to put you on the right track (but please make sure that the final code you turn into "university" is something you can explain and justify LOL).

First, put the images into a folder and set their properties to Copy if Newer so that they can be read from a known path at runtime.

images folder

This makes it easy to capture a list of the image file names when you start your program.

public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();
    protected override void onl oad(EventArgs e)
    {
        base.OnLoad(e);
        var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
        // Make an array of the image file names so you can search it.
        _images = 
            Directory
            .GetFiles(folder)
            .ToArray();
        textBoxSearch.TextChanged  = ontextBoxSearchChanged;
    }
}
private string[] _images;

Then, when the text changes, you can use a System.Linq expression or some other method to search this list for matches (making sure not to consider the extension or the folder path). Just make certain that this search functionality "knows what to do" if it finds multiple matches.

private void ontextBoxSearchChanged(object sender, EventArgs e)
{
    // Do not block on this event.
    BeginInvoke((MethodInvoker)delegate 
    {
        string[] matches;
        if(string.IsNullOrWhiteSpace(textBoxSearch.Text))
        {
            matches = new string[0];
        }
        else
        {
            // Use Linq to detect matches
            matches = 
                _images
                .Where(_ => 
                    Path.GetFileNameWithoutExtension(_)
                    .Contains(textBoxSearch.Text)
                ).ToArray();
        }
        labelMatchCount.Text = $"{matches.Length} matches";
        if(matches.Length.Equals(1))
        {
            // Found a single match
            labelMatchCount.Visible = false;
            pictureBox.Image = Image.FromFile(matches.First());
        }
        else
        {
            // Found multiple matches
            pictureBox.Image = null;
            labelMatchCount.Visible = true;
        }
    });
}

runtime

  • Related