winforms - c# add clickable images to gui using code and not the designer -
i have folder full of images etc , want add them gui inside tab once program ran.
steps:
- read contents inside folder
- add images tabcontrol , assign number them according in order added (1) first picture added (2) second etc
- the images need added in rows of 3 differend x values every 3 pictures each , 1 y value change each row of 3
[] [] []
[] [] []
[] []
doesnt need divideable in 3 ^ fine
my issue didnt find way add images gui without using designer , im new c# huge challange me :(
in ahk gui,add,picture doesnt work way in c#
help or form of tips/guidance appreciated
the reason pictures need "have value" on them once clicked know image clicked because should have click event sent same function checks number , changes value of variables accordingly
thanks c# overwhelming
public static void processdirectory(string targetdirectory) { console.writeline("processed folder '{0}'.", targetdirectory); // process list of files found in directory. string[] fileentries = directory.getfiles(targetdirectory); foreach (string filename in fileentries) processfile(filename); // recurse subdirectories of directory. string[] subdirectoryentries = directory.getdirectories(targetdirectory); foreach (string subdirectory in subdirectoryentries) processdirectory(subdirectory); } // insert logic processing found files here. public static void processfile(string path) { console.writeline("processed file '{0}'.", path); if (!imageextensions.contains(path.getextension(path).toupperinvariant())) { console.writeline("not image"); return; } flowlayoutpanel flowlayoutpanel1 = new flowlayoutpanel(); image = new bitmap(path); picturebox p = new picturebox(); p.image = i; p.tag = "im1"; p.click += onimageclick; // let have same event of pictures flowlayoutpanel1.controls.add(p); } // handles clicks private static void onimageclick(object sender, eventargs e) { messagebox.show("hey"); // leave implement... 'sender' picturebox clicked. // can picturebox casting, (picturebox)sender // throw new notimplementedexception(); }
to add images, text, or else gui programmatically in winforms (which sounds you're using) you'll create instance of derived control
, add form's controls
controlcollection. can use flowlayoutpanel
give grid view.
// list of paths files directory array var filelist = directory.getfiles(@"c:\images","*.jpg"); // create flowlayout panel flowlayoutpanel f = new flowlayoutpanel(); foreach (string path in filelist) { image = new bitmap(path); picturebox p = new picturebox(); p.image = i; p.click += onimageclick; // let have same event of pictures f.controls.add(p); } // add panel form this.controls.add(p); // handles clicks private void onimageclick(object sender, eventargs e) { // leave implement... 'sender' picturebox clicked. // can picturebox casting, (picturebox)sender throw new notimplementedexception(); }
this implementation quick , dirty: is, doesn't handle if directory isn't found, if there aren't files in there, etc). should give idea of go. recommend reading a lot more, getting book c# , walking through examples/tutorials can immensely helpful. you're right, c# can overwhelming... that's why google best friend! don't need learn how everything; find learn choosing small-in-scope project want , learning find things need.
Comments
Post a Comment