i'm learning programming in C# and would like to know if it is possible to output the value of a string that is 12 letters long into a table with 12 columns, each column will have one label which one letter of the string will be displayed in
Sure. Just iterate the characters and do whatever you want. And it would go a little something like this:
// create an instance of your table, whatever it may be.
// create an instance of a row
// add row to your table
var line = "hello world";
foreach (var letter in line)
{
// create an instance of a column
// add column to your row
// add label to your column
// set text property of lable = letter
}
Have a look at the String.Format method:
http://msdn.microsoft.com/en-us/library/system.string.format.aspx
Related
I am trying to read a Chinese\Japanese\Korean Word document sentence by sentence to find which sentence has track change or revision, and put these sentences which have track changes or revisions to another Word document's table.
For example, if one sentence has track change, then I will copy this sentence with format to that table's column 3 and column 4 in same row.
Then I will reject the track change in column 3 and accept track change in column 4.
The problem is I cannot get these Chinese\Japanese\Korean sentences' content but only "\r\a". So after I assign the sentence's formatted content to that table, only new line added in that table's cell, no other content.
The sample text: 노출의 안전성 이후(가 검토된 후) .
For example, here is a sentence: 노출의 안전성 이후(가 검토된 후) . In this sentence only 출 was marked with track change, I can get 출 with above code, but cannot get 노출의 안전성 이후(가 검토된 후). I want to get 노출의 안전성 이후(가 검토된 후), because in this sentence, one character 출 is in track change, not just 출.
You can add or delete the part of sample text with track change open and try it with below code:
int sentenceCount = doc1.Sentences.Count;
int i = 0;
for (int s = 1; s <= sentenceCount; ++s)
{
i++;
if (doc1.Sentences[s].FormattedText.Revisions.Count > 0)
{
if (i <= tableRowCount)
{
doc2.Activate();
table.Cell(i, 3).Range.FormattedText = doc1.Sentences[s].FormattedText;
table.Cell(i, 4).Range.FormattedText = doc1.Sentences[s].FormattedText;
table.Cell(i, 3).Range.Revisions.RejectAll();
table.Cell(i, 4).Range.Revisions.AcceptAll();
//string sss = doc2.Sentences[s].Text;
}
}
}
doc2.Save();
Could you please take a look and tell me why or any other solution if have? Thanks in advance.
I am trying to identify specific value under corresponding column in a word table using c#. Tried:
1- If table index is known - get table range as text and iterate over it to reach certain row and column.
Problem is- Table comes under certain heading anywhere in the document. Index is variable, so can't rely on index.
As Cindy has pointed out, the question in a current form is too broad. I'm not sure how you're reading your documents. Also, I'm not sure what you mean by "certain heading", do you know what text will be before the targeted table?
Nevertheless, you could check out the following sample, the code uses GemBox.Document:
string cellValue = null;
string headingText = "My Heading Text";
int rowIndex = 0;
int columnIndex = 0;
// Load Word document.
DocumentModel document = DocumentModel.Load("Sample.docx");
// Iterate through all paragraphs.
foreach (Paragraph paragraph in document.GetChildElements(true, ElementType.Paragraph))
{
// Check that paragraph is of heading style.
ParagraphStyle style = paragraph.ParagraphFormat.Style;
if (style == null || !style.Name.Contains("heading"))
continue;
// Check that heading paragraph contains our text.
if (!paragraph.Content.ToString().Contains(headingText))
continue;
// Get first table after the heading paragraph.
Table table = new ContentRange(paragraph.Content.End, document.Content.End)
.GetChildElements(ElementType.Table)
.Cast<Table>()
.First();
// Get cell value.
cellValue = table.Rows[rowIndex].Cells[columnIndex].Content.ToString();
break;
}
Console.WriteLine(cellValue);
I am trying to pick out specific letters/numbers from a text box, because each means something. After that I am trying to display in a label what it means.
So if I have a number AB-123456, I need to first pick out AB something like:
If (textBox.Text.Substring(0,2) == "AB") {
//Display to a label
}
First off, this doesn't work and I also tried substring(0,1) but also was receiving errors when I used my clear button to clear the text box.
After that I still need to pull the rest of the numbers. The next one I need to pull and define is 123, then 4 by itself, 5 by itself, and six by itself.
How do I go about pulling each of these individually if substring isnt working?
Try this:
if (textBox.Text.StartsWith("AB"))
{
//Display to a label
}
Use this if you don't want to have to check the Length of the text first. Also, you can include a StringComparison argument if you want to ignore case.
string input = textBox.Text;
// check the length before substring
If (input.Length >= 2 && input.Substring(0,2) == "AB") {
//Display to a label
}
or use regex:
string txt="AB-1234562323";
string re="AB-(\\d+)"; // Integer Number 1
Regex r = new Regex(re,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)// match found
{
// get the number
String number=m.Groups[1].ToString();
}
I have a label that I want to only copy the first character. The label reads 1000. I have my copy button set up like this
var textCopy = label.Text;
Clipboard.SetText(textCopy);
Can I add something to the label.Text part that only allows the first character to be copied?
string is an array of characters, so just get the first index of the string like this:
Clipboard.SetText(textCopy[0]);
Use Substring:
var textCopy = label.Text;
Clipboard.SetText(textCopy.Substring(0,1));
msdn
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();