I'm looking for a control that be able to show HTML code like shown is the following image:
I just need to show the HTML code loaded and display it without any edition.
I do not find it anywhere.
Any help?
Based on the info you've provided;
Create a new Form
Add a Close button to it
Add a TextBox and set it to ReadOnly
Set the .Text property in the TextBox to the HTML to show.
If you do want syntax highlighting, check for open source projects on GitHub or articles on codeproject.com
Assuming you have a WinForm and a text box called txHTML to display the text, and a textbox called txUrl to provide the url you want the html from. Put this on a button click event to fill your txHTML with what you desired.
protected void Get_Click(object sender, EventArgs e){
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(txUrl.Text);
// get the response
HttpWebResponse WebResp = (HttpWebResponse)wr.GetResponse();
string res = "";
using (StreamReader sr = new StreamReader(WebResp.GetResponseStream()))
{
res = sr.ReadToEnd();
sr.Close();
}
WebResp.Close();
txHTML.Text = res;
}
Related
I have a custom RichTextBox that derives from the RichTextBox base class. Its purpose is to display formatted text. However, any Rtf loaded is displayed as simple text without any formatting: font, font-size, font-style etc.
I have tried the following code to load the Rtf: (Note: rtbEx is the extended richtextbox control; RTF is a string containing the Rtf)
Using a file stream:
FileStream tempFile = File.Open(#"C:\RTF.rtf", FileMode.Open);
tempFile.Position = 0;
rtbEx.LoadFile(tempFile, RichTextBoxStreamType.RichText);
tempFile.Close();
Loading from the specified path:
rtbEx.LoadFile(#"C:\Users\Wilbur Omae\Desktop\RTF.rtf", RichTextBoxStreamType.RichText);
Directly setting the Rtf:
rtbEx.Rtf = RTF;
On checking the Rtf of the rtbEx, it seems to be perfect Rtf, yet it is displayed as plain text.
What could be the issue?
Update 1:
The custom RichTextBox is a control within a custom Form which it to be displayed as a TabPage.
you can use Clipboard in this case :
Clipboard.SetText(RichTextBox1.Rtf, TextDataFormat.Rtf);
and paste it
RichTextBox1.Text= Clipboard.GetText()
It works for me .. try it
As a workaround, I ensured the Rtf was set only when the form had been shown by trapping the Form.Shown event as shown below:
public class SermonReader : Form
{
public RichTextBoxEx rtbEx= new RichTextBoxEx();
private string RTF = "";
public SermonReader(string rtf)
{
RTF = rtf;
Shown += new EventHandler(ehFormShown);
FormBorderStyle = FormBorderStyle.None;
TopLevel = false;
Controls.Add(rtbEx);
rtbEx.Dock = DockStyle.Fill;
}
private void ehFormShown(object sender, EventArgs e)
{
rtbEx.Rtf = RTF;
}
}
I don't know why the issue is this complicated but I hope this helps.
Any other solution? Feel free to comment or answer.
I had the same problem with a richtextbox within a winformscontrol
within a dialog (MFC), the rtb should be filled with rtf, but
after setting RichTextBox.Rtf, loading from file or Clipboard it all was unformatted.
I could solved it by using Postmessage in OnInitDialog with UpdataData(FALSE) (Sets RichTextBox.Rtf again)
in the handler. seems as if creation yet was not complete ..
I am trying to click on an image in a datagridview and then write its image/file name into a textbox so I can access this from elsewhere.
First I try just a small app to make sure I can make it all work. A Dialog contains the dataviewgrid and I put a bitmap into it as below:
public ChooseFormat()
{
InitializeComponent();
dataGridView1[0,0].Value = new Bitmap(#"C:\a\eggs\grid_app\grid_app\bin\Debug\graphics\1L5HQ60.bmp");
}
Now I click on the image but all the things I have tried I cannot get hold of the file name. The closest I get is below but this returns "System.Drawing.Bitmap" and not the file name. I am sure this must just be a tweak here to make it work but I have tried teh few things I know and nothing is working.
void DataGridView1CellContentClick(object sender, DataGridViewCellEventArgs e)
{
txtbx_choice.Text = dataGridView1[0,0].Value.ToString();
}
Drilling into the cells's data in the debugger doesn't bring up any info on the source of it. Maybe I have overlooked something..
One simple solution is to store the filename in the cell's Tag property:
string fileName = #"C:\a\eggs\grid_app\grid_app\bin\Debug\graphics\1L5HQ60.bmp";
dataGridView1[0,0].Value = new Bitmap(fileName );
dataGridView1[0,0].Tag = fileName ;
Now you can always access it:
string displayedFile = dataGridView1[0, someRow].Tag.ToString();
I have placed a Picture Box on the same form and this how I am displaying the ImageColumn's data (an Image ) in picture box
pictureBox1.Image = (Image)dataGridView1[0, 0].Value;
WebBrowser Control seems to re-arrange attributes within HTML tags when setting webBrowser1.DocumentText..
I'm wondering if there is some kind of render mode or Document Encoding that I am missing. My problem can be seen by simply adding a RichTextBoxControl (txt_htmlBody) and a WebBrowser control (webBrowser1) to a windows form.
Add webBrowser1 WebBrowser Control, and add an event handler to; webBrowser1_DocumentCompleted
I used this to add my mouse click event to the web browser control.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Attach an event to handle mouse clicks on the web browser
this.webBrowser1.Document.Body.MouseDown += new HtmlElementEventHandler(Body_MouseDown);
}
In the mouse click event, we get which element was clicked on like so;
private void Body_MouseDown(Object sender, HtmlElementEventArgs e)
{
// Get the clicked HTML element
HtmlElement elem = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);
if (elem != null)
{
highLightElement(elem);
}
}
private void highLightElement(HtmlElement elem)
{
int len = this.txt_htmlBody.TextLength;
int index = 0;
string textToSearch = this.txt_htmlBody.Text.ToLower(); // convert everything in the text box to lower so we know we dont have a case sensitive issues
string textToFind = elem.OuterHtml.ToLower();
int lastIndex = textToSearch.LastIndexOf(textToFind);
// We cant find the text, because webbrowser control has re-arranged attributes in the <img> tag
// Whats rendered by web browser: "<img border=0 alt=\"\" src=\"images/promo-green2_01_04.jpg\" width=393 height=30>"
// What was passed to web browser from textbox: <img src="images/PROMO-GREEN2_01_04.jpg" width="393" height="30" border="0" alt=""/>
// As you can see, I will never be able to find my data in the source because the webBrowser has changed it
}
Add txt_htmlBody RichTextBox to the form, and set a TextChanged of the RichTextBox event to set the WebBrowser1.DocumentText as the RichTextBox (txt_htmlBody) text changed.
private void txt_htmlBody_TextChanged(object sender, EventArgs e)
{
try
{
webBrowser1.DocumentText = txt_htmlBody.Text.Replace("\n", String.Empty);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
When you run your program, copy the below example HTML into txt_htmlBody, and click the Image on the right and debug highLightElement. You will see by my coments why I can not find the specified text in my search string, because WebBrowser control re-arranges the attributes.
<img src="images/PROMO-GREEN2_01_04.jpg" width="393" height="30" border="0" alt=""/>
Does anyone know how to make WebBrowser control render my HTML as-is?
Thank you for your time.
You cannot expect the processed HTML to be 1:1 the same as the original source, when you obtain it back via element.OuterHtml. It's almost never the same, regardless of the rendering mode.
However, despite the attributes may have got rearranged, their names and values are still the same, so you'd just need to improve your search logic (e.g., by walking the DOM three or simply enumerating elements via HtmlDocument.All and checking their attributes via HtmlElement.GetAttribute).
I have a C# Windows form which based on user input will fetch multiple download links. Now I am facing difficulties in displaying this links to user so that they can click on their desired link to download the files.
I can display everything using MessageBox but could not make links in MessageBox and as the download link is quite long it is not user friendly.
I tried LinkLabel following example from http://msdn.microsoft.com/en-us/library/aa288420(v=vs.71).aspx. This can work but only for 1 link.
Any idea how I can do this for multiple links or is there any other method?
Create your own form to display message to user. Also, use TableLayoutPanel and LinkLabel to display multiple links in the created custom message form like below.
string[] links = new string[10];
TableLayoutPanel panel = new TableLayoutPanel();
panel.RowCount = links.Length;
panel.ColumnCount = 1;
int currentRow = 0;
foreach (var link in links)
{
LinkLabel linkLabel = new LinkLabel();
linkLabel.Text = "Click here to get more info.";
linkLabel.Links.Add(6, 4, link);
linkLabel.OnLinkClicked += OnLinkClicked;
panel.Controls.Add(linkLabel, 0, currentRow++);
}
this.Controls.Add(panel);
The event handler looks like below,
void OnLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(e.Link);
}
Refer the example code in this msdn link. http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel.link.aspx
Try this:
Process.Start(e.Link.LinkData.ToString());
I have an app where the user logins to a list of textboxs where the users can write in them save the data then close the app and when they open it the data is still in the textboxes.
I have been researching .txt and .xml to see which is the best format to use. I have also researched XML Serialization and what code goes in the .xml file but I'm a bit lost with do I have to change the name of the textboxes so that the data loads in the right box? Theres about 15 textboxes on the page itselfs.
I have added using System.Xml.Serialization; to my form.
Also when the user logins in it open an existing form and when the user logouts it just closes the form.
I'm a bit confused on how to load the page with all the data showing, saving the data(Iv created a save button for the textbox page) and also reading the file isn't reading and loading the same?
I'm using visual studio 2012 Winforms c#
You could use the attributes for the XML and name it from the user login.
static public void CreateFile(string username)
{
XmlWriter xmlW = XmlWriter.Create(username + ".xml");
xmlW.WriteStartDocument();
xmlW.WriteStartElement("Listofboxs");
//add the box following this canvas
xmlW.WriteStartElement("box");
xmlW.WriteAttributeString("nameofbox", "exampleName");
xmlW.WriteAttributeString("valueofbox", "exampleValue");
xmlW.WriteEndElement();
//
xmlW.WriteEndElement();
xmlW.WriteEndDocument();
xmlW.Close();
}
this will allow you to create the first file with the username.
Second, to display these informations when reloading your application, here's some code:
static public Dictionary<string, string> getBoxValue(string username)
{
Dictionary<string, string> listofbox = new Dictionary<string, string>();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(#"./" + username + ".xml");
XmlNode root = xmldoc.DocumentElement;
foreach (XmlNode box in root)
{
listofbox.Add(box.Attributes[0].Value.ToString(),box.Attributes[1].Value.ToString());
}
return listofbox;
}
For each node, the dictionnary will add a pair of string the name of the box and its value.
You can use it to fill your boxes.
I know this code may be a little unefficiency (should use "using" and such) but I hope it could help you.
I can't understanding ur problem completely. show the coding of your program what you want.
just try when your page is loading clear all textbox data.
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
To read and write the xml-file you can add a dataset to your project, where you define two columns. One column for the name of the textbox and another one for the value. On the dataset you can use the Methods WriteXml and ReadXml to read and write.
To load the xml at startup you have to subscribe for the load-event of the form. And in the Formclosing-event you can write your data.
public Form1()
{
this.Load += OnLoad();
this.FormClosing += OnFormClosing();
}
private void OnLoad(object sender, EventArgs e)
{
// Read Data from Xml with the dataset (dataset.Readxml...)
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
// Write the Data from the textboxes into the xml (dataset.writexml...)
}
To set the values of the textboxes in the load-part you can use the following code:
TextBox tb = this.Controls.Find("buttonName", true);
if(tb != null)
// set the value for the tb