Save file dialog doesn't appear sometimes - c#

I am developing an export function with save file dialog for list view, when list view rows go so much, more than 7000 or much, the save file dialog not appearing don't know why !
Update: added the snippet code, how I am filling the listview !
Here is the code I am using for exporting listview,
private void ExportBTN_Click(object sender, EventArgs e)
{
DialogResult dialogeResult = sfd.ShowDialog();
if (dialogeResult == DialogResult.OK)
{
using ( var tw = new StreamWriter(sfd.FileName))
{
foreach (ListViewItem item in URLListView.Items)
{
tw.WriteLine(item.Text);
}
tw.Close();
XtraMessageBox.Show("All links has been exported successfully.");
AddLog("All links has been exported successfully.");
}
}
}
Here is the way I am filling the listview:
if(URLListView.Items.Count == 0)
{
XtraMessageBox.Show("You have to get the main links first");
return;
}
if (GetInnerLinkBTN.Text == "Stop")
{
enablecontrols(true);
StopGettingInnerLink = true;
GetInnerLinkBTN.Text = "Start Get Innter Links";
}
else if (GetInnerLinkBTN.Text == "Start Get Innter Links")
{
enablecontrols(false);
StopGettingInnerLink = false;
GetInnerLinkBTN.Text = "Stop";
}
foreach (ListViewItem link in URLListView.Items)
{
string href = link.Text.ToString();
if (href.Trim() != string.Empty)
{
//XtraMessageBox.Show(href);
if (StopGettingInnerLink == true)
{
AddLog("Getting links has been stopped successfully!");
StopGettingInnerLink = true;
break;
}
else if(StopGettingInnerLink == false)
{
AddLog("Getting links from " + href);
// MainWebbrowser.Navigate(href);
runbrowserinthread(href);
await Task.Delay(5000);
AddLog("Giving the tool some rest for 5 seconds ! ");
}
}
AddLog("Scrapping inner links has been finished successfully!");
enablecontrols(true);

Related

How can I call same function using different elements like, buttons, textboxs...etc in C#?

I have this function that has different elements like buttons, textBoxes, dateTimePickers and comboBoxs. Image of the form for better understanding.
private void openLecture (object sender)
{
string url = "https://zoom.us/j/" + textLectureLink.Text;
//run it once
if (!initialized)
{
//Compare time and date to open lecture link
if (nowDate == calenderDatePicked.Text && nowTime == textLectureTime.Text
&& ampm == comboAMPM.Text)
{
try //Open Website
{
initialized = true; // to run it once
System.Diagnostics.Process.Start(url); //Open link
}
catch (Exception e)
{
initialized = true; // to run it once
System.Windows.Forms.MessageBox.Show("Click refresh and try again.");
}
}
}
}
I want to execute this function using different type of elements and not copying it to each of them. is there a way?

C# Button with DialogResult Retry

I'm going to use two buttons that have a DialogResult Retry. When you push the button the winform will hide, do something and it pops-up again. I use the While method for this. But if you have two buttons with a retry this won't work unless you set one button DialogResult to Yes and do a While method. But is there a better way to do this, case switch or something?
Note this is within a Class
try
{
// Create a form to select objects.
DialogResult result = System.Windows.Forms.DialogResult.None;
while (result == DialogResult.None || result == DialogResult.Retry)
{
// Picking Objects.
if (result == DialogResult.Retry)
{
System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.FileName = "test";
saveFileDialog1.Filter = "Family Files (*.rfa)|*.rfa|All Files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
var dialogResult = saveFileDialog1.ShowDialog();
if (dialogResult == DialogResult.OK)
{
string address = "http://www.autodesk.com/revit-basic-sample-family-2017-enu?_ga=2.28765692.1750602280.1538397390-459409917.1521646598";
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.DownloadFile(address, saveFileDialog1.FileName);
Autodesk.Revit.DB.Family family = null;
using (Transaction tx = new Transaction(doc))
{
tx.Start("Load Family");
if (doc.LoadFamily(saveFileDialog1.FileName, out family))
{
String name = family.Name;
TaskDialog.Show("Revit", "Family file " + name + " has been loaded ");
}
else
{
TaskDialog.Show("Revit", "Can't load the family file or already exists.");
}
tx.Commit();
}
}
if (dialogResult == DialogResult.Cancel)
{
}
}
// Show the dialog.
using (testForm selectionForm = new vuurenForm(commandData))
{
result = selectionForm.ShowDialog();
}
}
return Result.Succeeded;
You may set the DialogResult in the code, not in the form designer. Just double click the buttons and add something like:
private void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Retry;
}
That way both buttons will have the same DialogResult.
Than the loop is OK with only checking for the DialogResult.Retry.
Exactly you can try this code to manage your DialogResult like:
switch(MessageBox.Show("Text", "Title", MessageBoxButtons.YesNo))
{
case DailogResult == DialogResult.Yes:
//Do something
case DailogResult == DialogResult.Retry:
//Do something
}
Actually for two Button objects you have to have two event-handler objects and you can set:
DialogResult = DialogResult.Retry;
In the event that you want to Retry.
You can try this:
var dialogResult = DialogResult.Retry;
while (dialogResult == DialogResult.Retry) {
try {
CheckSomething();
break;
}
catch {
if (dialogResult == DialogResult.Abort) {secondDialog.DialogResult = Retry;}
throw;
}
}
You can also use enums like below:
enum Result {Ignore, Abort,Retry};

c# - cannot delete the last remaining item in listview

I am working on a project (simple phone book) for personal use. I am having trouble deleting the last remaining item in my listview (listView1). Here you can take a look how it looks like:
So, let's say that I have got 5 contacts in the list and when I try to remove all of them, it's not possible. It is possible to remove only 4 of them. When I try to remove all of them and then close/run the application, there will be no removed contacts. When I try to remove 4 of them and I close/run the program, they would be deleted. When I try to remove the last one, it is not possible either. When I close/run app it would always remain there.
Since it doesn't makes sense to upload the whole code here, you can take a closer look at it at this link.
How can I solve this issue?
This should work for removing items from the listview:
Person person = new Person();
try { person = FindPerson(listView1.SelectedItems[0].Text); } catch { return; }
if (listView1.SelectedItems.Count > 0)
{
try
{
if (listView1.SelectedItems.Count == 0) return;
foreach (ListViewItem eachItem in listView1.SelectedItems)
{
people.RemoveAll(x => x.Name == eachItem.Text);
listView1.Items[listView1.Items.Count - 1].Selected = true;
listView1.Items.Remove(eachItem);
}
}
catch { }
ClearAll();
ReadOnlyON();
}
else
{
MessageBox.Show("Nothing is selected!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
perhaps you can use the clear method to remove the last item.
ListView1.Clear();
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.clear(v=vs.110).aspx
You can loop through the list backwards and remove each item or use as suggested ListView1.Clear();
Just replace this line
listView1.SelectedItems[0].Remove();
in Remove()
with
foreach ( ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
}
SelectedItems[0].Remove(); does not work for last item.
This is the way I delete items:
void Rmv()
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this contact?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
Remove();
}
else if (dialogResult == DialogResult.No)
{
return;
}
}
void Remove()
{
if (listView1.SelectedItems.Count > 0)
{
Person person = new Person();
person = FindPerson(listView1.SelectedItems[0].Text);
people.RemoveAt(listView1.SelectedItems[0].Index);
foreach (ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
}
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
dateTimePicker1.Value = DateTime.Now;
if (listView1.SelectedItems.Count == 0)
{
textBox1.ReadOnly = false;
textBox2.ReadOnly = false;
textBox3.ReadOnly = false;
textBox4.ReadOnly = false;
textBox5.ReadOnly = false;
textBox6.ReadOnly = false;
dateTimePicker1.Enabled = true;
UserCount();
}
}
else
{
MessageBox.Show("Nothing is selected! ", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
void UserCount()
{
try
{
if ((listView1.Items.Count) == 0)
{
toolStripLabel1.Text = Convert.ToString(listView1.Items.Count) + "& contacts";
}
else if ((listView1.Items.Count) == 1)
{
toolStripLabel1.Text = Convert.ToString(listView1.Items.Count) + "& contact";
}
else
{
toolStripLabel1.Text = Convert.ToString(listView1.Items.Count) + "& contacts";
}
}
catch { }
}

Faster way of reading csv to grid

I have following in Windows Forms .NET 3.5
It works fine for csv with records less than 10,000 but is slower for records above 30,000.
Input csv file can can any records between 1 - 1,00,000 records
Code currently used :
/// <summary>
/// This will import file to the collection object
/// </summary>
private bool ImportFile()
{
try
{
String fName;
String textLine = string.Empty;
String[] splitLine;
// clear the grid view
accountsDataGridView.Rows.Clear();
fName = openFileDialog1.FileName;
if (System.IO.File.Exists(fName))
{
System.IO.StreamReader objReader = new System.IO.StreamReader(fName);
do
{
textLine = objReader.ReadLine();
if (textLine != "")
{
splitLine = textLine.Split(',');
if (splitLine[0] != "" || splitLine[1] != "")
{
accountsDataGridView.Rows.Add(splitLine);
}
}
} while (objReader.Peek() != -1);
}
return true;
}
catch (Exception ex)
{
if (ex.Message.Contains("The process cannot access the file"))
{
MessageBox.Show("The file you are importing is open.", "Import Account", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show(ex.Message);
}
return false;
}
}
Sample Input file :
18906,Y
18908,Y
18909,Y
18910,Y
18912,N
18913,N
Need some advice on optimizing this code for fast reads & view in grid.
List<string[]> rows = File.ReadAllLines("Path").Select(x => x.Split(',')).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("1");
dt.Columns.Add("2");
rows.ForEach(x => {
dt.Rows.Add(x);
});
dgv.DataSource = dt;
Try that, I suspected that you have some form of column names in the datagrid for now I just made them 1 and 2.
To filter as per your original code use:
List<string[]> rows = File.ReadAllines("Path").Select(x => x.Split(',')).Where(x => x[0] != "" && x[1] != "").ToList();
To get your columns from the DataGridView
dt.Columns.AddRange(dgv.Columns.Cast<DataGridViewColumn>().Select(x => new DataColumn(x.Name)).ToArray());
There isn't much to optimize in regards to speed, but following is much more readable. If it is too slow, it probably isn't the method reading the file, but your WinForm that needs to display >30k records.
accountsDataGridView.Rows.Clear();
using (FileStream file = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096))
using (StreamReader reader = new StreamReader(file))
{
while (!reader.EndOfStream)
{
var fields = reader.ReadLine().Split(',');
if (fields.Length == 2 && (fields[0] != "" || fields[1] != ""))
{
accountsDataGridView.Rows.Add(fields);
}
}
}
You can try to use SuspendLayout() and ResumeLayout() Methods.
From MSDN Documentation
"The SuspendLayout and ResumeLayout methods are used in tandem to suppress multiple Layout events while you adjust multiple attributes of the control. For example, you would typically call the SuspendLayout method, then set the Size, Location, Anchor, or Dock properties of the control, and then call the ResumeLayout method to enable the changes to take effect."
accountsDataGridView.SuspendLayout();
accountsDataGridView.Rows.Clear();
// .....
// in the end after you finished populating your grid call
accountsDataGridView.ResumeLayout();
Instead of putting the data directly into the grid you should take a look at the VirtualMode of the DataGridView.
In your code you're doing two things at one time (read the file, fill the grid), which leads to your freezed gui. Instead you should put the grid into the virtual mode and read the file within a BackgroundWorker into a list which holds the data for the grid. The background worker can after each line read update the virtual size of the grid, which allows to already see the data while the grid is loading. By using this approach you'll getting a smooth working grid.
Below you'll find an example which just needs to be filled into a form that uses a DataGridView with two text columns, a BackgroundWorker and a Button:
public partial class FormDemo : Form
{
private List<Element> _Elements;
public FormDemo()
{
InitializeComponent();
_Elements = new List<Element>();
dataGridView.AllowUserToAddRows = false;
dataGridView.AllowUserToDeleteRows = false;
dataGridView.ReadOnly = true;
dataGridView.VirtualMode = true;
dataGridView.CellValueNeeded += OnDataGridViewCellValueNeeded;
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork += OnBackgroundWorkerDoWork;
backgroundWorker.ProgressChanged += OnBackgroundWorkerProgressChanged;
backgroundWorker.RunWorkerCompleted += OnBackgroundWorkerRunWorkerCompleted;
}
private void OnBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
var filename = (string)e.Argument;
using (var reader = new StreamReader(filename))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
var parts = line.Split(',');
if (parts.Length >= 2)
{
var element = new Element() { Number = parts[0], Available = parts[1] };
_Elements.Add(element);
}
if (_Elements.Count % 100 == 0)
{
backgroundWorker.ReportProgress(0);
}
}
}
}
private void OnBackgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
dataGridView.RowCount = _Elements.Count;
}
private void OnBackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
dataGridView.RowCount = _Elements.Count;
button.Enabled = true;
}
private void OnButtonLoadClick(object sender, System.EventArgs e)
{
if (!backgroundWorker.IsBusy
&& DialogResult.OK == openFileDialog.ShowDialog())
{
button.Enabled = false;
backgroundWorker.RunWorkerAsync(openFileDialog.FileName);
}
}
private void OnDataGridViewCellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
var element = _Elements[e.RowIndex];
switch (e.ColumnIndex)
{
case 0:
e.Value = element.Number;
break;
case 1:
e.Value = element.Available;
break;
}
}
private class Element
{
public string Available { get; set; }
public string Number { get; set; }
}
}

SaveFileDialog Menuclick Item Doesnt Work.

If I set path = "C:\\MSREAD.txt"; and Click on SaveAs Menu Item ,it saves Filetext,But If I dont give the String path and save it from saveFD.FileName it doesnt work.Please help me with this issue.
Thanks a lot
public void SaveToFile()
{
String SavedFile = "";
saveFD.InitialDirectory = #"C:";
saveFD.Title = "Save a Text File";
saveFD.FileName = "";
RichTextBox richTextBox1 = new RichTextBox();
saveFD.Filter = "Text Files|*.txt|All Files|*.*";
try
{
if (saveFD.ShowDialog() != DialogResult.Cancel)
{
SavedFile = saveFD.FileName;
path = SavedFile.ToString();
//path = "C:\\MSREAD.txt";
MessageBox.Show(path);
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
SaveMyTextBoxContents(path);
}
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveToFile();
}
public void SaveMyTextBoxContents(string path)
{
if (listBoxItems.SelectedIndex == -1)
{
if (rdBtnSlow.Checked && rdBtnNo.Checked)
{
using (StreamWriter outputFile = new StreamWriter(path))
{
foreach (string item in listBoxItems.Items)
{
saveAllText = slowNo + " " + item;
outputFile.WriteLine(saveAllText);
}
}
}
}
}
Here is your problem:
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
SaveMyTextBoxContents(path);
You first save the richTextBox text to file, but then override the same file with SaveMyTextBoxContents, However the file is empty because of SaveMyTextBoxContents method will only save something if some conditions are true "not selected item and both check boxes are checked", and the listBoxItems.Items.Count > 0 which apparently not the case

Categories