Reseting the text in a control.
-
In a windows form I have a textbox and a button. Initially the text box contains "Enter ID" as the default text. I enter some ID and click the button which saves the ID to an XML. After that, the text in the textbox should revert back to "Enter ID" as before. Right now my code is a below... // Windows Form Designer generated code // First time I intialize the dafault text this.txtbox1.Text = "Enter ID"; // Button_click private void button1_Click(object sender, System.EventArgs e) { //Write To File and reset all textboxes on the form for default text SaveToXML(); ResetAllTextBoxes(); } // Reset all text boxes on the window private void ResetAllTextBoxes() { this.textbox1.Text = "Enter ID"; .... .... this.textbox10.Text = "There has to be some another way" } I'm not satisfied with this implementation because if I have 10 textboxes I need to have 20 lines of code doing the same. 10 lines in the Windows Form Designer generated code and 10 in ResetAllTextBoxes() method. I tried the textbox1.ResetText() method but instead of reseting it to default text such as "Enter ID" it resets to blank. Is there a better solution. Thanks ---------- Venus Patel http://patelsinc.blogspot.com/ A student knows little about a lot. A professor knows a lot about little. I know everything about nothing. -- modified at 17:25 Tuesday 10th January, 2006
-
In a windows form I have a textbox and a button. Initially the text box contains "Enter ID" as the default text. I enter some ID and click the button which saves the ID to an XML. After that, the text in the textbox should revert back to "Enter ID" as before. Right now my code is a below... // Windows Form Designer generated code // First time I intialize the dafault text this.txtbox1.Text = "Enter ID"; // Button_click private void button1_Click(object sender, System.EventArgs e) { //Write To File and reset all textboxes on the form for default text SaveToXML(); ResetAllTextBoxes(); } // Reset all text boxes on the window private void ResetAllTextBoxes() { this.textbox1.Text = "Enter ID"; .... .... this.textbox10.Text = "There has to be some another way" } I'm not satisfied with this implementation because if I have 10 textboxes I need to have 20 lines of code doing the same. 10 lines in the Windows Form Designer generated code and 10 in ResetAllTextBoxes() method. I tried the textbox1.ResetText() method but instead of reseting it to default text such as "Enter ID" it resets to blank. Is there a better solution. Thanks ---------- Venus Patel http://patelsinc.blogspot.com/ A student knows little about a lot. A professor knows a lot about little. I know everything about nothing. -- modified at 17:25 Tuesday 10th January, 2006
No. You have to write the code to change the values, and the auto generated code will stay the same. Personally, I would not set the values in the forms designer, but just in a method, and call the method after InitializeComponents is called, just to put it all in one place. Christian Graus - Microsoft MVP - C++
-
In a windows form I have a textbox and a button. Initially the text box contains "Enter ID" as the default text. I enter some ID and click the button which saves the ID to an XML. After that, the text in the textbox should revert back to "Enter ID" as before. Right now my code is a below... // Windows Form Designer generated code // First time I intialize the dafault text this.txtbox1.Text = "Enter ID"; // Button_click private void button1_Click(object sender, System.EventArgs e) { //Write To File and reset all textboxes on the form for default text SaveToXML(); ResetAllTextBoxes(); } // Reset all text boxes on the window private void ResetAllTextBoxes() { this.textbox1.Text = "Enter ID"; .... .... this.textbox10.Text = "There has to be some another way" } I'm not satisfied with this implementation because if I have 10 textboxes I need to have 20 lines of code doing the same. 10 lines in the Windows Form Designer generated code and 10 in ResetAllTextBoxes() method. I tried the textbox1.ResetText() method but instead of reseting it to default text such as "Enter ID" it resets to blank. Is there a better solution. Thanks ---------- Venus Patel http://patelsinc.blogspot.com/ A student knows little about a lot. A professor knows a lot about little. I know everything about nothing. -- modified at 17:25 Tuesday 10th January, 2006
You could make your own custom textbox that inherits the built in textbox, and that has a DefaultText property. To easily reset all textboxes, keep a static Arraylist in the class, where each textbox adds a reference to itself when it is created. Then add a static method to the class that loops through the Arraylist and resets the text of each textbox. --- b { font-weight: normal; }