I have three ComboBoxs get there value from folder and subfolder. when I close the WinForm
and run it again I have to set ComboBoxs value again..
What I need to do is Save the previous selection of ComboBoxs
private void Form1_Load(object sender, EventArgs e)
{
if (Directory.Exists(rootDirectory))
{
comboBox1.DataSource = Directory.GetDirectories(rootDirectory).Select(Path.GetFileName).ToList();
comboBox1.SelectedIndexChanged += comboBox1_SelectedValueChanged;
comboBox2.SelectedIndexChanged += comboBox2_SelectedIndexChanged;
comboBox3.SelectedIndexChanged += comboBox3_SelectedIndexChanged;
comboBox1.Enter += comboBox1_Enter;
}
else
{
MessageBox.Show("Cannot find folder!!! ");
}
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
var parentDir = Path.Combine(rootDirectory, comboBox1.SelectedItem.ToString());
comboBox2.DataSource = Directory.GetDirectories(parentDir).Select(Path.GetFileName).ToList();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
var parentDir = Path.Combine(rootDirectory, comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString());
comboBox3.DataSource = Directory.GetDirectories(parentDir).Select(Path.GetFileName).ToList();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
var parentDir = Path.Combine(rootDirectory, comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString(), comboBox3.SelectedItem.ToString());
}
1 - select the combobox
2 - go to Properties > Data > (ApplicationSettings)
3 - add application settings binding to Text property
4 - on FormClosed event save application settings
Saving settings:
private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default.Save();
}
Credits here!
First this fiction is incorrect for listing directories, sub directories and files. Bad idea to use dropdown(s).
Use "TreeView" component in win form.
Example : https://www.c-sharpcorner.com/article/display-sub-directories-and-files-in-treeview/
So, you can save/set only the selected value in treeview.
Note : You can also review the recursive functions.
Related
So I have 2 list boxes within my form. Listbox1 contains different types of items that have a price and Listbox2 contains how much of that item you want to purchase. How do I update my price label so when I select both options from each list box it updates the label and gives me a price. Here's an example to help you better understand.
I select the $1.50 Chocolate Chip Cookie item in my ListBox1 and in ListBox2 I select the 1 Dozen Cookie item. So I would want my priceLabel to update to $18.00. How would I do this?
As of now I have tried creating some code in the listBox1_SelectedIndexChanged method but I am returned these 3 following values... $0.00...$2.00...$4.00
Here's my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
double index = listBox1.SelectedIndex;
double index2 = listBox2.SelectedIndex;
double total = index * index2;
label9.Text = total.ToString("C");
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
const int ESTIMATED_ARRIVAL = 3;
label10.Text = monthCalendar1.SelectionStart.AddDays(ESTIMATED_ARRIVAL).ToShortDateString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
The problem in your code is that the ".SelectedIndex" property only returns a 0-index position of the currently selected item in the listbox. That's why the calculation goes wrong.
I would like to suggest the use of the ".SelectedItem" property. Because it returns the selected item itself instead of it's postition. So if your items contain a double value, then the calculation will succeed.
I have a combobox and in the combobox I have multiple options like 5sec, 10sec 20 sec, etc, when I select any one option the gridview refreshes after that specific time.
Following is code which load files in datagridview.
public string Path { get; set; }
private void UploadButton_Click(object sender, EventArgs e)
{
var o = new OpenFileDialog();
o.Multiselect = true;
if(o.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
o.FileNames.ToList().ForEach(file=>{
System.IO.File.Copy(file, System.IO.Path.Combine(this.Path, System.IO.Path.GetFileName(file)));
});
}
this.LoadFiles();
}
private void Form_Load(object sender, EventArgs e)
{
this.LoadFiles();
}
private void LoadFiles()
{
this.Path = #"d:\Test";
var files = System.IO.Directory.GetFiles(this.Path);
this.dataGridView1.DataSource = files.Select(file => new { Name = System.IO.Path.GetFileName(file), Path = file }).ToList();
}
Follow these steps:
Put a Timer on your Form
Add 5, 10, 20 to your ComboBox and set its DropDownStyle property to DropDownList
Handle Load event of Form
Handle SelectedIndexChanged event of ComboBox
Handle Tick event of Timer
Write Codes:
private void Form1_Load(object sender, EventArgs e)
{
//Setting 0 as selected index, makes SelectedIndexChanged fire
//And there we load data and enable time to do this, every 5 seconds
this.comboBox1.SelectedIndex = 0; //To load each 5 seconds
}
private void LoadFiles()
{
//Load Data Here
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.timer1.Stop();
this.LoadFiles(); //optional to load data when selected option changed
this.timer1.Interval = Convert.ToInt32(this.comboBox1.SelectedItem) * 1000;
this.timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.LoadFiles();
}
I have seen many solutions about this but i didn't get proper answer. All answers are confusing.
contextMenu.MenuItems.Add(new MenuItem("Update", CopyClick));
DataGridViewCell ActiveCell = null;
private void CopyClick(object sender, EventArgs e)
{
if (ActiveCell != null && ActiveCell.Value != null)
Clipboard.SetText(ActiveCell.Value.ToString());
}
currently i am using above code and it will copy current cell value but i want like - if i select any row and press copy then it will copy only first value from the row.
so how can i do that?
I hope this helps.
var str = YourDataGridView.Rows[ActiveCell.RowIndex].Cells[0].Value.ToString();
Clipboard.SetText(str);
Use this snippet to copy the first item from your datagridview selected Row:
int ActiveRow = null;
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);
}
void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
ActiveRow = e.RowIndex;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
contextMenuStrip1.Show();
contextMenuStrip1.Items[0].Click += new EventHandler(Copy_Click);
}
}
void Copy_Click(object sender, EventArgs e)
{
if(ActiveRow!=null)
Clipboard.SetText(dataGridView1.Rows[ActiveRow].Cells[0].ToString());
}
Now there are 2 buttons, let's call them "Button1" and "Button2" respectively.
If I click "Button1", store "1" in ViewState["Record"]; also, if I click "Button2",
store "2" in ViewState["Record"], etc.
When I click Button1⟶Button1⟶Button2⟶Button2, my expected result is: in ViewState["Record"], there are a list of records, like
[1,1,2,2].
Here is my click event code:
protected void Button1_Click(object sender, EventArgs e)
{
ViewState.Add("Record", "1");
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState.Add("Record", "2");
}
//show the viewstate result
protected void ShowResult(object sender, EventArgs e)
{
for (int i = 1; i <= 4; i++)
{
Label.Text += ViewState["Record"];
}
}
It shows "2222" instead of "1122".
How can I code to resolve it?
When you add Record key a value actually it not adds, just puts a value Record key. You always see last added value on your loop.
You should add a List to ViewState and add value to list.
Add this property
protected List<long> ViewStateList
{
get{ if(ViewState["Record"] == null) ViewState["Record"] = new List<long>();
return (List<long>)ViewState["Record"] }
}
And use like
protected void Button1_Click(object sender, EventArgs e)
{
ViewStateList.Add(1);
}
Loop
protected void ShowResult(object sender, EventArgs e)
{
foreach(long item in ViewStateList)
{
Label.Text += item.ToString();
}
}
in c#, i have a listview control. i want the text fields in ONE of the columns to get truncated at the beginning instead of the end.
i.e. i want the text fields in column 2 to read:
...name1
...name2
...name3
instead of
filena...
filena...
filena...
Try
void Form1_Load(object sender, EventArgs e)
{
listView1.OwnerDraw = true;
listView1.DrawColumnHeader +=listView1_DrawColumnHeader;
listView1.DrawSubItem+=listView1_DrawSubItem;
}
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawText();
}
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
e.DrawText(TextFormatFlags.Right);
}
if it's just one specific column that you would like to see this behavior say col index = 5 then try
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex == 5)
{
e.DrawText(TextFormatFlags.Right);
}
else
{
e.DrawText();
}
}