c# - How to call A) a textBox B) And button click event inside a Form(That is created through a class in a button click event) -
this question has answer here:
- communicate between 2 windows forms in c# 10 answers
i created class
public static class prompt { public static form showcategorydialog() { form addcategorydialog = new form() { width = 500, height = 150, formborderstyle = formborderstyle.fixeddialog, text = "caption", startposition = formstartposition.centerscreen }; label textlabel = new label() { left = 50, top = 20, text = "asd" }; textbox textbox = new textbox() { left = 50, top = 50, width = 400 }; button confirmation = new button() { text = "ok", left = 350, width = 100, top = 70, dialogresult = dialogresult.ok }; confirmation.click += (sender, e) => { addcategorydialog.close(); }; addcategorydialog.controls.add(textbox); addcategorydialog.controls.add(confirmation); addcategorydialog.controls.add(textlabel); addcategorydialog.acceptbutton = confirmation; return addcategorydialog; } } now in form's button click event
private void addcateogoryclick(object sender, eventargs e) { form = new form(); = prompt.showcategorydialog(); a.showdialog(); } there, textbox, button , label created in form , form assigned form 'a'. since form 'a' has textbox, button , label, want call them like
if(a.textbox.text == string.empty) { //do } and access it's button_click event also, like
a.button_click { if(button.text == "ok") //do } summing up: dont know how access form's control(textbox, button etc) , dont know how use events in situation.
derive new class form. in constructor add controls same way in showcategorydialog(), , each control add property let access control. e.g. sample shows how button:
public class categorydialog : form { public categorydialog() { button1 = new button() { text = "ok", left = 350, width = 100, top = 70, dialogresult = dialogresult.ok }; controls.add(button1); } button button1; // property let access button public button button1 { { return button1; } } } when create instance of form, can access button this:
categorydialog = new categorydialog(); a.button1.text = ... events (like button click) accessible automatically without doing anything. e.g.
a.button1.click += (sender, e) => { //sender parameter contains instance of button clicked var button = sender button; if(button.propertytobechecked == ...)//check whatever want ... }; if using visual studio, it's simpler. add new form project (it create derived class automatically), drag controls (button, label, etc.) form , click on each control , change it's property "modifiers" public.
Comments
Post a Comment