Click here to Skip to main content
15,913,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have an textbox which accepts numeric and decimal values.
currently am uisng replace method such as

if (txtPlannedHours.Text.Length>1 && 
     !(String.IsNullOrWhiteSpace(txtPlannedHours.SelectedText)))
{
  txtPlannedHours.Text = 
     txtPlannedHours.Text.Replace(txtPlannedHours.SelectedText, e.Text);
}


So now if i have a value as 126 and if i select only 26 and try to replace with 8, the textbox is displaying as 818 instead of 18. It is appending a replaced value at the beginning and the end of the textbox and always the position of the cursor is being displayed at the beginning of the textbox.

Can anyone provide me a solutio for this?

Am adding the above code in an event.
Am using C# with WPF.
Posted
Updated 13-Mar-14 0:38am
v2
Comments
Aravindba 13-Mar-14 6:32am    
why u use replace method,in ordinay if u select any number and clear that number and type new one,like ordinary backspace
Member 10593922 13-Mar-14 6:35am    
because i was not able to edit the selected value so i used...
can u please provide me some sample of how to implement what you are saying?
BillWoodruff 13-Mar-14 7:00am    
What Event are you using, and what determines the content of e.Text ?
Member 10593922 13-Mar-14 7:06am    
Am using private void txtPlannedHours_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (txtPlannedHours.Text.Length > 1 && !(String.IsNullOrWhiteSpace(txtPlannedHours.SelectedText)))
{



txtPlannedHours.Text = txtPlannedHours.Text.Replace(txtPlannedHours.SelectedText, e.Text);

txtPlannedHours.SelectionStart = txtPlannedHours.Text.Length;
txtPlannedHours.Focus();
}

}


and e.text will contain the value each time entered in the textbox
BillWoodruff 13-Mar-14 7:18am    
I don't use WPF, but I think a key issue here is observing exactly what the contents of e.Text is before you execute the 'Replace method. I suggest you put a break-point on the line starting with 'txtPlannedHours.Text = ... and observe what e.Text is.

1 solution

The WPF Event PreviewTextInput is specific to WPF's implementation of all Controls. I cannot replicate the behavior you observe in WPF in WinForms.

After some thought, I conclude that you are having two problems:

1. unexpected side-effects of the replace operation

2. failure to observe expected change in the position of the insertion cursor in the TextBox.

Issue #2 can, I hypothesize, be explained by the fact that the TextBox Control must have Focus before the operations you perform.

Issue #1 is more puzzling. But, a good first step in evaluating whether both issues might be related to the TextBox not having Focus is for you simply to re-locate setting Focus to the TextBox before you operate on it:
C#
txtPlannedHours.Focus();

txtPlannedHours.Text = txtPlannedHours.Text.Replace(txtPlannedHours.SelectedText, e.Text);

txtPlannedHours.SelectionStart = txtPlannedHours.Text.Length;
Now, if this change makes no difference, the next logical hypothesis is that there is something in the specific WPF Event you are handling that's affecting the outcome here.

So, it becomes even more critical to carefully observe ... by using break-points, or writing to the Console ... what the values of both the 'SelectedText and the value of e.Text is before and after you execute your code that changes the contents of the TextBox.

I suggest you try a variety of selections, and look at the before-and-after values, and see if you see a pattern. Consider posting those observations here.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 13-Mar-14 14:34pm    
Some good points here, my 5.
By they way, thank you very much for pointing out my mistake.
Yes, I just miscalculated, sorry. I removed my mistaken answer.
—SA
BillWoodruff 13-Mar-14 22:18pm    
Ah, Sergey, if only in "real life" we could just delete our mistakes :) A "devil's advocate" with knowledge of contemporary neuro-science might argue that we do, since recent research confirms more and more the fact that our memories are continually edited, and re-edited, deconstructed, and reconstructed ad hoc to serve the need for the fictions of sanity, and the perceptions of ourself as "rational," without which we would be, paradoxically, both unimaginably free, and, functionally, insane.

Unfortunately (?), the memories we alter, or delete, cannot alter, or delete, the consequences of our actions, and our ultimate responsibility for them.
Sergey Alexandrovich Kryukov 13-Mar-14 23:58pm    
Well placed question mark. I would say, quite fortunate.

Interesting talking. I feel that I would not need "recent research", because I probably always knew, on personal level, that my memory is being reconstructed, and so on. At the same time, when I was a child, I suddenly realized that we never ever forget anything, and I continue to believe in that, so far. There is no contradiction here. "Reconstructed" memory is just collected in upper layers and hides layers lying below. Probably, there can be some ways to recall what's hidden underneath.

I remember that, to my surprise, when my son was some 5 years old, he suddenly, without any connection to the moment of conversation, told me: "you know one thing? my memory is eternal"...

—SA
BillWoodruff 14-Mar-14 3:12am    
"you know one thing? my memory is eternal" How truly wonderful your son said that ! Through my long first career as a social worker, and psychotherapist, and through extensive exploration of myself in various western and "eastern" modes, as well as my avid following of current research in the biological foundations of consciousness, and neuro-science, I have come to suspect that many of my most vivid early memories are essentially fantasias. But ... how would I really know ? That is the question ! I like Robert Louis Stevenson's words:

“...for no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall.
Sergey Alexandrovich Kryukov 14-Mar-14 9:23am    
So interesting! But, to some extent, some of these memories can be validated. It becomes very apparent, for example, if you can watch a movie you did not see for a long while. If you remember some scene which is not there you could suspect it was cut out, but if you watch some scene which appears differently compared to what you remember... And again, this is not true forgetting. This is hiding of real memory by your fantasy, or conception. People mentally hide what they really see under the imagination generated by some simplified conception. False recollections is a very well known phenomenon.

(Do you know that the phrase "Elementary, Watson" never appears as is in any of the original A. Conan Doyle's texts? :-)

—SA

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