Is it possible to convert a TextPointer to an Integer - c#

I have been editing some long existing code to implement some new functionality. this requires replacing a textbox with a richtextbox whilst keeping the existing implementation the same. the rich text box had comparable properties to the textbox until i got to the caretindex. The original line (which worked for a textbox) was:
this.ViewModel.Body = this.ViewModel.Body.Insert(this.txtEditor.CaretIndex, link);
so when i changed "txtEditor" to a richtextbox i implemented this:
this.ViewModel.Body = this.ViewModel.Body.Insert(this.txtEditor.CaretPosition, link);
i believe these are similar however "Caret Position" is of type TextPointer whereas "CaretIndex" was an integer and the Body.Insert method will only take an integer as its first parameter.
Is there any way to convert a TextPointer to and Integer and if so, how is it done?
Thanks

It is somewhat convoluted, but it is possible to get the caret index using some hacks:
var start = txtEditor.Document.ContentStart;
var here = txtEditor.CaretPosition;
var range = new TextRange(start, here);
int indexInText = range.Text.Length;
To use:
this.ViewModel.Body = this.ViewModel.Body.Insert(indexInText, link);

Related

How to change text dynamic in unity

at line 161,I want to insert my text in parameter t,but it won't change when i debug it.although the parameter tmp had alredy changed.
I want to change this Text in UI,when my parameter t changes.
With respect to your specific issue, Insert is defined as:
public string Insert (int startIndex, string value);
and returns a new string. In C#, strings aren't modified, new strings are created. In this way, they act like a value type, even though they're a reference type. In other words, once a string is created, it is never modified - it's 'immutable'. So, you need to store your newly created string.
In cases like this, I like to use the string interpolation, as it allows me to get a slightly clearer representation of what the final string will look like.
var tmp = System.Text.Encoding.UTF8.GetString ( e.Message );
t.text = $"{tmp}\n{t.text}"; // Note that a newline is represented as \n
Or, if you add the System.Text namespace; you could reduce it down to:
using System.Text;
...
t.text = $"{Encoding.UTF8.GetString ( e.Message )}\n{t.text}";
The string type in c# is immutable, therefore Insert returns a new string instead of modifying the current one.
Do:
t = t.text.Insert(0, tmp + "//n");
See also
How to modify string contents in C#

Method name expected in C#

So, I am developing an application which is able to drag and drop files into the form and display the information of it into a datagridview. Basically I already got something developed in VB.Net and now I want to upgrade it to C# and make somethings better adding features.
I am stuck at the moment because in VB.Net I have this peace of code
Private MeuFicheiro As FileInfo
Private Sub frmMenu_DragDrop(sender As Object, e As DragEventArgs) Handles cmdEntrar.DragDrop
Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
MyFile = New FileInfo(ficheiro)
If String.IsNullOrWhiteSpace(MyFile.Extension) Then
Exit Sub
End If
End Sub
And I trying to use the Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0) C#, but making some changes of course like this:
string file = e.Data.GetData(DataFormats.FileDrop)(0); in the form method:
private void frmMenu_DragDrop(object sender, DragEventArgs e)
{
string ficheiro = e.Data.GetData(DataFormats.FileDrop)(0);
MeuFicheiro = new FileInfo(ficheiro);
if (string.IsNullOrWhiteSpace(MeuFicheiro.Extension))
{
return;
}
}
But it gives me an error in the e.Data.GetData(DataFormats.FileDrop)(0) called Method name
Method name expected
So do you guys have something to help me solving this error?
change from parenthesis to square brackets. In C# square brackets are used for indexing arrays.
Do this:
string file = e.Data.GetData(DataFormats.FileDrop)[0]; // see square brackets
If GetData returns IList then change e.Data.GetData(DataFormats.FileDrop)(0) to e.Data.GetData(DataFormats.FileDrop)[0]
There are actually two errors on your code:
First, DataObject.GetData returns an Object instance. You will first need to cast that object to the type you are expecting to be stored on that Drag event.
Then, assuming that the returing type was indeed a collection of Strings, you need to use square brackets ([]) to access the first item.
var collection = e.Data.GetData(DataFormats.FileDrop) as IList<string>;
string ficheiro = collection[0];
Note that I casted to IList<string>, as that will cover most of C#'s indexable generic collections (array, list, hashSet...).
In the other hand, if that object GetData returns is a string, you should do:
string ficheiro = e.Data.GetData(DataFormats.FileDrop).ToString();

String.Remove doesn't work [duplicate]

This question already has an answer here:
string.Remove doesnt work [duplicate]
(1 answer)
Closed 9 years ago.
I have such code and can't understand where is the mistake, despite the fact, that this code pretty easy. So q is a full path, and I need to get required path to Gen_ParamFile
string q = #"C:\ProgramData\RadiolocationQ\script-Data=12^6-12^33.xml";
string _directoryName1 = #"C:\ProgramData\RadiolocationQ";
int Length = _directoryName1.Length + "ascript".Length;
string Gen_ParamFile = q;
Gen_ParamFile.Remove(0, Length); // this line don't do anything
var Gen_Parfile = Path.Combine(_directoryName1, "GeneralParam-Data" + Gen_ParamFile);
I used function like said here http://msdn.microsoft.com/ru-ru/library/9ad138yc(v=vs.110).aspx
It does, it just doesn't affect the actual string, it creates a new one as a result. Use:
Gen_ParamFile = Gen_ParamFile.Remove(0, Length);
Because String.Remove method returns a new string. It doesn't change original one.
Returns a new string in which a specified number of characters in the
current instance beginning at a specified position have been deleted.
Remember, strings are immutable types. You can't change them. Even if you think you change them, you actually create new strings object.
You can assign itself like;
Gen_ParamFile = Gen_ParamFile.Remove(0, Length);
As an alternative, you can use String.SubString method like;
Gen_ParamFile = Gen_ParamFile.SubString(Length);
defiantly you can use like
Gen_ParamFile = Gen_ParamFile.Remove(0, Length);
apart from that you can also use
String.Substring method according to your requirment

Encoding fails when marshalling string via COM Interop in C# (double UTF8 encoding?)

I'm writing a plugin for Autodesk Navisworks, trying to pass a C# unicode string to a property on a COM object. However, the string is encoded incorrectly somewhere in the process.
var property = ...;
property.Name = "中文"; // becomes "??"
property.Value = "中文"; // OK
"中文" comes out as "??" in the user interface, whereas strings limited to ASCII work just fine (e.g. "abcd"). Furthermore, setting the Value-property (a VARIANT) on the same object works just fine, but not the Name.
Further exploration leads me to try encoding the string "ä" as utf-8:
C3 A4
and somehow "encoding" this into a (unicode) string:
property.Name = "\u00c3\u00a4"; // shows up as "ä"
Surprisingly this seemed to work.
This led me to try the following:
var bytes = Encoding.UTF8.GetBytes("中文abcd");
char[] chars = new char[bytes.Length];
for(int i = 0; i < chars.Length; i++)
chars[i] = (char)bytes[i];
string s = new string(chars);
However, when I use this trying to encode "中文abcd" I only get the first character "中" in the GUI. Yet, with "äabcd" I get more than one character again...
What is happening here? How can I get around the problem? Is it a marshalling problem (e.g. incorrectly specified encoding in the COM Interop)? Or perhaps some weird code inside the application? If it's a marshalling problem, can I modify it for this property only?
Turns out that Name was an "internal" string, and I should have used the property UserName for text displayed in the GUI.
I.e. I changed:
var property = ...;
property.Name = "中文"; // becomes "??"
property.Value = "中文"; // OK
to this:
var property = ...;
property.UserName = "中文"; // OK!
property.Value = "中文"; // OK
which worked. Presumably UserName is implicitly set from Name internally in some way ignoring or mishandling the encoding.

textbox Insert and remove function in vb.net or c#

How to update the text from the TextBox control?
Consider a TextBox that already contains the string "Wel"
To insert text in the TextBox, I use:
TextBox1.Text = TextBox1.Text.Insert(3, "come")
And to remove characters from the TextBox:
TextBox1.Text = TextBox1.Text.Remove(3, 4)
But I need to be able to do this:
TextBox1.Text.Insert(3, "come");
TextBox1.Text.Remove(3, 4);
However, this code doesn't update the TextBox.
It this possible?
Can this be accomplished via the append method?
Text property of TextBox is of type string which is immutable it's not possible to change the existing string. Insert() or Remove() returns a new instance of string with the modification and you will have to assign this new instance back to TextBox's Text property.
There is TextBox.AppendText() that you might be interested in. It appends text to the end of the string but you cannot do anything like Insert() or Remove() with it though.
EDIT:
for your keypress, you could do something like this
char charToReplace = (char) (e.KeyChar + 1); // substitute replacement char
textBox1.SelectedText = new string(charToReplace,1);
e.Handled = true;
'string' is a immutable type, so each time string value changes new memory is allocated. So if you want to insert or remove text from TextBox, you have to assign it back to TextBox.Text property. However, if you just want to append text to the TextBox.Text, you can do
textBox.AppendText("Hello");

Categories