TextBlock + RichTextBox to clipboard - c#

I'm trying to get the contents of dynamically created TextBlock and dynamically created RichTextboxes (they sit side by side) into the clipboard in c# + wpf, however, I'm not able to do so. I've search all over google to no avail, the latest code I came up with is
StringBuilder clipboard = new StringBuilder();
String rtb = scrlPanel.Children.OfType<RichTextBox>().ToString();
//List<RichTextBox> rtb = scrlPanel.Children.OfType<RichTextBox>().;
foreach(TextBlock txtb in scrlPanel.Children.OfType<TextBlock>())
{
clipboard.Append(txtb.Text + " " + "::" + Environment.NewLine + rtb.ToString() + Environment.NewLine);
}
Clipboard.SetText(clipboard.ToString());
but it doesn't work, the codes copies the TextBlocks just fine but the RichTextBoxes content display" "System.Linq.Enumerable+d__aa`1[System.Windows.Controls.RichTextBox]"
Any help or pointers is greatly appreciated.
Thanks,

You are copying the from the ToString() - method, which by default shows the typename.
You should do it like this:
StringBuilder clipboard = new StringBuilder();
List<RichTextBox> rtbs = scrlPanel.Children.OfType<RichTextBox>().ToList();
List<TextBlock> texts = scrlPanel.Children.OfType<TextBlock>().ToList();
foreach(TextBlock txtb in texts)
{
RichTextBox rtb = rtbs[texts.indexOf(txtb)];
string rtbtext = new TextRange(rtb .Document.ContentStart, rtb .Document.ContentEnd).Text;
clipboard.Append(txtb.Text + " " + "::" + Environment.NewLine + rtbtext + Environment.NewLine);
}
Clipboard.SetText(clipboard.ToString());
That is, if you just as many TextBoxes as RichTextBoxes and they have the same order.
PD: There's probably better ways of doing this, but this would be a quick fix.

Related

new line at Text Box at winform

I want to pass a string into a method in form1 class every time an event occurs in another class and then I want to print some text as multi-lined in a TextBox in the Windows Form associated with form1 class.
The thing is I'm getting all the text in that TextBox in a single line and want to print it on a new line:
private void myEvt_valueChnaged(string s)
{
textBox1.Invoke(new Action(() => textBox1.Text = s ));
}
I tried to use += and then I got the text later in the line and not below the line .
I also tried to add +="/n/r" + s and it was printed as is in the Text Box
Then I tried using += Environment.NewLine() + s and got the error: Environment.NewLine cannot be used like a method .
"\n " is good enough for a new line in c#.
String newline = "\n "
textBox1.Invoke(new Action(() => textBox1.Text = newline + s ));
You should set the Multiline property of the textbox control to True and then just call this:
textBox1.Invoke(new Action(() => textBox1.Text = String.Concat(textBox1.Text, Environment.NewLine, s)));
if you want to append the text in a new line and maintain the already present text. Otherwise you can use this to put simply the text in the new line:
textBox1.Invoke(new Action(() => textBox1.Text = String.Concat(Environment.NewLine, s)));
Note that Environment.NewLine is a property not a method.

How to pass a parameter in wpf C#

I have a code in WindowAfterLogin.xaml:
<TextBlock x:Name="In_Time" Foreground="#FF55534F"
FontSize="71.312" FontFamily="HelveticaNeueCyr"
Height="79.45" LineStackingStrategy="BlockLineHeight"
Canvas.Left="2.724" LineHeight="71.312"
TextAlignment="Center" TextWrapping="Wrap"
Canvas.Top="69.985" Width="231.581" Text="09:00" />
And then I want to change the value of that TextBlock in WindowAfterLogin.xaml.cs, so I've done this :
MainWindow objMainWindow = new MainWindow();
WindowAfterLogin objAfterLogin = new WindowAfterLogin();
objMainWindow.Show();
objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
this.Close();
But, when I press F5 button (Compile), it didn't change. Where is the problem here?
Are you creating a separate instance of WindowAfterLogin ? If you want to do that, you will have to Show() the new instance. Try this:
WindowAfterLogin objAfterLogin = new WindowAfterLogin();
objAfterLoginShow();
objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
//Close(); //Close any other window that needs to be closed..
If you wanted the In_Time.Text to change for the current instance, (assuming that has been showed or is visible currrently), you can try this perhaps in the constructor or your method that Initializes the WindowAfterLogin :
In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
As a simplification to bit answer, try to write something like this instead of your code
MainWindow objMainWindow = new MainWindow();
objMainWindow.Show();
In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
this.Close(); //Not sure if You need it. Try to comment this line to be able to notice textblock text changes
Also I think You need
DateTime.Now.ToString("hh:mm")
instead of
DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString()

Show different color strings(text) in textbox asp.net

In my asp.net website i have a textbox which provides the log information. I am using this log to display the error,success and other information. What i want to do is i want to show the result text in textbox in color according to the type of log. For eg; i want to display error as red text, success as green and so on.
I tried the following code but using this code changes the color of entire content of textbox.
/// <summary>
/// colorIndex (0 = default, 1 = red, 2 = green)
/// </summary>
/// <param name="logValue"></param>
/// <param name="colorIndex"></param>
private void writeToLog(string logValue, int colorIndex)
{
if (colorIndex == 0)
{
TextBox2.ForeColor = Color.Black;
}
else if(colorIndex == 1)
{
TextBox2.ForeColor = Color.Red;
}
else if(colorIndex == 2)
{
TextBox2.ForeColor = Color.Green;
}
TextBox2.Text = "[ " + DateTime.Now + "] " + logValue + Environment.NewLine + TextBox2.Text;
}
Actually i want the output as follows:
You can see in above output there are three different colors of text in same texbox.
This output is actually from the desktop application. And i want to show same type of output in asp.net web page? How can i do it? Please help. Thanks in advance!!
Thanks for the answers everyone! Like Knaģi said, since may client need not to edit the value in log,I thought use of WYSIWYG editors was unnecessary. So for solving my problem i used a div instead of textbox.
<div id = "log" style="border: thin solid #00CC99; width:844px; height:193px; overflow:auto;" runat= "server">
</div>
And in my code behind i filled the log as:
private void writeToLog(string logValue, int colorIndex)
{
logValue = "[ " + DateTime.Now + "] " + logValue;
if (colorIndex == 0)
{
string htmlCode = "<font style='color:black'>"+logValue+"</font><br/>";
log.InnerHtml = htmlCode + log.InnerHtml;
}
else if(colorIndex == 1)
{
string htmlCode = "<font style='color:red'>" + logValue + "</font><br/>";
log.InnerHtml = htmlCode + log.InnerHtml ;
}
else if(colorIndex == 2)
{
string htmlCode = "<font style='color:green'>" + logValue + "</font><br/>";
log.InnerHtml = htmlCode + log.InnerHtml ;
}
}
You can't use a simple TextBox. Instead you must use one of many WYSIWYG editors. There are many to choose from available with wrappers for any server side language.
But since your code does not really need the user to be able to modify this data, you should simply render HTML code with the correct styles and apply overflow-y: auto; max-height:100px CSS styles so that the scrollbar is rendered when the content is too long.
In asp.net, i think table is best option.and you can change text color using id and class for
and give there style by applying css
<table id="log">
<tr><th>Response Type</th><th>Response Time</th><th>Response Data </th></tr>
<tr class="error"><td></td><td></td><td></td></tr>
<tr class="info"><td></td><td></td><td></td></tr>
</table>
you need to add row by jQuery or Code behind
i think it is help full for you.
You can use label instead. That way it would be simple and easy to render the text with html span tag to define different styles.
if (colorIndex == 0)
{
Lable2.Text += "<span style='color:Black'>Black information...</span><br/><br/>";
}
else if(colorIndex == 1)
{
Lable2.Text += "<span style='color:Red'>Red information...</span><br/><br/>";
}
else if(colorIndex == 2)
{
Lable2.Text += "<span style='color:Green'>Green information...</span><br/><br/>";
}
You need use RichTextBox
or some WYGIWYS editor

When I save items in a listbox there are too many lines in between

I need help in a little feature in my program.
My program gets items from a website then saves it to a listbox. From that listbox it transfers it to another listbox putting the items, each in their own line.
From there I save the file in a text file.
When I check the file on my desktop there are two lines in between each item even though in my listbox it didnt show any items in between. Can you help?
Here is the code to add the items to the other listbox:
listBox6.Items.Add(listBox5.SelectedItem.ToString() + ":" + txtBox2.Text + Environment.NewLine);
Here is the code to save the listbox items:
StreamWriter Write;
SaveFileDialog Open = new SaveFileDialog();
try
{
Open.Filter = ("Text Document|*.txt|All Files|*.*");
Open.ShowDialog();
Write = new StreamWriter(Open.FileName);
for (int I = 0; I < listBox6.Items.Count; I++)
{
Write.WriteLine(Convert.ToString(listBox6.Items[I]));
}
Write.Close();
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}
Replace Write.WriteLine with Write.Write since you already added Environment.NewLine before.
Better yet remove the explicit new line:
listBox6.Items.Add(listBox5.SelectedItem.ToString() + ":" + txtBox2.Text)

How to add text in a multiline textbox?

I have to add details of my file into a multiline textbox. But all the details are getting added in a single line in the text box and not in a vertical sequence. I used Environment.NewLine and also used "\r\n", but it's not of any help. I have ticked the multiline text box in a Windows Forms form and also set it to true but to no avail.
My line of code is like this:
m_Txt.Multiline = true;
m_Txt.Text = fileInfo.m_Title + "\r\n" +
fileInfo.m_Identifier + Environment.NewLine +
fileInfo.m_TotalTime;
A cleaner answer is:
Assuming txtStatus is a textbox:
txtStatus.Multiline = True;
txtStatus.Clear();
txtStatus.Text += "Line 1" + Environment.NewLine;
txtStatus.Text += "Line 2" + Environment.NewLine;
Using the built in enumeration means cleaner code.
Shift+Enter
In the Visual Studio resource editor, you can hit "Shift + Enter"
to create a new line, as doing something like "\r\n" will get escaped
out. You will also need to increase the cell height to see both
lines as it does not auto-size.
If you're doing it programatically, append the new line to m_Txt.Lines, which is a string[].
m_Txt.Lines = new string[]{ fileInfo.m_Title, fileInfo.m_Identifier, fileInfo.m_TotalTime};
Not sure why your code would not work unless something else is going on.
I just created a WinForms project using C#, added a textbox, set it multiline and added the following code - works a charm.
textBox1.Text = "a\r\nb";
I just wrote this code, seems to be working fine.
public void setComments(String comments)
{
String[] aux;
if(comments.Contains('\n')) //Multiple lines comments
{
aux = comments.Split('\n');
for (int i = 0; i < aux.Length; i++)
this.textBoxComments.Text += aux[i] + Environment.NewLine;
}
else //One line comments
this.textBoxComments.Text = comments;
}

Categories