0
votes

Hi I am working in WPF C# application and I am new.

In my application, there is a stack panel, lets call this stack panel as first stack panel. This stack panel has one text box whose width and height are not set. It has margin of 100. Its text wrapping is set to wrap.

User provides text information in to the text box. If user want more text box, then there is a button. Upon clicking this button, a new text box with previous text box properties appears in the first stack panel and user continuous typing.

The main goal is to show, the print preview of the first stack panel while user typing text into the text boxes of the first stack panel.

So what I did is: I have another stack panel, lets call this second stack panel beside to the first stack panel. There is function or method which is called upon each key press. This function or method, collects all the text from each text box from first stack panel into a list (of type string) and then it adds each string from the list as textblock (children) to second stackpanel. And once all the string from the list added to the second stackpanel, the textbox from the first stackpanel gets back focus so that user can able to continue typing text.

So far It is working fine. The problem is, as user adds more textboxes, it takes longer time to appear in the second stackpanel and set back the focus to the textbox from first stack panel.

To over come the situation, I used the following code:

private void OnPreviewKeyUp(object sender, KeyEventArgs e){

//code to collect text from each textbox from first stackpanel and add into a list of type string.

           this.Dispatcher.Invoke(()=> {
                    ReloadTextInfoToPanel(textBoxTextList);   
             });
}

the above code, still taking same amount of time. I even tried using

this.Dispatcher.InvokeAsync();

But same amount of time it is consuming.

What I want is, when user is typing in the text boxes of first stack panel then the function or method should be called independently, with out blocking the control.

I hope you have understood, what is the issue. Pardon my Bad English. Please guide me. Thanks in advance.

1
May I ask why you don't create a corresponding TextBlock in the second panel when each TextBox is created in the first panel and then simply create a one way binding between their respective properties? - o_weisman
Thanks o_weisman for your response. the second stack panel has a4 size. The function which adds texts from first stack panel to the second stack panel, it also checks for available space in the second stack panel and if no space is avaialble then it adds new stack panel and adds the text into it. If I keed on adding textbloks mean while user adds new textboxes then if later on, user changes the text then the texts in the second panel needed to adjust also to fit on each a4 size stack panel. Please note, the second stack panel is print version of the first stack panel. - Kumar Sree
Then answer from @RUL is good advice, however the real issue is what is going on in ReloadTextInfoToPanel. If you use MVVM concepts here (Data Binding) you should be able avoid interrupting the user at all. So show us your code and we can provide you a more efficient routine. - Chris Schaller

1 Answers

1
votes

This is not a direct answer for your question, but I cannot make comments, but still want to try to help


You may consider updating the second panel every second or two (using a timer), instead of every key hit. This will lower the times of update significantly, but may not bother the user.