(This is a continuation of the discussion on this question)
I have code that looks in a specific folder in the C: drive. It can tell what is in that folder, and gets what the user selects. But the problem is switching to a new Data folder to get the code from.
All the code necessary to run the program is kept in the Data folder, the company I am interning with wants to be able to switch Data folders so they can show their software off better, and have it geared towards whoever they are showing it to.
So basically my program needs to switch data folders so the company can show their software better.
Ill post all my code, its not much, so you guys can look at all of it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string defaultPath = #"C:\Mavro\MavBridge\";
public Form1()
{
InitializeComponent();
dropdown();
}
private void button1_Click(object sender, EventArgs e)
{
//some sort of code to switch directory before close goes here
MessageBox.Show("Data folder has been changed.", "Done");
Application.Exit();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string path = comboBox1.SelectedItem.ToString();
defaultPath = path;
}
private void buttonTest_Click_1(object sender, EventArgs e)
{
}
public void dropdown()
{
string[] dispDirectories = Directory.GetDirectories(defaultPath, "Data*");
comboBox1.Items.Clear();
comboBox1.Items.AddRange(dispDirectories);
}
}
}
To answer your second question, about stripping the Default Path from the combobox display
//Where you load your directories
string[] dispDirectories = Directory.GetDirectories(#"c:\", "*.*");
// so here we will iterate through all the directories found and remove the default path from it.
for (int i=0;i<dispDirectories.Count();i++)
dispDirectories[i]=dispDirectories[i].Remove(0, defaultPath.Length);
Then where you set your path change to this. Because we removed the default Path we now have to add it again.
string path = defaultPath+comboBox1.SelectedItem.ToString();
defaultPath = path;
Look at your button1_Click method. Change your message Box to
MessageBox.Show("Data folder has been changed to "+defaultPath,"Done");
private void button1_Click(object sender, EventArgs e)
{
//some sort of code to switch directory before close goes here
MessageBox.Show("Data folder has been changed to "+defaultPath,"Done");
Application.Exit();
}
you will see that you have already changed the default Path
EDIT(#K'Leg Suggestion to make answer more clear): If you want to get the subdirectories, after you make your first selection you should call method dropdown() in comboBox1_SelectedIndexChanged
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string path = comboBox1.SelectedItem.ToString();
defaultPath = path;
dropdown();
}
A better would be receive default path as parameter in dropdown(), something on the following line
public void dropdown(string defaultPath)
{
string[] dispDirectories = Directory.GetDirectories(defaultPath, "Data*");
comboBox1.Items.Clear();
comboBox1.Items.AddRange(dispDirectories);
}
and then call dropdown method in comboBox1_SelectedIndexChanged as:
dropdown(comboBox1.SelectedItem.ToString());
EDIT: (based on the comments on OP) the problem is the filter you are specifying for the GetDirecotries, for every path you pass to it, it looks for folder starting with Data and then any characters, for example Data.Apple, now when you set your path to Data.Apple, there it again looks for folder which should start with Data, you may pass the filter in the dropdown method on some condition
you may define the method dropdown as:
public void dropdown(string defaultPath, string filter)
{
string[] dispDirectories = Directory.GetDirectories(defaultPath, filter);
comboBox1.Items.Clear();
comboBox1.Items.AddRange(dispDirectories);
}
Then you can call the dropdown for the first time as :
public Form1()
{
InitializeComponent();
dropdown(#"C:\Mavro\MavBridge\","Data*");
}
and then in the SelectedIndexChanged as:
dropdown(comboBox1.SelectedItem.ToString(),"*"); // * means select all
Related
I don't understand what to do
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1 = "France")
{
pictureBox1.Image = Image.FromFile(#"C:\user\proga ot alejandro\1.jpg");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
I should make programm when i text a country name in textbox it will open a picture in picture box
I need help please. I would be kicked from the university if i wont make that programm.
Try this code in your Text Changed event - then build on all your other countries that are needed.
if (textBox1.Text.ToLower() == "france")
{
pictureBox1.Image = Image.FromFile(#"C:\path to picture");
}
else if (textBox1.Text.ToLower() == "us")
{
pictureBox1.Image = Image.FromFile(#"C:\path to picture");
}else if(continue on...){}
Here are a few suggestions to put you on the right track (but please make sure that the final code you turn into "university" is something you can explain and justify ;).
First, put the images into a folder and set their properties to Copy if Newer so that they can be read from a known path at runtime.
This makes it easy to capture a list of the image file names when you start your program.
public partial class MainForm : Form
{
public MainForm() => InitializeComponent();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
// Make an array of the image file names so you can search it.
_images =
Directory
.GetFiles(folder)
.ToArray();
textBoxSearch.TextChanged += ontextBoxSearchChanged;
}
}
private string[] _images;
Then, when the text changes, you can use a System.Linq expression or some other method to search this list for matches (making sure not to consider the extension or the folder path). Just make certain that this search functionality "knows what to do" if it finds multiple matches.
private void ontextBoxSearchChanged(object sender, EventArgs e)
{
// Do not block on this event.
BeginInvoke((MethodInvoker)delegate
{
string[] matches;
if(string.IsNullOrWhiteSpace(textBoxSearch.Text))
{
matches = new string[0];
}
else
{
// Use Linq to detect matches
matches =
_images
.Where(_ =>
Path.GetFileNameWithoutExtension(_)
.Contains(textBoxSearch.Text)
).ToArray();
}
labelMatchCount.Text = $"{matches.Length} matches";
if(matches.Length.Equals(1))
{
// Found a single match
labelMatchCount.Visible = false;
pictureBox.Image = Image.FromFile(matches.First());
}
else
{
// Found multiple matches
pictureBox.Image = null;
labelMatchCount.Visible = true;
}
});
}
I have a a simple Form, with a panel that holds a question with four CheckButtons as answers.
The users will go through the form and select the answer for each Question.
Once they click the button to accept the answer ("buttonNewAnswer_Click" below in the code)
the answer gets consolidated in a List named "answers" and then I write that into "results" and format it so I can write one line to a .csv file.
Once all the Questions are covered, the users will click "buttonExit_Click" button
and that will write the "results" to the .csv and also exit the Application.
Unfortunatelly I cannot get the "results" list from the "buttonNewAnswer_Click" to "buttonExit_Click".
Thank you for your help/suggestions.
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SIMPLE_FORM
{
public partial class Form1 : Form
{
//public List<String> results = new List<String>();
string myCsvFileTest = #"myFile.csv"
// Button to update the answers list
private void buttonNewAnswer_Click(object sender, EventArgs e)
{
// Algorithm to update the "answers" list
var results = new StringBuilder();
foreach (var i in answers)
{
results.AppendFormat("{0},", i.ToString());
}
}
// Button to write the results to a .csv and then close the application
private void buttonExit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Press \"Yes\" to confirm closing the Application", " ", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
using (StreamWriter writer = new StreamWriter(myCsvFileTest, true, Encoding.UTF8))
{
writer.WriteLine(results);
}
System.Windows.Forms.Application.Exit();
}
else
{
this.Activate();
}
}
I'm trying to get the "results" list from the "buttonNewAnswer_Click" and use it somewhere else in the code such as "buttonExit_Click" to write to a .csv
You need to declare the results object as a Form1 class member.
Right now, you are defining it as a local variable in buttonNewAnswer_Click function - so it's destroyed once the function ends.
Simplified code based on the code in the question:
public partial class Form1 : Form
{
// declare and allocate
StringBuilder results = new StringBuilder();
private void buttonNewAnswer_Click(object sender, EventArgs e)
{
// fill the results object
foreach (var i in answers)
{
results.AppendFormat("{0},", i.ToString());
}
}
private void buttonExit_Click(object sender, EventArgs e)
{
// you can use the result here.
// results
}
}
I am trying to select a bunch of files and put the names into a checkedlistbox but it always displays the full directory path with the filename. I only want the user to see the file name but I want to preserve the path inside the code so when the user clicks a button the program can still find the files and operate on them.
My question has already been asked on another forum but I can't seem to get the expected results, currently, my code does as follows:
user clicks button_1: User selects folder containing files
all CSV files with only their file name in the checkedlistbox are displayed, with a message box appearing displaying their full paths. The user proceeds to check necessary files.
User clicks button_2: Displays a message box with the checked filenames, but not the full file paths, of which I am trying to retrieve.
Any help in this would be most appreciated thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace SelectFiles
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
checkedListBox1.CheckOnClick = true;
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
checkedListBox1.Items.Clear();
string[] files = Directory.GetFiles(fbd.SelectedPath);
List<FileInfo> excel_files = new List<FileInfo>();
foreach (string file in files)
{
FileInfo f = new FileInfo(file);
MessageBox.Show((f.FullName));
excel_files.Add(f);
}
BindingSource bs = new BindingSource();
bs.DataSource = excel_files;
checkedListBox1.DataSource = bs;
checkedListBox1.DisplayMember = "Name";//Path.GetFileName(file);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
List<FileInfo> list_all_excelfiles = new List<FileInfo>();
foreach (FileInfo item in checkedListBox1.CheckedItems)
{
list_all_excelfiles.Add(item);
MessageBox.Show(Path.GetFileName(item.FullName));
}
}
}
}
If I understood correctly, you want to get the file full path when user click on button2.
This can be achieved by modifying your code.
In the button2 event, are asking for Path.GetFileName
Change it to
Path.GetFullPath
which will return the full path of the file.
Your code should be looks like :
private void button2_Click_1(object sender, EventArgs e)
{
List<FileInfo> list_all_excelfiles = new List<FileInfo>();
foreach (FileInfo item in checkedListBox1.CheckedItems)
{
list_all_excelfiles.Add(item);
MessageBox.Show(Path.GetFullPath(item.Name));
}
}
Note : in your code, you are trying to clear the items from checkedListBox1 by Clear() method but you'll face an exception.
System.ArgumentException: 'Items collection cannot be modified when
the DataSource property is set.'
and that's because you added a data source already !
instead use :
checkedListBox1.DataSource = null;
I got a homework assignment here. I've created a form with 4 text boxes for data entry, Account #, First Name, Last Name, Balance. I have four buttons, Create File, Save data to file, clear, and exit. Basically all the program does is create a text file, then I input my data into the text boxes, then I hit save data file which will write the data to the text file I have created. Clear and Exit are already done, and I have the program working as far as creating the text file, now I just need someone to point me in the right direction on how to actually write the data I entered into the text file. Here is my code, Thanks in advance
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Chapter_17_Ex.Sample_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCreate_Click(object sender, EventArgs e)
{
SaveFileDialog file = new SaveFileDialog();
file.FileName = "client.txt";
file.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
file.ShowDialog();
StreamWriter filewrite = new StreamWriter(file.FileName);
}
private void btnSave_Click(object sender, EventArgs e)
{
TextWriter file = new TextWriter
}
private void btnClear_Click(object sender, EventArgs e)
{
txtAccount.Clear();
txtBalance.Clear();
txtFirstName.Clear();
txtLastName.Clear();
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
You're on the right track with creating that StreamWriter instance. What you want to do now is use the WriteLine() method of that class. It's also a good idea to wrap that StreamWriter instance in a using block:
private void btnCreate_Click(object sender, EventArgs e)
{
SaveFileDialog file = new SaveFileDialog();
file.FileName = "client.txt";
file.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
file.ShowDialog();
using(StreamWriter filewrite = new StreamWriter(file.FileName))
{
filewrite.WriteLine( String.Format("First Name is {0}", txtFirstName.Text) );
//use Write() or WriteLine() again as needed.
}
}
For the file create you can use:
File.Create(file.FileName).Close(); // replace the StreamWriter code with this
For the simplest way to write into that file:
string line = string.Join(",", txtAccount.Text, txtBalance.Text, txtFirstName.Text, txtLastName.Text) + System.Environment.NewLine;
File.AppendAllText(file.FileName, line);
You also need to move the 'SaveFileDialog file' to be a class field, not a method local variable.
You didn't specify the output format. What I chose is CSV... but there's no "escaping" of the input, which needs to be done to prevent inputting commas from messing up the output. That part is up to you.
Note the use of File.Create and File.AppendAllText - these save you from having to put using around the actual I/O.
I know this question has been asked many of times about how to create a search button. I am very new to C# programming and I am having a hard time creating a search and just haven't found what I am looking for from other posts. So I hope someone can help me.
I have created a Windows Form Application and I have a form setup using "Details" view from my DataSet and the data shows up correctly in the application when I scroll from record to record. My data is stored in a sdf file. I want to have people either enter in an "account number" or a persons "last name" and then be able to hit the search button. And after the search button the prearranged fields would update with the information. For the ability to either choose the "last name" or the "account number" I can have the items listed in a combo box if need be.
I have included a copy of the code some of the naming of the items have been changed as to not disclose my profession. Any help is greatly appreciated.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void custtableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.custtableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.custDataSet);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void custtableBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
{
this.Validate();
this.custtableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.custDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'custDataSet.custtable' table. You can move, or remove it, as needed.
this.custtableTableAdapter.Fill(this.custDataSet.custtable);
}
private void file_Name_12TextBox_TextChanged(object sender, EventArgs e)
{
}
private void fillByToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.custtableTableAdapter.FillBy(this.custDataSet.custtable);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void btnfind_Click(object sender, EventArgs e)
{
}
}
}
What is the custtableBindingNavigatorSaveItem_Click and custtableBindingNavigatorSaveItem_Click_1 ? Thet are the same, if they are two buttons doing the same thing then you can use the same method from both buttons (though why two buttons to do the same thing I do not know).
Anyway, you have some choices and it depends on the datasize, speed needed, whether the data should be offline or db locked and so on...
Set you queries/stored procedures (sprocs are safer) to allow for limiting the result. This lets the DB do all the hard work - it is what DB's are designed for.
Read everything into memory (your table fill) and select off the table - not to disimilar really with Linq queries. This means all the data is offline and you will need to decide when and how you write it back to the database (if at all).
PS: I am intrigued, what is your profession that your pseudonym here is not enough to hide behind? Imagination gone wild !