Click here to Skip to main content
15,919,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,Im programming a wpf that uses usercontrol tht contain a textbox and a button,and now I wanna help the user restaure what he wrote before in the textbox,Can u give me an idea how to do that especially that Im using many usercontrol,is there a method to save then restaure the state of the app?
Posted
v2


There is no method that I know of that will do what you want. But there are ways in which to accomplish what I think you want. Here's one.



Use a ComboBox, instead of a TextBox. I assume that your button signals that text is available in your TextBox. This same concept applies to the ComboBox. Declare a list that will contain all of the input entered earlier by your user. It will initially be empty.


C#
List < string >     earlier_entries = new List < string > ( );


If you want to keep the input from earlier executions, save and restore the list to/from a user-accessible file.



Let's name the ComboBox user_entry_CB and the Button user_entry_BUT.



The following code is the event handler for the user_entry_BUT click event.


C#
// ************************************** user_entry_BUT_Click

private void user_entry_BUT_Click ( object    sender,
                                    EventArgs e )
    {

    if ( String.IsNullOrEmpty ( user_entry_CB.Text ) )
        {
        MessageBox.Show ( "You must supply an entry",
                          "Missing Entry" );
        }
    else
        {
        string  text = user_entry_CB.Text;

        if ( !earlier_entries.Contains ( text ) )
            {
            earlier_entries.Add ( text );
            }

        user_entry_CB.Text = String.Empty;
        user_entry_CB.DataSource = null;
        user_entry_CB.DataSource = earlier_entries;
        user_entry_CB.Focus ( );

        // text contains the current user's entry
        }
    }


The only trick is to maintains the earlier user entries in the list and setting the DataSource of the ComboBox to the list. Remember to set the ComboBox's DataSource to null before reassigning the list to it.



Hope that helps.

 
Share this answer
 
Comments
Prince Damon 18-Mar-14 4:41am    
Hi,
so I tried what you advised me to do,it works,but I need a textbox so that the user enters whatever he wants to write.
Now I want to ask when I insert dynamic usercontrol in the mainwindow, can I know when the content of the usercontrol changed,I mean when it's created or its textbox content changed or an new dynamic usercontrol is created in its inside
gggustafson 18-Mar-14 12:28pm    
A ComboBox is made up of a text portion and a dropdown portion. The user can enter text in the text portion or may choose an item from the dropdown portion. When the user chooses an item from the dropdown, the chosen item is copied to and displayed in the text portion. The button is used to signal that text is available.

Before responding, I need some clarification regarding your second question.

By "mainwindow" do you mean the application's form? If so, I strongly suggest that you not use the form as a location in which to place a "dynamic usercontrol". Place a Panel into the Form and populate the Panel with controls.

By "dynamic", do you really mean "comes into existence; goes out of existence" as the execution proceeds? This would open the issues of displaying a control that is created during execution or using the Visible property of a control.

By "usercontrol", do you really mean "UserControl"? This requires that you understand how to combine WinForm components (DialogBox, TextBox, ComboBox, RadioButton, etc.) into your own UserControl. If you do not mean UserControl, what do you mean? An example of a UserControl is the PasswordEye Control. An example of a user-drawn control is Anatomy of a UserControl (SliderControl).

For me to help, you MUST understand WinForm controls.
You can try to use Application/User Settings. I've done that many time in Winforms apps, but i think that using it in WPF app is very easy too. Try to look here:
1. http://blogs.msdn.com/b/patrickdanino/archive/2008/07/23/user-settings-in-wpf.aspx[^]
2. http://geekswithblogs.net/mbcrump/archive/2010/06/17/configuring-applicationuser-settings-in-wpf-the-easy-way.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900