I am trying to simply add a webbrowser control to a window and then have it open up a page. I tried a web URL as well as a local HTML file to no avail. Here is my code:
namespace qTab1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FileStream source = new FileStream("index.html", FileMode.Open, FileAccess.Read);
webBrowser1.DocumentStream = source;
//// When the form loads, open this web page.
//webBrowser1.Navigate("www.google.com");
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
// Set text while the page has not yet loaded.
this.Text = "Navigating";
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Better use the e parameter to get the url.
// ... This makes the method more generic and reusable.
this.Text = e.Url.ToString() + " loaded";
}
}
}
This is my project at the moment:
What am I doing wrong?
The reason this happens is that when you press Debug or build your project any other way the root directory is the directory of the executable (so it would be - ./bin/Debug), not the directory of the project.
To fix this, you can do the following:
Right click the html file, click Properties and set the variable "Copy to output directory" to Copy always. That way, the html file will get copied with your executable.
Now you have to load the local file into the WebBrowser control. The following should work :
webBrowser1.Url = new Uri("file:///" + Directory.GetCurrentDirectory() + "/index.html");
Related
I am trying to update a label on my windows form with statistics from another methods execution that scrapes a webpage and creates a zip file of the links and creates a sitemap page. Preferably this button would run the scraping operations and report the statistics properly. Currently the scraping process is working fine but the label I want to update with statistics data is not changing on the button click. Here is what my code looks like now:
protected void btn_click(object sender, EventArgs e)
{
//Run scrape work
scrape_work(sender, e);
//Run statistics work
statistics(sender, e);
}
protected void scrape_work(object sender, EventArgs e)
{
//Scraping work (works fine)
}
protected void statistics(object sender, EventArgs e)
{
int count = 0;
if (scriptBox.Text != null)
{
count += 1;
}
var extra = eventsBox.Text;
var extraArray = extra.Split('\n');
foreach (var item in extraArray)
{
count += 1;
}
//scrapeNumLbl is label I want to display text on
scrapeNumLbl.Text = count.ToString();
}
Would I have to use threading for this process or is there some other way I can get this process to work? I have already tried this solution but was having the same issue where the code runs but the label does not update. Any help would be greatly appreciated, this minor thing has been bugging me for hours now.
I eventually solved this by writing the path to the zip file to a label button on the form rather than sending it right to download on the client's browser on button click. The issue was that the request was ending after the zip file was sent for download. To ensure that both methods ran at the proper time I moved the call to the scrape_work method to inside of of statistics
In order for the path to be clickable and the file to download properly I had to make the "label" in the form a LinkButton in the .aspx page
<asp:LinkButton ID="lblDownload" runat="server" CssClass="xclass" OnClick="lblDownload_Click"></asp:LinkButton>
And made the lblDownload_Click run like the following:
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=zipFiles.zip");
string folder = Server.MapPath("~/zip");
string endPath = folder + "/zipFiles.zip";
Response.TransmitFile(endPath);
Response.End();
Running it this way the page reloads with the new labels properly written and the zip file available to download as well.
ASSUMING THIS CODE IS RUNNING SYNCHRONOUSLY (you aren't threading the scraping in a call I don't see), Are you sure you are reaching the code that sets the label text? I simplified your code as below (removing the iteration over the array and just setting the label text to a stringified integer) and am not having any trouble changing the text of a label.
namespace SimpleTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
scrape_work(sender, e);
statistics(sender, e);
}
protected void scrape_work(object sender, EventArgs e)
{
//Scraping work (works fine)
}
protected void statistics(object sender, EventArgs e)
{
int count = 666;
scrapeNumLbl.Text = count.ToString();
}
}
}
Result:
I making a wpf application but in settings window, I'm changing the text of textboxes listed in view, then I click save
button to update Properties.Settings.Default. Then, I close settings window. I go back to the MainWindow. But when I shutdown the application, in the next session my values does not show up. How could I solve it?
Save Button:
private void save_Click(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.PC_Name = pcname.Text;
Properties.Settings.Default.Acc_name = name.Text;
Properties.Settings.Default.Acc_sname = sname.Text;
}
Loaded Event(Page):
private void ayarlarekrani_Loaded(object sender, RoutedEventArgs e)
{
pcname.Text = Properties.Settings.Default.PC_Name;
name.Text = Properties.Settings.Default.Acc_name;
sname.Text= Properties.Settings.Default.Acc_sname;
}
Looks like you are missing:
Properties.Settings.Default.Save();
http://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-write-user-settings-at-run-time-with-csharp
I am still only just learning C# in Visual Studio and I am trying to make a simple text encryption application. My problem at the moment is that when I use the command:
File.WriteAllText(name, inputTextBox.Text);
(Where name is the name of the file chosen in a SaveFileDialog and inputTextBox.Text is the text in a textbox on the main form)
however the file is never actually created. I even tried to build the application and run it as administrator but nothing happened.
What's even weirder is when I opened File Explorer, in the Quick Access section where it shows recent files, all the files that were supposed to be created show up there but don't exist when I click "Open File Location" and if I just try to open them, notepad just tells me the file doesn't exist.
The files are also not in my recycle bin or anything.
Here's the rest of my code in case it's something wrong with that:
public Form1()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
saveDialog.ShowDialog();
}
private void saveDialog_FileOk(object sender, CancelEventArgs e)
{
string name = saveDialog.FileName;
File.WriteAllText(name, inputTextBox.Text);
}
And in case you're wondering saveDialog is already an element in my form so there's no problem with that.
Since in your posted code the initialization of the SaveFileDialog is missing, and you say in your comment that the Debugger doesn't halt in the event body I take the long shot to assume that the event is not registered appropriately.
Try to make sure that your class (minimally) looks like the following example:
public partial class Form1 : Form
{
SaveFileDialog saveDialog;
public Form1()
{
InitializeComponent();
// create instance of SaveFileDialog
saveDialog = new SaveFileDialog();
// registration of the event
saveDialog.FileOk += SaveDialog_FileOk;
}
private void saveButton_Click(object sender, EventArgs e)
{
saveDialog.ShowDialog();
}
private void saveDialog_FileOk(object sender, CancelEventArgs e)
{
string name = saveDialog.FileName;
File.WriteAllText(name, inputTextBox.Text);
}
}
If your problem still remains, then I will remove my answer
I have a very simple program that has 4 buttons each button opens a url.
What i would like is to get each button to use Firefox as the default browser.
I know i can set it as the default browser and then it will work however if a user sets internet explorer as the defualt it will open in that instead.
These url only work in Firefox as it has certificate issues.
So essentially my question is how can i change my code to make sure that if you click on any of the 4 buttons, that it will open up in firefox and not internet explorer.
Here is my code
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=on");
BtnOff.BackColor = Color.Red;
Bnton.BackColor = Color.Chartreuse;
}
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=off");
Bnton.BackColor = Color.Red;
BtnOff.BackColor = Color.Chartreuse;
}
private void button3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/pause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnDND.BackColor = Color.Chartreuse;
BtnQue.BackColor = Color.Red;
}
private void button4_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/unpause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnQue.BackColor = Color.Chartreuse;
BtnDND.BackColor = Color.Red;
}
Thanks for taking the time to look appreciate it.
you can use
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "http://yoursite.com/dummy.htm");
you can keep the path of firefox.exe in config file instead of hard coding. Alternatively set the PATH environment variable.
Is there an easy way to show an dialog when the program is started for the first time (and only the first time), for some kind of instruction or specifying settings?
You could save it as a bool in your settings and you should check at load event of first form.
Your settings file should have a setting that I called "FirstRun" do this with following steps:
Right click your Project
Click "Properties"
Click "Settings" tabpage(probably on the left)
Add setting like I did as seen in image above
Note: The Scope can be changed to "Application", if that is your application's need, since you didn't mention in your question.
Your Settings file should look like image below:
public void Form1_Load(object sender, EventArgs e)
{
if((bool)Properties.Settings.Default["FirstRun"] == true)
{
//First application run
//Update setting
Properties.Settings.Default["FirstRun"] = false;
//Save setting
Properties.Settings.Default.Save();
//Create new instance of Dialog you want to show
FirstDialogForm fdf = new FirstDialogForm();
//Show the dialog
fdf.ShowDialog();
}
else
{
//Not first time of running application.
}
}
Note: wrote this from my phone, so I couldn't compile to test
Edit: Checked code and added image from desktop.
You can have bool value in your settings file which is a "user setting" which means you can change it to true save it for this specific user.
When your application starts just check that value. If it's false show your dialog and change it to true and it will stay true.
public void Form_Load(object sender, EventArgs e)
{
if(Settings.Default.ShowDialog)
{
Settings.Default.ShowDialog = false;
Settings.Default.Save();
// show first disalog
}
// rest of code if needed
}
Here's an MSDN link on user settings:
http://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx
Ok, so I assume you're creating WinForms application. First of all, locate the Load event in your main Form event lists (or simply double click your Form in Designer panel). The following method stub will pop up:
public void Form1_Load(object sender, EventArgs e)
{
}
And modify it like this:
public void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Your message here");
}