c# - display an image in a form when the image name is selected -


new wpf. want display image in form when image name selected combo box (image stored in sql database fills combo).

would know of example how can done. have added code populates text boxes when selecting combo.

   private void comboboxdisplay_selectionchanged(object sender, selectionchangedeventargs e)      {         string constring = "data source=tcp:****;initial   catalog=******;persist security info=true;user id=*******;password=******";          string query = "select * tables name='" +  comboboxdisplay.selecteditem.tostring() + "' ;";         sqlconnection condatabase = new sqlconnection(constring);         sqlcommand cmddatabase = new sqlcommand(query, condatabase);         sqldatareader myreader;          try         {             condatabase.open();             myreader = cmddatabase.executereader();              while (myreader.read())             {                  string sreid = myreader.getint32(0).tostring();                 string sname = myreader.getstring(1);                 string spicture = myreader.getstring(3);                  txtreid.text = sreid;                 txtname.text = sname;                 txtpicture.text = spicture;              }          }         catch (exception ex)         {             messagebox.show(ex.message);         } 

normally, users of site prefer question authors provide bit more information have. see have @ least tried find answer yourself.

however, new user, provide complete solution. there several ways of achieving this... show 1 way , can adapt needs.

start new wpf project , add following mainwindow.xaml.cs file:

using system; using system.collections.objectmodel; using system.windows;  namespace wpfapplication1 {     public partial class mainwindow : window     {         public mainwindow()         {             initializecomponent();             datacontext = this;             images.add(new tuple<string, string>("picture 1",  "/wpfapplication1;component/images/picture 1.png"));             images.add(new tuple<string, string>("picture 2",  "/wpfapplication1;component/images/picture 2.png"));             images.add(new tuple<string, string>("picture 3",  "/wpfapplication1;component/images/picture 3.png"));         }          public static dependencyproperty imagesproperty = dependencyproperty.register( "images", typeof(observablecollection<tuple<string, string>>), typeof(mainwindow),  new propertymetadata(new observablecollection<tuple<string, string>>()));          public observablecollection<tuple<string, string>> images         {             { return (observablecollection<tuple<string, string>>)getvalue( imagesproperty); }             set { setvalue(imagesproperty, value); }         }     } } 

here have created dependencyproperty named images hold image information. of type tuple<string, string> , enables me add name and file path of each picture. need adapt system of having images in database. suggest saving images in folder , referencing file paths easier trying load image objects image control.

next, put following code mainwindow.xaml file:

<window x:class="wpfapplication1.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <grid>         <grid.rowdefinitions>             <rowdefinition />             <rowdefinition height="100" />         </grid.rowdefinitions>         <image grid.row="0" source="{binding selectedvalue,  elementname=imagecombobox}" />         <combobox grid.row="1" name="imagecombobox" itemssource="{binding images}"  displaymemberpath="item1" selectedvaluepath="item2" height="23" width="120" />     </grid> </window> 

here bind images collection combobox.itemssource property , set displaymemberpath property item1... name of first tuple property holds picture name - therefore, names displayed. set selectedvaluepath property item2... name of second tuple property holds picture file path - therefore value of selected item file path of selected image.

also note image.source property set binding selectedvalue, elementname=imagecombobox. means source of pictures come combobox.selectedvalue, if remember, set file path of selected image.

things note:

you can of course replace tuple objects own classes long update names of correct properties use combobox.selectedvaluepathand combobox.displaymemberpath properties.

please add images use example code folder named images in application root directory. also, unless name them picture 1.png, picture 2.png , picture 3.png, need update file paths add images collection.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -