This is the first of 2 posts on the subject. In this post I'm going to explain how I manipulate the text to create cut and paste within the app. In the next I'll show how I created a shared clipboard. And how you can add the functionality to your own apps too!
The cutting, copying and pasting of text within an app is simple really. It basically involves storing a copy of the text in question in a string variable within the code.
private string localClipboard;
To be able to select more than a single word, the code supports a "shift" mode:
private bool shiftSelected; private int shiftSelectPositionStart; private int shiftSelectPositionLength; private void ShiftClick(object sender, EventArgs e) { shiftSelected = !shiftSelected; shiftSelectPositionStart = tb.SelectionStart; shiftSelectPositionLength = tb.SelectionLength; }
Which can be combined with a SelectionChanged event on the text to capture the larger string. (Being sure to consider if the new selection point is before or after the original/previous selection.)
private void SelectionChanged(object sender, RoutedEventArgs e) { var currentStart = tb.SelectionStart; var currentLength = tb.SelectionLength; if (shiftSelected) { if (shiftSelectPositionStart < currentStart) { tb.SelectionStart = shiftSelectPositionStart; tb.SelectionLength = currentStart - shiftSelectPositionStart + currentLength; } else { tb.SelectionStart = currentStart; tb.SelectionLength = shiftSelectPositionStart - currentStart + shiftSelectPositionLength; } } shiftSelected = false; }Copying text is easy:
private void CopyClick(object sender, EventArgs e) { this.localClipboard = tb.SelectedText; }But cutting text is slightly more complicated as we need to replace the original string with the text before and after the selection.
private void CutClick(object sender, EventArgs e) { this.localClipboard = tb.SelectedText; var removeAt = tb.SelectionStart; var afterCut = tb.Text.Substring(removeAt + tb.SelectionLength); var beforeCut = tb.Text.Substring(0, removeAt); tb.Text = beforeCut + afterCut; tb.SelectionStart = beforeCut.Length; }Then that just leaves pasting the text back in at a newly selected location.
private void PasteClick(object sender, EventArgs e) { var insertAt = tb.SelectionStart; tb.Text = tb.Text.Substring(0, insertAt) + this.localClipboard + tb.Text.Substring(insertAt + tb.SelectionLength); tb.SelectionStart = insertAt + this.localClipboard.Length; tb.SelectionLength = 0; }The above also selects the newly pasted string. Simples! Now, while you digest all that, please head on over to http://www.wp7comp.com/scratch-pad/ and leave some lovely comments - Thanks.
is it possible to get text from Webpage ?
ReplyDeleteI would like to make a browser by using C# that is able to get selected text and automatically show the selected text in textbox.
I also tried using JavaScript to make function that copys and pastes text from webpag.
finally, I figured out that I cannot do!
If you have any idea to solve this problem, please let me know.
I need your wonderful technique.
I need you. please help me!
PS. I am using now:
viusal stdio 2010 express for windows phone
windows phone SDK7.1
thank you.
Samurai
@Samurai
ReplyDeleteAssuming that you're just using an embedded browser control in your app then I don't believe it's possible to get any selected text as there are no APIs availabel to provide it.
If you really must get at the selected text in rendered HTML you'll need to display it yourself but I can't imagine the use case which would justify the amount of work that would be needed to do that across arbitrary sites.
new orleans saints jerseys
ReplyDeleteferragamo shoes
true religion outlet
ghd hair straighteners
cartier watches
kate spade
air jordan 11
kate spade sale
cheap nike shoes
dallas cowboys jersey
prada handbags
true religion jeans
babyliss hair dryer
ray ban aviator
cheap nfl jerseys
louis vuitton outlet
true religion
karen millen sale
michael kors canada
louis vuitton outlet online
tiffany and co
michael kors uk
gucci outlet
the north face outlet store
karen millen dresses
chelsea jersey
iphone 6 cases
mont blanc pens
jordan 4 retro
michael kors outlet online sale
hermes outlet
kobe shoes
abercrombie outlet
swarovski jewelry
louis vuitton outlet
tory burch outlet online
tods shoes
nike free uk
instyler ionic styler pro
moncler coats
0722maoqiuyun
san antonio spurs, http://www.sanantoniospursjersey.com/
ReplyDeletesalomon running shoes, http://www.salomonshoes.us.com/
golden state warriors, http://www.warriorsjersey.com/
ralph lauren,ralph lauren uk,ralph lauren outlet,ralph lauren outlet online,polo ralph lauren outlet,ralph lauren polo
minnesota vikings, http://www.minnesotavikingsjersey.us/
hermes bags, http://www.hermesbags.co.uk/
adidas outlet store, http://www.adidasoutletstore.us.com/
canada goose coats, http://www.uggbootscheap.eu.com/
michael kors outlet store, http://michaelkors.outletonlinestores.us.com/
nike air max,air max,air max 2014,nike air max 2014,air max 90,airmax,air max 95,air max 2013,nike air max 90,nike air max 2013
cheap mlb jerseys, http://www.cheapmlbjerseys.us.com/
cyber monday, http://www.blackfriday2015.in.net/
michael kors outlet,michael kors outlet online,michael kors,kors outlet,michael kors handbags,michael kors outlet online sale,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors
moncler jackets, http://www.uggoutletstore.eu.com/
soccer jerseys,soccer jerseys wholesale,soccer jerseys cheap,soccer jerseys for sale,cheap soccer jersey,usa soccer jersey,football jerseys
canada goose outlet, http://www.canada-goosejackets.us.com/
air jordan 4 retro, http://www.airjordan4.org/
ugg uk outlet, http://www.snowboots.us.com/
air jordan 11 retro, http://www.airjordan11.net/
kkkk1012