How to close Uri file after editing - c#

I am using webBrowser in Windows Forms to edit a text file.
I can save the file via the dialog, but I don't know how to tell the webBrowser control to unload the document?
So that if I press button again it will open the saved file again.
private void EditDates_Click(object sender, EventArgs e)
{
if (EditDates.Text.StartsWith("Close"))
{
webBrowser1.ShowSaveAsDialog();
EditDates.Text = "Edit Dates";
webBrowser1.Document.CLOSE????
}
else
{
EditDates.Text = "Close Dates";
webBrowser1.Url = new Uri(#"H:\EOD\ProductionDates.txt");
webBrowser1.Document.ExecCommand("EditMode", false, null);
}
}

if (EditDates.Text.StartsWith("Close"))
{
webBrowser1.ShowSaveAsDialog();
EditDates.Text = "Edit Dates";
webBrowser1.Url = new Uri("about:blank");
}

Related

Old form still display winforms c#

I implement a form for handle excel file when click button "Start".
Event click Start button:
private void btnImport_Click(object sender, EventArgs e)
{
showFormSelectLanguage();
if (CheckSheetFile() == true) {
using (WaitingForm frm = new WaitingForm(handleExcel))
{
frm.ShowDialog(this);
}
var dialogMessage = new DialogMessage();
dialogMessage.ShowDialog(this);
} else
{
ShowDialogNotFoundSheet();
}
}
showFormSelectLanguage method display dialog for select language:
private void showFormSelectLanguage()
{
var formSelectLanguage = new FormSelectLanguage();
formSelectLanguage.ShowDialog(this);
}
ShowDialogNotFoundSheet function for check sheet excel exist:
private void ShowDialogNotFoundSheet()
{
var dialogNotFoundSheet = new DialogNotFoundSheet();
dialogNotFoundSheet.setTextContent("Not found sheet");
dialogNotFoundSheet.ShowDialog(this);
}
Event click confirm select language button at Select language form:
private void btnConfirmLanguage_Click(object sender, EventArgs e)
{
//close dialog
this.Close();
}
Event click Close button for close DialogNotFoundSheet form:
private void btnCloseDialogNotFoundSheet_Click(object sender, EventArgs e)
{
this.Close();
}
CheckSheetFile method:
private bool CheckSheetFile()
{
var isCorrectFile = false;
try
{
xlWorkBook = xlApp.Workbooks.Open(txtFilePath.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
var xlWorkBook1 = xlWorkBook.Sheets["SheetName"];
isCorrectFile = true;
}
catch (Exception e)
{
return false;
}
return isCorrectFile;
}
Issue:
When I click Close button at DialogNotFoundSheet form. Then FormSelectLanguage from still display. It repeats. How can resolve it?
Expected 2 forms can close
Thanks!
Update:
All References btnImport_Click:
UI:
I don't exactly know what you did with btnImport_Click, but if your purpose is to disable the function of a button at a time and to enable it at another time, actually you don't have to register or unregister the click event, you can simply set button's Enabled propety.
//btnImport.Click += btnImport_Click;
btnImport.Enabled = true;
//btnImport.Click -= btnImport_Click;
btnImport.Enabled = false;
My guess of the reason of this loop is that you have called += btnImport_Click many times, but -= btnImport_Click is never (or less) run.
For instance if you do:
btnImport.Click += btnImport_Click;
btnImport.Click += btnImport_Click;
Each time btnImport is clicked, btnImport_Click will get invoked twice.

Click on "X" button to save file

I am trying to make WTF application were user clicks on "X" button to make save file, now this can be done by Message.Show or to ask directly to save. I have created one code already but when user click on save or cancel, error window shows up thatprogram start working and it wants to send information to Microsoft.
private void Window_Closing(object sender, CancelEventArgs e)
{
Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
saveDlg.DefaultExt = ".rtf";
saveDlg.Filter = "RTF Documents (.rtf)|*rtf";
Nullable<bool> rezultat = saveDlg.ShowDialog();
if (rezultat == true)
{
string filename = saveDlg.FileName;
System.IO.File.Create(filename);
}
{
this.Close();
}
}
I think that you might have intended there to be an else
else
{
this.Close();
}
Secondly, calling this.Close(); inside the Window_Closing event is just asking for a Stack Overflow exception.
You don't need to close the window again. It is already closing.
modify with your code with proper else statement with your if condition
private void Window_Closing(object sender, CancelEventArgs e)
{
Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
saveDlg.DefaultExt = ".rtf";
saveDlg.Filter = "RTF Documents (.rtf)|*rtf";
Nullable<bool> rezultat = saveDlg.ShowDialog();
if (rezultat == true)
{
string filename = saveDlg.FileName;
System.IO.File.Create(filename);
}
else
{
this.Close();
}
}

How can i keep rewriting to text file to add text?

In top of form1 i did:
StreamWriter w;
In the constructor i did:
w = new StreamWriter(#"d:\test.txt");
Now i have a checkbox that enable a button:
if (checkBox2.Checked)
{
if (marklightnings == true)
{
myTrackPanelss1.panel1.Enabled = true;
myTrackPanelss1.panel1.Visible = true;
button1.Enabled = true;
}
}
Now in the button1 click event i write to the text file:
private void button1_Click_1(object sender, EventArgs e)
{
w.WriteLine("test");
w.Close();
}
What i want to do is that each time i check the checkbox it will open the text file for writing but not to create a new text file but to use the same exist text file and just add a text to it.
Its not a settings file but it is a text file i need later to read the text from it.
What i need is that when the button1 is enabled i will be able to click on it ant it will add text to the text file each click.
So i did w.Close();
But then it cant be add more text to be written. If i will make new instance for w it will create new empty text file.
Try:
w = new StreamWriter(#"d:\test.txt", true);
Or:
private void button1_Click_1(object sender, EventArgs e)
{
using (StreamWriter w = File.AppendText(#"d:\test.txt"))
{
w.WriteLine("test");
}
}

Inserting Selected Item on Specific Folder

I have 2 buttons (Button1 - Browse ; Button2 - Upload) and 1 textBox
Here is the scenario. When I click on Browse, it will open a window and will browse for a specific item.
The selected item will display on textbox.
private void Browse_Click(object sender, EventArgs e)
{
btnSample.Title = "Sample File Upload";
btnSample.InitialDirectory = #"c:\";
btnSample.Filter = "TIF|*.tif|JPG|*.jpg|GIF|*.gif|PNG|*.png|BMP|*.bmp|PDF|*.pdf|XLS|*.xls|DOC|*.doc|XLSX|*.xlsx|DOCX|*.docx";
btnSample.FilterIndex = 2;
btnSample.RestoreDirectory = true;
if (btnSample.ShowDialog() == DialogResult.OK)
{
textBox1.Text = btnSample.FileName;
}
}
When I click on the Upload Button the file on the textbox will be on the created folder.
I'm done with creating the folder. but my problem is how can i insert the selected file on the folder.
private void button4_Click(object sender, EventArgs e)
{
string path = #"C:\SampleFolder\NewFolder";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
MessageBox.Show("Successfully Created New Directory");
}
else
{
MessageBox.Show("Filename Exist");
}
}
var sourceFilePath = #"C:\temp\file.txt";
var destFilePath= #"C:\otherFolder\file.txt";
If you want to move the file:
File.Move(sourceFilePath, destFilePath);
If you want to copy the file
File.Copy(sourceFilePath, destFilePath);
easy huh?
ofcourse, you'd have to adapt the paths to your problem...

Cancel opening link in browser

In my Windows phone application I use RichTextBox element
I have a hyperlink on it, and when user click on it there is a dialog: Do you want to open this link in exteranl browser. If user say no, external browser shouldn't be opened. I cancel navigation but in any case - external browser opens. How can I cancel opening link in browser?
//Constructor
static Helper()
{
var phoneApplicationFrame = Application.Current.RootVisual as PhoneApplicationFrame;
if (Application.Current.RootVisual as PhoneApplicationFrame != null)
{
phoneApplicationFrame.Navigating += new NavigatingCancelEventHandler(NavigationService_Navigating);
}
}
link.Foreground = new SolidColorBrush(Colors.Blue);
link.MouseOverForeground = new SolidColorBrush(Colors.Blue);
link.TargetName = "_blank";
var linkText = new Run() { Text = linkDesc };
link.Inlines.Add(linkText);
link.Click += new RoutedEventHandler(NavidateTo);
private static void NavidateTo(object sender, RoutedEventArgs routedEventArgs)
{
if (MessageBox.Show(
Constants.BrowserNavigating,
"",
MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
{
StateManager.Set("ExternalBrowser", "true");
}
else
{
StateManager.Set("Browser", "true");
}
}
public static void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
var res = StateManager.Get("ExternalBrowser");
if (res != null)
{
StateManager.Remove("ExternalBrowser");
e.Cancel = true;
}
}
Rather than have the HyperlinkButton open the link itself, don't specify the NavigationUri but handle the Tap event yourself.
In the eventhandler ask the question and only open the browser if they say yes.
This will be much simpler than trying to cancel something that is already in progress.

Categories

Resources