In my C# app I get an xml from a server that contains some replies like in a forum thread (with elements like author, time, body, title, whatever).
When I get this xml, I create a new form in which i want to display these replies, and a little text box with an "add reply" button. I'd also like some edit buttons on perhaps my own replies in the reply list displayed in the form.
The simplest way that came to my mind to display the replies is to put a web browser control in the form, generate a full html page in a string from the xml, and throw it in that web browser control. And under it i can put the text box with the add reply button.
Everything is ok, except that i have no idea of how i could implement the edit function on my own replies (i mean i could add a link in there... but link to what)
I would like to know if there is a way to get that edit event from the web browser control (my guess is i can't) or another (maybe simple/easy) idea of displaying the replies in a winform using other controls
Yes, that's possible, you want to turn "design mode" on for the document. Add a reference to Microsoft.mshtml. Start a new Windows Forms project and drop a WB and a button on the form. Make the code look similar to this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
webBrowser1.DocumentText = "<html><body><textarea rows='15' cols='92' name='post-text' id='wmd-input'></textarea></body></html>";
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
button1.Click += button1_Click;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
mshtml.IHTMLDocument2 doc = webBrowser1.ActiveXInstance as mshtml.IHTMLDocument2;
doc.designMode = "On";
}
private void button1_Click(object sender, EventArgs e) {
var html = webBrowser1.Document.Body.All["post-text"].InnerHtml;
// do something with that
//...
}
}
Related
I'm writing a program in c# (visual studio, windows forms) which involves the user selecting a name from a combo box, and then clicking a button which brings them to a quiz on another form. I want a text file to be created at the end of the quiz showing the name selected and the quiz results, followed by a "~" symbol.
I honestly don't know where to start. I'm using stream reader.
This is the code I used for the combo box and the button that sends you to the next form
private void quizSelectPupilCB_SelectedIndexChanged(object sender, EventArgs e)
{
selectedClass = quizSelectPupilCB.SelectedItem.ToString();
}
private void quizStartBTN_Click(object sender, EventArgs e)
{
Quiz2 formQuiz2 = new Quiz2();
formQuiz2.Show();
this.Hide();
}
How do I use this data on another form?
I don't even know where to start. I tried looking up things like "how to access data from one form and put it in another c#" and similar things but everything I found was either not what I was looking for, or worded in a really confusing way.
As a variant you can save selectedItem to String/StringCollection, then to app settings(Settings.Default). And on another form get this value back.
I have main Form with DataGridView. I'm opening new Form, filling text fields, and pressing button to close the window. Once the button is clicked I also want to send data from text fields to DataGridView.
I'm currently experimenting on one textbox.
Form1:
public String SetLastDataGridViewFirstName
{
set { dgvDisplay.Rows[dgvDisplay.RowCount].Cells[0].Value = value; }
}
Form2:
private void btnAdd_Click(object sender, EventArgs e)
{
MainMenu.SetLastDataGridViewFirstName = txbAddFirstName.Text;
this.Close();
}
But I'm unable to access setter. Its obvious that this implementation won't work (just showing example what I'm thinking about), but I'm unable to find working solution. All tutorials show how to send data to new Form, not already existing.
I'm creating a native C# application and I need to do a simple thing:
Once the user clicks some certain button, another .cs file is opened (with its own design, code and stuff). If it is possible, I would like to know how to close the current form at the same time.
EDIT: what I exactly need:
namespace Mokesciai
{
public partial class Mokesciai : Form
{
public Mokesciai()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//write code here to open another page called "NewPage.cs" with its own subfiles "NewPage.Designer.cs" and
//"NewPage.resx", as shown in the solution explorer
}
}
}
The application is C# Windows application
EDIT2: what I want in the graphical way: http://sdrv.ms/JXKVEL
By clicking "Click me" I want to open the new form
private void button1_Click(object sender, EventArgs e)
{
YourSecondForm objForm=new YourSecondForm();
objForm.Show();
this.Close();
}
Assuming YourSecondForm is the name of your another form which you want to display on the button click event.
That is a form.
You can create a new instance of the form class, then call Show().
As far as i understand from your question, may be you want to do this. You can use Process.Start() to start any other application from your native app.
using System.Diagnostics;
string path=#"path to the app"
Process.Start(path);
OR
Create a new form place a multi line text box, then read the file using StreamReader & fill its result on the text box. For more information on how to use Stream Reader Check out this or this
Have a unique customer request which Im unsure how to tackle.
The customer has a webpage form with a browse button to select a file. When the browse button is clicked, instead of showing the local files, they want to pop-up a window with a textbox to enter a code. This code is then used to select a file from a local folder containing 1000 files each with their own code. They want to prevent the user from viewing the other files in that folder.
I did write a custom Windows form to mimic the webpage form but they already have the webpage online and would like to reuse it.
Any ideas how to intercept the browse button? I can use a C# Application with the web browser component, but can that intercept the browse button?
The only option that I can see working is using a C# Application with the web browser component. You can then use WebBrowser.ObjectForScripting to provide a method that can be called to trigger your custom picker window through Javscript, e.g:
window.external.ShowPickerWindow();
You then have two options:
Interrogate the DOM of the page once it's loaded and replace the button with one that triggers your picker window.
Have the customer change their page so it checks for the existance of a window.external.ShowPickerWindow method and basically does option (1) for you.
You can then have a method, perhaps called window.external.GetPickedCode() to pull the code out in the page.
Rob kinder steered me along the correct thinking track by saying "replace the button" which has lead me to a solution which works beautifully!
In short, I hide the browse button, insert a new button next to it that when clicked, opens a new window with a textbox. This textbox then sets a string value in the parent form which is used onSubmit to attach the file.
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement btnBrowse = wb.Document.GetElementById("fiPhoto");
if (btnBrowse != null)
{
HtmlElement newbtn = wb.Document.CreateElement("input");
newbtn.SetAttribute("id", "btnLoad");
newbtn.SetAttribute("type", "button");
newbtn.SetAttribute("value", "Load");
newbtn.Click += new HtmlElementEventHandler(newbtn_Click);
btnBrowse.Parent.AppendChild(newbtn);
btnBrowse.Style = "display:none";
}
HtmlElementCollection forms = wb.Document.Forms;
if (forms.Count > 0)
{
HtmlElement form = wb.Document.Forms[0];
form.AttachEventHandler("onsubmit", delegate(object o, EventArgs arg)
{
FormToMultipartPostData postData = new FormToMultipartPostData(wb, form);
postData.AddFile("photo", photo);
postData.Submit();
});
}
}
private void newbtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.ShowDialog();
}
FormToMultipartPostData is too big to post in here but it basically manually constructs the Content-Disposition to be posted
Don't show the actual file browser, imitate one which is showing only that one file in in.
Or since you know the file path when correct code is entered copy the file to temp folder you created and open file browser to browse that folder and it will be contain only that file.
I have seen a lot of posts regarding this particular subject on SO as well as on the web in general and most if not all code is as seen below
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
webBrowser1.Navigate(new Uri("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onsubmit.htm"));
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
mshtml.HTMLDocument htmlDoc = null;
htmlDoc = (mshtml.HTMLDocument) this.webBrowser1.Document;
if (webBrowser1.Document != null)
{
foreach (mshtml.HTMLFormElement form in htmlDoc.forms)
{
form.submit();
break;
}
}
}
The code has no errors whatsoever but for the life its not submitting. The sample page that I am using has simple button, what it does, it alerts the selection of the radio button and then submits the form. For some strange reason when the form is submitted via code using the WebBrowser control, the form is submitted but the alert never shows up.
I am not sure what I am doing wrong here. Any help on this would be appreciated.
Would performing a click on the button do what you need it to do? You will need to add a COM reference to the Microsoft HTML Object Library (which you may already have). For example, if you load up google into the webbrowser control, this code will place "hello world" into the search box and perform the search:
mshtml.IHTMLDocument2 doc = ((mshtml.HTMLDocumentClass)webBrowser1.Document);
((mshtml.IHTMLElement)doc.all.item("q")).setAttribute("value", "hello world");
MessageBox.Show("Clicking I'm feeling lucky button");
((mshtml.HTMLInputElement)doc.all.item("btnI")).click();
Edit: I updated the code for the components that the WPF WebBrowser control uses. Also note that this sometimes throws a script error from google, but that appears to be a timing issue based on some of the ajax calls google has on the home page.
To fix your problem you need to replace line:
form.submit();
With following code:
var children = form as IEnumerable;
var inputs = children.OfType<mshtml.HTMLInputElement>();
var submitButton = inputs.First(i => i.type == "submit");
submitButton.click();
This will show alert about user selection and submit form.
I've got a more dirty one-liner working by injecting a JavaScript to submit the form
_webBrowser.InvokeScript("eval", new object[] { "document.getElementById('formName').submit()" });
That's been working for me when interacting with a site using a lot of JavaScript and button beyond the form.