i'm trying to send one parameter from funtion to one any control on the page but i can not do that. what i'm doing wrong?
i checked that the paramter (PDX) is getting right values and they are changing but nothing shown on the any control on the page. (like textbox, listbox etc. i tried all )
private void OnPDXDataArrived(string PDX, bool Header)
{
TextBox1.Text += PDX;
PDXDataArrived(PDX, Header);
}
public string blabla;
private void PDXDataArrived(string PDX, bool Header)
{
int FieldCount = int.Parse(txtUpload_Fields.Text);
int nCnt = 0;
if (Header == true)
{
ListBox4.Items.Add(PDX);
TextBox1.Text += PDX;
blabla += PDX.ToString();
TextBox1.Text = blabla;
Console.WriteLine(blabla);
}
else
{
strTemplete += PDX + "\r";
string nStemp = strTemplete.Substring(0, strTemplete.Length - 1);
datammm = nStemp.Split(new string[] { "\r" }, StringSplitOptions.None);
for (int dx = 0; dx < datammm.Length; dx++) ListBox4.Items.Add(datammm[dx]);
}
}
Related
I know that SelectionStart property of WinUI UWP TextBox will return the CaretIndex. But, I want to get the exact Column and Line Position of Text. In WPF, GetLineFromCharacterIndex(CaretIndex) and TextBox.Lines[LineIndex].Length could be used to find the Current Line Index and Column number respectively. How can I achieve the same in WinUI UWP Textbox ?
Try this method:
public static int GetCurrentLineIndex(TextBox textBox)
{
int caretIndex = textBox.SelectionStart;
if (caretIndex == 0)
return 0;
string[] lines = textBox.Text?.Split('\r') ?? Array.Empty<string>();
int offset = 0;
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
offset += line.Length;
if (caretIndex <= offset)
return i;
offset++;
}
return 0;
}
It may need some slight improvement but it should give you the idea how you could determine the current line of the cursor.
You can call it from wherever you want to get the index, e.g.:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
int index = GetCurrentLineIndex(sender as TextBox);
//...
}
Maybe you could do something like this:
var text = Textbox.Text;
var lines = text.Split('\r');
...
This has worked for me in the past using WPF but I have never tried UWP.
This also seems like a workaround so there might be a better, more practical, solution.
This example uses MVVM structure but you can apply the same concepts with a temp variable which stores the previous value.
<TextBox Height="600" Width="600"
Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap" AcceptsReturn="True"/>
Then I added this to the constructor:
this.DataContext = this;
This isnt best practice and if you were using MVVM you would set up a ViewModel and use that (I did this for testing purposes).
Then I created my properties like this:
private int _line;
public int Line
{
get { return _line; }
set
{
_line = value;
tb1.Text = value.ToString();
}
}
private int _column;
public int Column
{
get { return _column; }
set
{
_column = value;
tb2.Text = value.ToString();
}
}
private string _text;
public string Text
{
get { return _text; }
set
{
if (_text + '\r' != value)
{
Line = GetLine(_text, value);
Column = GetColumn(_text, value, Line);
}
else
{
Line++;
Column = 0;
}
_text = value;
}
}
Then added my functions:
public int GetLine(string original, string newText)
{
var oLines = GenArray(original);
var nLines = GenArray(newText);
//set this to -1 if you want 0-based indexing
int count = 0;
foreach (var line in nLines)
{
count++;
if (oLines.Length < count || line != oLines[count - 1])
{
break;
}
}
return count;
}
public int GetColumn(string original, string newText, int lineChanged)
{
var oLine = GenArray(original)[lineChanged - 1];
var nLine = GenArray(newText)[lineChanged - 1];
//set this to -1 if you want 0-based indexing
int count = 0;
foreach (var c in nLine)
{
count++;
if (oLine.Length < count || c != oLine[count - 1])
{
}
}
return count;
}
private string[] GenArray(string text)
{
string[] lines;
if (text == null)
{
lines = new string[1] { "" };
}
else if (text.Contains('\r'))
{
lines = text.Split('\r');
}
else
{
lines = new string[1] { text };
}
return lines;
}
If you don't use MVVM just do this:
public string[] TempLines { get; set; }
...
//after the calculation code has finished
TempLines = TextBox.Split('\r');
Then you can substitute TempLines for value
In RichTextBox, I'm trying to fire an event with pressing period ('.') But It's not working for the first time.
If I write "Lorem Ipsum.", It's not working but If I write "Lorem Ipsum ." or "Lorem Ipsum.." It's OK.
PS: I've added the KelimeGuncelle method and GetWordGroupInstances() dictionary also.
Here's the block:
private void rtbMakale_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.OemPeriod)
{
kelimeGuncelle();
}
}
The kelimeGuncelle method:
void kelimeGuncelle()
{
Dictionary<String, int> TekliKelimeGruplari = GetWordGroupInstances(1);
foreach (var item in TekliKelimeGruplari)
{
for (int i = 0; i < lstKelimeler.Items.Count; i++)
{
var kelime = lstKelimeler.Items[i];
string guncellenecekKelime = kelime.ToString().Remove(kelime.ToString().IndexOf(" ( ") - 1);
string gelenKelime = item.Key;
string _guncellenecekKelime = kelime.ToString();
int pFrom = _guncellenecekKelime.IndexOf("(") + 1;
int pTo = _guncellenecekKelime.LastIndexOf("/");
int guncellenecekSayi = Convert.ToInt32(_guncellenecekKelime.Substring(pFrom, pTo - pFrom));
int kFrom = _guncellenecekKelime.IndexOf("/") + 1;
int kTo = _guncellenecekKelime.LastIndexOf(")");
int toplamYazilacakSayi = Convert.ToInt32(_guncellenecekKelime.Substring(kFrom, kTo - kFrom));
int kelimeninSirasi = lstKelimeler.Items.IndexOf(kelime);
if (Equals(guncellenecekKelime, gelenKelime))
{
guncellenecekSayi = item.Value;
lstKelimeler.Items.RemoveAt(kelimeninSirasi);
lstKelimeler.Items.Insert(kelimeninSirasi, guncellenecekKelime + " ( " + guncellenecekSayi + "/" + toplamYazilacakSayi + " )");
//lstKelimeler.Refresh();
}
if (rtbMakale.Text.Contains(guncellenecekKelime) == false)
{
lstKelimeler.Items.RemoveAt(kelimeninSirasi);
lstKelimeler.Items.Insert(kelimeninSirasi, guncellenecekKelime + " ( 0/" + toplamYazilacakSayi + " )");
//lstKelimeler.Refresh();
}
}
}
TekliKelimeGruplari.Clear();
}
And GetWordGroupInstances:
Dictionary<String, int> GetWordGroupInstances(int GroupSize)
{
Dictionary<String, int> WordGroupInstances = new Dictionary<string, int>();
String[] sourceText = GetSourceText().Split(' ');
int pointer = 0;
StringBuilder groupBuilder = new StringBuilder();
while (pointer < sourceText.Length - GroupSize)
{
groupBuilder.Clear();
int offset = pointer + GroupSize;
for (int i = pointer; i < offset; i++)
{
groupBuilder.Append(" ").Append(sourceText[i]);
}
String key = groupBuilder.ToString().Substring(1);
if (!WordGroupInstances.ContainsKey(key))
{
WordGroupInstances.Add(key, 1);
}
else
{
WordGroupInstances[key]++;
}
pointer += 1;
}
return WordGroupInstances;
}
try using a MessageBox.Show("Test"); for testing , maybe something in your kelimeGuncelle() method is wrong.
private void rtbMakale_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.OemPeriod)
{
MessageBox.Show("Test");
}
}
I got it!
In the GetWordGroupInstances, I've changed the while loop. Here:
while (pointer <= sourceText.Length - GroupSize)
in this program, when the Recall button (recallBtn_Click()) is clicked, it calls a method (that calculates directions) from another class which should then call the showPath() method. the show path method should then display its output in a textBox. But the values don't show even though i can see from debugging that the values are being sent to the text box. can anybody tell me where i went wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
storeRetSelect.SelectedIndex = 0;
PrioritySelect.SelectedIndex = 0;
}
public void showPath(List<PathFinderNode> mPath)
{
var T = new Form1().directionsTextBox;
foreach (PathFinderNode node in mPath)
{
if ((node.X - node.PX) > 0) { T.Text += "Right" + System.Environment.NewLine ; }
if ((node.X - node.PX) < 0) { T.Text += "Left" + System.Environment.NewLine; }
if ((node.Y - node.PY) > 0) { T.Text += "UP" + System.Environment.NewLine; }
if ((node.Y - node.PY) < 0) { T.Text += "Down" + System.Environment.NewLine; }
}
}
private void recallBtn_Click(object sender, EventArgs e)
{
var path = new pathPlan();
string desigString = inputTextBox.Text;
int[] desig = new int[3];
for (int i = 0; i < desigString.Length; i++) { desig[i] = (int)char.GetNumericValue(desigString[i]); }
path.Recall(desig[1], desig[2], (-1) * desig[0]);
}
}
With this line you are initialising a new object and get the reference of the textbox there.
var T = new Form1().directionsTextBox;
But I assume you want to use the textbox of the form which is allready open. Change the line to the following to access the textbox of the current object.
var T = this.directionsTextBox;
This code i used to text change as i need.
private void textBox1_TextChanged(object sender, EventArgs e)
{
string A = textBox1.Text.Trim();
A = A.Replace("A", "C");
A = A.Replace("F", "H");
A = A.Replace("C", "W");
A = A.Replace("B", "G");
textBox2.Text = (A);
}
Now i need to stop text changing after,
if i type '|' symbol in tetxbox1, Again i need to
start text changing if i type '|' symbol again,such as happened thing in this image.
So how can i prevent text changing between these two symbols only ||
You're replace code won't work how you have it, as it will just keep changing the characters for the same string(you change A to C, and later down you change C to W, so your final first character would be W and not C like you want).
Below is an overly complicated method(i also added a method that runs through each character of the string doing the replace) but it should work, and you can change as needed. Good luck
private void textBox1_TextChanged(object sender, EventArgs e)
{
string A = textBox1.Text.Trim();
string[] Aarry = A.Split('|');
string cleanedString = "";
for (int i = 0; i < Aarry.Length; i++)
{
if (i % 2 == 0)
cleanedString += FixText(Aarry[i]) + " ";
else
cleanedString += Aarry[i] + " ";
}
textBox2.Text = cleanedString ;
The method below will go through each character doing the replace
public string FixText(string A)
{
string newText = "";
for (int i = 0; i < A.Length; i++)
{
switch (A.Substring(i, 1))
{
case "A":
newText += A.Substring(i, 1).Replace("A", "C");
break;
case "F":
newText += A.Substring(i, 1).Replace("F", "H");
break;
case "C":
newText += A.Substring(i, 1).Replace("C", "W");
break;
case "B":
newText += A.Substring(i, 1).Replace("B", "G");
break;
default:
break;
}
}
return newText;
}
To handle the >500 lines of replacement type you have, you could setup a dictionary using method below:
public Dictionary<string, string> ReturnReplacementDictionary()
{
Dictionary<string, string> dictLibrary = new Dictionary<string,string)()
{
{"A","C"},
{"F","H"},
{"C","W"},
{"B","G"}
};
return dictLibrary;
}
In the above you would just continue adding in all your other replacement values.
Then you would call use that method below instead of the switch case(If you don't add a character/replacement to the dictionary method you can see it will just set the replacement character to blank):
public string FixTextUsingDictionary(string A)
{
Dictionary<string, string> replaceDict = ReturnReplacementDictionary();
string newText = "";
for (int i = 0; i < A.Length; i++)
{
string replacementLetter="";
if (replaceDict.TryGetValue(A.Substring(i, 1), out replacementLetter))
{
newText += replacementLetter;
}
// Added so that if the char is not in the dictionary the output will just have the original char
else { newText += A.Substring(i, 1); }
}
return newText;
}
Good luck
If the text is entered manually not pasted from clipboard, my solution will be:
int counter = 0;
private string replaceSpecial(string A)
{
if (A.Equals("A")) return "C";
else if (A.Equals("F")) return "H";
else if (A.Equals("C")) return "W";
else if (A.Equals("B")) return "G";
else if (A.Equals("|")) return "";
else return A;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.Equals('|'))
{
counter++;
}
if (counter == 0 || counter % 2 == 0)
textBox2.Text += replaceSpecial(e.KeyChar.ToString());
else
textBox2.Text += e.KeyChar.ToString().Replace("|", "");
}
considering that the only entered character is "|".
good luck
void StringReplace(string initialString)
{
bool insideSpecialCharacter = false;
string[] Pattern = { "A-C", "C-W", "F-H", "B-G" };
string specialCharacter = "|";
char[] characters = initialString.ToCharArray();
char?[] Newcharacters = new char?[characters.Length];
for (int i = 0; i < characters.Length; i++)
{
if (!characters[i].ToString().Equals(specialCharacter))
{
if (insideSpecialCharacter)
{
Newcharacters[i] = characters[i];
}
else
{
CheckPattern(Pattern, characters, Newcharacters, i);
}
}
else
{
insideSpecialCharacter = (insideSpecialCharacter) ? false : true;
}
}
txtSecond.Text = string.Concat(Newcharacters).Trim();
}
//-------Checks the Pattern Array and Replaces the Characters-----------
private static void CheckPattern(string[] Pattern, char[] characters, char?[] Newcharacters, int i)
{
for (int j = 0; j < Pattern.Length; j++)
{
string[] replaceValue = Pattern[j].Split('-');
if (characters[i].ToString() == replaceValue[0])
{
Newcharacters[i] = Convert.ToChar(characters[i].ToString().Replace(characters[i].ToString(), replaceValue[1]));
break;
}
else
{
Newcharacters[i] = characters[i];
}
}
}
I'm developing a text editor in C#, and I'm trying to make a line count.
private void updateNumberLabel()
{
Point pos = new Point(0, 0);
int firstIndex = Document.GetCharIndexFromPosition(pos);
int firstLine = Document.GetLineFromCharIndex(firstIndex);
pos.X = ClientRectangle.Width;
pos.Y = ClientRectangle.Height;
int lastIndex = Document.GetCharIndexFromPosition(pos);
int lastLine = Document.GetLineFromCharIndex(lastIndex);
int actualLine = Document.GetLineFromCharIndex(actualPos);
pos = Document.GetPositionFromCharIndex(lastIndex);
if (lastLine != actualLine)
{
numberLabel.Text = "";
for (int i = firstLine; i <= lastLine + 1; i++)
{
numberLabel.Text += i + 1 + "\n";
}
}
}
It works fine and adds the number of lines while you write them, but if you delete one, it will only update if you delete or add one more line.
I want make it instantaneous. If you delete one, the count shall be decreased instantaneously.
Maybe this is too easy, but what about that:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
var lineCount = richTextBox.Lines.Count();
numberLabel.Text = lineCount.ToString();
}
Make sure you assign it to the TextChanged event.
If this is not what you need, please add some more information what you are trying to achieve.
I'm really late, but Lines is an array of string. Just get the length.
richTexBox.Lines.Length.ToString();
I have found one open source and applied it to this problem.
I have confirmed that it works well and have implemented it.
RichTextBox Colums and Row
Simple Code(
The link has a demo source.):
this.rtb.CursorPositionChanged +=
new System.EventHandler(this.rtb_CursorPositionChanged);
this.rtb.SelectionChanged +=
new System.EventHandler(this.rtb_SelectionChanged);
.
.
.
private void rtb_CursorPositionChanged(object sender, System.EventArgs e)
{
int line = rtb.CurrentLine;
int col = rtb.CurrentColumn;
int pos = rtb.CurrentPosition;
statusBar.Text = "Line " + line + ", Col " + col +
", Position " + pos;
}
private void rtb_SelectionChanged(object sender, System.EventArgs e)
{
int start = rtb.SelectionStart;
int end = rtb.SelectionEnd;
int length = rtb.SelectionLength;
statusBar.Text = "Start " + start + ", End " + end +
", Length " + length;
}
namespace Nik.UserControls
{
public class RicherTextBox2 : System.Windows.Forms.RichTextBox
{
public event EventHandler CursorPositionChanged;
protected virtual void OnCursorPositionChanged( EventArgs e )
{
if ( CursorPositionChanged != null )
CursorPositionChanged( this, e );
}
protected override void OnSelectionChanged( EventArgs e )
{
if ( SelectionLength == 0 )
OnCursorPositionChanged( e );
else
base.OnSelectionChanged( e );
}
public int CurrentColumn
{
get { return CursorPosition.Column( this, SelectionStart ); }
}
public int CurrentLine
{
get { return CursorPosition.Line( this, SelectionStart ); }
}
public int CurrentPosition
{
get { return this.SelectionStart; }
}
public int SelectionEnd
{
get { return SelectionStart + SelectionLength; }
}
}
internal class CursorPosition
{
[System.Runtime.InteropServices.DllImport("user32")]
public static extern int GetCaretPos(ref Point lpPoint);
private static int GetCorrection(RichTextBox e, int index)
{
Point pt1 = Point.Empty;
GetCaretPos(ref pt1);
Point pt2 = e.GetPositionFromCharIndex(index);
if ( pt1 != pt2 )
return 1;
else
return 0;
}
public static int Line( RichTextBox e, int index )
{
int correction = GetCorrection( e, index );
return e.GetLineFromCharIndex( index ) - correction + 1;
}
public static int Column( RichTextBox e, int index1 )
{
int correction = GetCorrection( e, index1 );
Point p = e.GetPositionFromCharIndex( index1 - correction );
if ( p.X == 1 )
return 1;
p.X = 0;
int index2 = e.GetCharIndexFromPosition( p );
int col = index1 - index2 + 1;
return col;
}
}
}
Los799
Sorry for answering a "bit" late, I saw this question just now.
But if the problem still stands, here's a simple way to count lines, just use a foreach loop:
int CountOfLines = 1;//1 because min 1 line is always in a text of a control, that has a Text property
foreach (char c in YourText)
{
if (c == '\r' | c == '\n')//these are all equal the ENTER key
{
CountOfLines++;
}
}
You can also use foreach to count characters as well, but with foreach you can choose characters, that you don't want to be counted in the count of characters. For example:
int CountOfCharacters = 0;//0 because by default there are no characters in a textbox or label.
foreach (char c in YourText)
{
if (c != '\t' & c != '\n' & c != '\r')//in this example I want to count only the characters that are not an ENTER or a TAB.
{
CountOfCharacters++;
}
}
Hope this helps you and everybody else, who's reading this, even if it's a bit late answer. :)
Instead of trying to battle with the default rich text box, why don't you try making your own control so you have full control of the text formatting?
After all, if you're developing your own text editor, it would make sense to have text stored and managed in a way that makes sense to you, the developer, instead of trying to fight with a format designed for a slightly different purpose.
Newer solution to this question for WPF applications
DOK1 is a WPF flow document
TX1 is a TextBox control
TX1.Text = DOK1.Blocks.Count.ToString();