c# - Programmatically adding two buttons to each row as it loads -


i've created listview in new wpf window , function populates listview when called. function takes url of web server i've stored data, increments "id" , gets data , stores in listview. therefore populates listview number of items.

the problem i'm facing want add 2 buttons, on & off, each listview item gets populated programmatically. i.e, if 16 items added, want 2 buttons each item, , if it's 12 items, similar procedure. here's code:

namespace user_login {     /// <summary>     /// interaction logic mainwindow.xaml     /// </summary>      public partial class mainwindow : window     {         window1 w = new window1();          public mainwindow()         {             initializecomponent();             }          public void populate()         {             int i;             int num = 16;              (i = 1; <= num; i++)             {                 string val = convert.tostring(i);                 string currenturl = "http://xpleria.com/devices.php?query=dev&id=";                 string newurlwithchangedsort = replacequerystringparam(currenturl, "id", val);                 string result = getcontent(newurlwithchangedsort);                     w.list1.items.add(result);             }         }          public string getcontent(string url)         {             string content = "";              // html data             webclient client = new webclient();              try             {                 content = client.downloadstring(url);             }             catch (exception)             {                 system.windows.forms.messagebox.show("no connection detected!!!");             }             return content;         }          public static string replacequerystringparam(string currentpageurl, string paramtoreplace, string newvalue)         {             string urlwithoutquery = currentpageurl.indexof('?') >= 0                 ? currentpageurl.substring(0, currentpageurl.indexof('?'))                 : currentpageurl;              string querystring = currentpageurl.indexof('?') >= 0                 ? currentpageurl.substring(currentpageurl.indexof('?'))                 : null;              var queryparamlist = querystring != null                 ? httputility.parsequerystring(querystring)                 : httputility.parsequerystring(string.empty);              if (queryparamlist[paramtoreplace] != null)             {                 queryparamlist[paramtoreplace] = newvalue;             }             else             {                 queryparamlist.add(paramtoreplace, newvalue);             }              return string.format("{0}?{1}", urlwithoutquery, queryparamlist);         }          private void button_click_1(object sender, routedeventargs e)         {             string user = textbox1.text;             string password = textbox2.password;             string currenturl = "http://xpleria.com/login.php?query=login&user=wcam&pass=wireless";             string newurlwithchangedsort = replacequerystringparam(currenturl, "user", user);             string newurl = newurlwithchangedsort;             string finalurl = replacequerystringparam(newurl, "pass", password);             string result= getcontent(finalurl);             string value = result.substring(0, 8);              string invalid = "xpleria0";             string valid = "xpleria1";              if (value.equals(invalid))             {                 system.windows.messagebox.show("the username and/or password have entered invalid, please try again");             }             else if (value.equals(valid))             {                 string sessionid = result.substring(8, 32);                 system.windows.messagebox.show("hi, welcome cleta");                 this.close();                  using (new user_login.loading.pleasewait(this.location))                 {                     w.show();                     populate();                 }             }         }          public system.drawing.point location { get; set; }     } } 

i'm going recommend take step , start giving serious consideration organizing code. realize isn't answer question asked is answer question should asking.

first of all, code relating retrieval of these items url should moved class of kind. class should accept url string constructor parameter , gather appropriate data. should create class use populate data each individual item , expose list. time you're done code in window should little more complex than:

var itemsgetter = new itemsgetter(url); foreach(var item in itemsgetter.items) {     // populate listview } 

once you're done recommend create usercontrol. user controls extremely useful in situations need represent dynamic number of data entities each own set of controls allow operations performed on each one. should create usercontrol label , 2 buttons need. usercontrol's constructor should expect parameter of data type created represent each 1 of classes. there can have buttons operate on data type necessary.

finally, you'll need way have usercontrol interact window. example 1 of buttons "delete". you'd want item disappear list once operation complete. don't tempted tie in control window passing parameter or something. instead, read on action events , learn how can create event on user control bind in foreach loop of window when you're populating list view. when usercontrol has completed delete operation triggered button can raise usercontrol's event prompt window remove control list view.

last not least, name controls.

hopefully helps.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -