In my Application , I am facing complication for fetching the exact Text Location or Index . My Text will be like
"AAAAAAAA" + (......a long space.......) + "BBBBBBBB"
I could able to get these values as array from my DatagridViewTextBoxCell . But i couldn't able to get their location in the cell for a highlight purpose. Please help me to solve this.
My Code to fetch value under CellPainting Event is
DataGridViewTextBoxCell currentCell =
(DataGridViewTextBoxCell)grid.Rows[e.RowIndex].Cells[e.ColumnIndex]
Thanks in advance
I'm not sure if your problem is how to search or how to paint your cell. Here an example how to search for a text within a string or use a regular expression to find a text.
static void Main(string[] args)
{
string someText = "AAAAAAAAAA sjkvsvkjq BBBBBBBBBBB";
// This is how to find a Text within a String:
string searchText = "BBBBBBBBBBB";
int indexOfString = someText.IndexOf(searchText);
Console.WriteLine("Text found at index " + indexOfString);
// This is how to find text within a pattern:
Regex regularExpression = new Regex("AAAAAAAAAA (.*) BBBBBBBBBBB");
string textBetweenBraces = regularExpression.Match(someText).Groups[1].ToString();
Console.WriteLine("Pattern matched. Text in Pattern: " + textBetweenBraces);
// Paint your cell:
if (regularExpression.IsMatch(someText))
{
DataGridViewTextBoxCell cell = null; // Select your cell
cell.Style.BackColor = Color.Red;
}
}
My have below process 1 .txt file
firstname:lastname
sumon:kamal
abdul:halim
kamrul:khan
rafiq: akbor
How I am show 1st name textbox1 with 2nd textbox show last name?
Example for clear
textbox1 show sumon & textbox2 show kamal
I am trying more road but still not getting any road for this.
I am trying using streamReader for show textbox data but full line only coming.
How reading 1 line with 2 textbox both name?
If I take your question right, you want something like this.
public partial class Main : Form
{
private const string PATH = "C:\\person.txt";
public Main()
{
InitializeComponent();
// read lines from file at specified path
var lines = File.ReadLines(PATH);
// take first line from aquired collection
var line = lines.FirstOrDefault();
if (line == null)
return;
// split string to first and last names
var parts = line.Split(':');
if (parts.Length != 2)
return;
// send them to textBox.Text property
textBox1.Text = parts[0];
textBox2.Text = parts[1];
}
}
Don't forget drop textBox1 and textBox2 to your form.
Iterate over lines if you need something else.
In my application i have to highlight some text (incorrect words) which are returned by an API to show in a multiline textbox.
How to highlight specific words in the string returned by an API.
My code is
ServiceReference1.GetTextSoapClient c = new GetTextSoapClient();
string text = c.GetTextFromImage(#"D:\Files\OCR\" + FileUpload1.FileName);
txtContent.Text = text;
List<string> list_Words = GetWords(text);
How to highlight specific words in text.
if you just want to select a word in a multiline text box you must first find the word :
int start_index = textBox1.Text.IndexOf("word");
then highlight it :
textBox1.Focus();
textBox1.Select(start_index, word lenght);
but if you want to select multiple words and change the color , highlight background etc.... you must use rich text box
I have a program that reads through a Microsoft Word 2010 document and puts all text read from the first column of every table into a datatable. However, the resulting text also includes special formatting characters (that are usually invisible in the original Word document).
Is there a way that I can take the string of text that I've read and strip all the formatting characters from it?
The program is pretty simple, and uses the Microsoft.Office.Interop.Word assemblies. Here is the main loop where I'm grabbing the text from the document:
// Loop through each table in the document,
// grab only text from cells in the first column
// in each table.
foreach (Table tb in docs.Tables)
{
for (int row = 1; row <= tb.Rows.Count; row++)
{
var cell = tb.Cell(row, 1);
var listNumber = cell.Range.ListFormat.ListString;
var text = listNumber + " " + cell.Range.Text;
dt.Rows.Add(text);
}
}
EDIT: Here is what the text ("1. Introduction") looks like in the Word document:
This is what it looks like before being put into my datatable:
And this is what it looks like when put into the datatable:
So, I'm trying to figure out a simple way to get rid of the control characters that seem to be appearing (\r, \a, \n, etc).
EDIT: Here is the code I'm trying to use. I created a new method to convert the string:
private string ConvertToText(string rtf)
{
using (RichTextBox rtb = new RichTextBox())
{
rtb.Rtf = rtf;
return rtb.Text;
}
}
When I run the program, it bombs with the following error:
The variable rtf, at this point, looks like this:
RESOLUTION: I trimmed the unneeded characters before writing them to the datatable.
// Loop through each table in the document,
// grab only text from cells in the first column
// in each table.
foreach (Table tb in docs.Tables)
{
for (int row = 1; row <= tb.Rows.Count; row++)
{
var charsToTrim = new[] { '\r', '\a', ' ' };
var cell = tb.Cell(row, 1);
var listNumber = cell.Range.ListFormat.ListString;
var text = listNumber + " " + cell.Range.Text;
text = text.TrimEnd(charsToTrim);
dt.Rows.Add(text);
}
}
I don't know exactly what formatting you're trying to remove, but you could try something like:
text = text.Where(c => !Char.IsControl(c)).ToString();
That should strip the non-printing characters out.
Al alternative can be that You need to add a rich textbox in your form (you can keep it hidden if you don't want to show it) and when you have read all your data just assign it to the richtextbox. Like
//rtfText is rich text
//rtBox is rich text box
rtBox.Rtf = rtfText;
//get simple text here.
string plainText = rtBox.Text;
Why dont you give this a try:
using System;
using System.Text.RegularExpressions;
public class Example
{
static string CleanInput(string strIn)
{
// Replace invalid characters with empty strings.
try {
return Regex.Replace(strIn, #"[^\w\.#-]", "",
RegexOptions.None, TimeSpan.FromSeconds(1.5));
}
// If we timeout when replacing invalid characters,
// we should return Empty.
catch (RegexMatchTimeoutException) {
return String.Empty;
}
}
}
Here's a link for it as well.
http://msdn.microsoft.com/en-us/library/844skk0h.aspx
Totally different approach would be to look at the Open Office XML SDK.
This example should get you started.
Hello so I managed to get the row where my mouse highlighted it. However I don't know how to access its content.
for example: I highlighted row 25 I can get that it highlighted row 25 howver I don't know how to access its content and put it all in a textbox. anyone?
you can use the value property to access the content of the cell as follows
// the content of the cell
var x = dataGridView1.Rows[0].Cells[0].Value;
// this gets the content of the first selected cell
dataGridView1.SelectedCells[0].Value;
// you can set the cells the user is able to select using this
dataGridView1.SelectionMode= SelectionMode. //intellisense gives you some options here
To do exactly what you are asking something like this should suffice
System.Text.StringBuilder t = new System.Text.StringBuilder();
String delimiter = ",";
foreach(DataGridViewCell c in dataGridView1.SelectedRows[0].Cells)
{
t.Append(c.Value);
t.Append(delimiter);
}
textBox1.Text = t.ToString();