c# - passing value to handler -
i have code this:
private void button1_click(object sender, eventargs e) { openfiledialog1.showdialog(); } private void openfiledialog1_fileok(object sender, canceleventargs e) { string ext = path.getextension(openfiledialog1.filename); if(string.compare(ext, ".fdb") == 0) { string filename = openfiledialog1.safefilename; string filedirectory = path.getdirectoryname(openfiledialog1.filename); string databasetxt = @"c:\users\arist\appdata\roaming\tdwork\"; string[] database = { filedirectory + filename }; if (directory.exists(databasetxt)) { system.io.file.writealllines(databasetxt + "databases.txt", database); } else { directoryinfo di = directory.createdirectory(databasetxt); system.io.file.writealllines(databasetxt + "databases.txt", database); } } else { messagebox.show("fajl koji ste izabrali nije firebird baza (.fdb)"); e.cancel = true; } }
now, want create more buttons open same file dialog. problem want pass openfiledialog directory different textboxes. logic this:
if open button1, pass value textbox1, if open button2, pass value textbox2, if open button3, pass value textbox3.
so wanted create int check (1, 2, 3) when press button1, pass check = 1 opendialog1_fileok, switch there , actions.
problem not know how pass handler, , if possible. if there other solution, please write it.
first, use openfiledialog this, without handling whole new function it:
if(openfiledialog1.showdialog() == dialogresult.ok){ //...code }
second, goal you'll have sure names of controls ending on digit want (e.g. "button1" , "textbox1"). can this:
void button1click(object sender, eventargs e) { //messagebox.show(bt.name[bt.name.length - 1].tostring()); if(openfiledialog1.showdialog() == dialogresult.ok) { if(!path.getextension(openfiledialog1.filename).endswith(".fdb")) //checking if extension .fdb (as you've shown in example) { messagebox.show("fajl koji ste izabrali nije firebird baza (.fdb)"); return; //return if it's not , no further code gets executed } string filedirectory = path.getdirectoryname(openfiledialog1.filename); //getting directory string nameofmybutton = (sender button).name; //you name of button int lastdigitofmyname = convert.toint16(name[name.length - 1]); //returns number of button textbox neededtextboxtoshowdirectory = this.controls.find("textbox" + lastdigitofmyname, true).firstordefault() textbox; //this search control name "textbox1" neededtextboxtoshowdirectory.text = filedirectory; //you display text //... doing rest of stuff here } }
Comments
Post a Comment