Working with multiple files in C# - c#

I am facing special problem.
I have a button, which when clicked, will cause click event to open file dialog to choose file. Name of this file (SafeFileName) will be displayed in combo box. Then I read first 9 rows of that file and store them as struct of this type:
public struct DCM_INFO
{
public string FILE_NAME;
public string FILE_PATH;
public string VERSION;
public string NAME;
public string DATE;
public string BOX;
public string SERIAL_NUM;
public string SERIES;
public string POINT;
public string NOTE;
public string VARIANT;
}
First two strings are "SafeFileName" and "FileName".
This struct is then displayed in ListView.
And now the thing I want to do:
After I open second file with exact the same button click event, I want to add second "SafeFileName" to combobox and make it visible(displayed on top), then save data from file in DCM_INFO struct with different name than the first one. After this, If i choose different file than actively displayed in combo box, listview will be updated with adequate data from DCM_INFO struct.
I have figured out the part with combo box (display active file) but how to properly switch between listviews ?

You could overwrite the ToString method in the struct and add the struct directly to the combobox. In the SelectionCheangeCommited Event of the ComboBox you could extract the item (cast it back to the struct) and create the appropriate ListViewItems which are then added to your ListView (after clearing the ListViews Items collection)
public struct DCM_INFO
{
public string FILE_NAME;
public string FILE_PATH;
public string VERSION;
public string NAME;
public string DATE;
public string BOX;
public string SERIAL_NUM;
public string SERIES;
public string POINT;
public string NOTE;
public string VARIANT;
public override string ToString()
{
return FILE_NAME;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox1.SelectedItem != null)
{
DCM_INFO item = (DCM_INFO)this.comboBox1.SelectedItem;
// Create ListViewItems and add them to ListView
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
DialogResult ret = ofd.ShowDialog();
if (ret == System.Windows.Forms.DialogResult.OK)
{
DCM_INFO tmp = new DCM_INFO();
// read file and fill struct
this.comboBox1.Items.Add(tmp);
}
}
}

Related

How do I get a label in a form to display the values of a property

In my C# windows form I have 2 forms. I would like to display a collection of strings in a label on a form. When I debug I show the 2 elements in my array but they are not showing in the label I am passing it to. When I hover of toString the data is there but how to I pass it to sender so it will display in the label control I have on my form?
In the snippet of code below to data is in toString but how do I get it from there down to sender.ToString????
public AccountReport_cs(Func<string> toString)
{
this.toString = toString;
}
private void AccountReport_cs_Load(object sender, EventArgs e)
{
label1.Text = sender.ToString();
}
This is another piece of the code that will open form2 where the information should be displayed.
private void reportButton2_Start(object sender, EventArgs e)
{
AccountReport_cs accountReport = new AccountReport_cs(allTransactions.ToString);
accountReport.ShowDialog();
}
Here is the last piece of code and this will show how the data gets to EndOfMonth.
public class Transaction
{
public string EndOfMonth { get; set; }
}
public override List<Transaction> closeMonth()
{
var transactions = new List<Transaction>();
var endString = new Transaction();
endString.EndOfMonth = reportString;
transactions.Add(endString);
return transactions;
}
If you need to send information between forms, the best thing you can do is create a property in the target form and assign the value you want to send before displaying the form; thus you will not need to change the default constructor of the form.
// Destiny form
public partial class FormDestiny : Form {
// Property for receive data from other forms, you decide the datatype
public string ExternalData { get; set; }
public FormDestiny() {
InitializeComponent();
// Set external data after InitializeComponent()
this.MyLabel.Text = ExternalData;
}
}
// Source form. Here, prepare all data to send to destiny form
public partial class FormSource : Form {
public FormSource() {
InitializeComponent();
}
private void SenderButton_Click(object sender, EventArgs e) {
// Instance of destiny form
FormDestiny destinyForm = new FormDestiny();
destinyForm.ExternalData = PrepareExternalData("someValueIfNeeded");
destinyForm.ShowDialog();
}
// Your business logic here
private string PrepareExternalData(string myparameters) {
string result = "";
// Some beautiful and complex code...
return result;
}
}

Passing data from textbox to datagridview

I am working in Visual Studio running a Windows application.
I am wondering if I can fill a DataGridView from a TextBox, that was a passed value itself?
For example, the user would search for a patient from a dialog form. The patient's name they select would populate a TextBox on my main form. I want that selected patients prior test history to populate a DataGridView on that main form within a tab.
Is this possible, if so how would I accomplish this?
It is possible. I would suggest setting up some sort of data binding. More specifically you will want some class that maintains state and data binds to your controls and possibly your dialog form. I don't know how much you are looking for so this might be going overboard but I would suggest something like this:
public class MainForm : Form
{
public MainForm(StateManager stateManager)
{
_stateManager = stateManager;
//data binding for your text box
txtPatientName.DataBindings.Add(nameof(txtPatientName.Text), stateManager, nameof(stateManager.PatientName));
//data binding for your grid
historyGrid.DataSource = stateManager.History;
}
private void btnShowForm_Click(object sender, EventArgs e)
{
using(var form = new DialogForm())
{
var result = form.ShowDialog();
if(result == DialogResult.Ok)
{
_stateManager.UpdatePatient(form.InputPatientName);
}
}
}
private StateManager _stateManager;
}
//this is the form where you enter the patient name
public class DialogForm : Form
{
//this holds the value where the patient's name is entered on the form
public string InputPatientName { get; set; }
}
//this class maintains your state
public class StateManager : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string PatientName
{
get { return _patientName; }
set
{
_patientName = value;
OnPropertyChanged(nameof(PatientName));
}
}
public BindingList<MedicalHistoryItems> History => _history ?? (_history = new BindingList<MedicalHistoryItems>());
public void UpdatePatient(string patientName)
{
History.Clear();
var historyRetriever = new HistoryRetriever();
History.AddRange(historyRetriever.RetrieveHistory(patientName));
}
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(propertyName);
}
private BindingList<MedicalHistoryItems> _history;
private string _patientName;
}

C# How to concatenate path and variable

I want to display a different photo according to the text on the listBox. It will be about 1000 photos +.
The listbox.text will be the name of the picture(1, 2 ,3 etc).
I have no idea how to do that.
pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.(listBox2.Text);
Thanks for any help!
I guess this is what you want, you should get selected Item's text:
var imgName = listBox2.SelectedItem.ToString();
pictureBox1.Image = Resources.ResourceManager.GetObject(imgName) as Bitmap;
A very handy thing to do with list boxes is to add objects to them. The listbox displays the object's ToString() as the name of the object and you can then get the object directly using the SelectedItem property. Something like this would do what you need:
namespace showimage
{
public partial class Form1 : Form
{
private List<ImagePicker> image_list;
public Form1()
{
InitializeComponent();
image_list = new List<ImagePicker>();
// Add the images - creating an ImagePicker object per file
image_list.Add(new ImagePicker("Photo1", "photo1.jpg"));
image_list.Add(new ImagePicker("Photo2", "photo2.jpg"));
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.AddRange(image_list.ToArray());
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ImagePicker picked_image = (ImagePicker)listBox1.SelectedItem;
pictureBox1.Load(picked_image.filename);
}
}
public class ImagePicker
{
private string _name;
private string _filename;
public string filename
{
get
{
return _filename;
}
}
public ImagePicker(string name, string filename)
{
_name = name;
_filename = filename;
}
public override string ToString()
{
return _name;
}
}
}

Read the Value of an Item in a listbox

I'm developing a small windows store application in C# where I have populated the values and content for a listbox using the following code snippet.
code 1 : adds the song title as a item to a listbox, using the Song class to create the item
private void addTitles(string title, int value)
{
Song songItem = new Song();
songItem.Text = title;
songItem.Value = value;
listbox1.Items.Add(songItem); // adds the 'songItem' as an Item to listbox
}
code 2 : Song class which is used to set values to each item ('songItem')
public class Song
{
public string Text { get; set; }
public int Value { get; set; }
public override string ToString()
{
return Text;
}
}
Population content to the listbox is functioning currently.
What I want is to get the 'Value' of each item on a Click event, on run-time.
For that purpose, how can I read(extract) the Value of the selected Item in the listbox, in C#? ( Value is the songItem.Value )
code 3 : I have tried this code, as trying to figure out a solution, but it didn't work
private void listbox1_tapped(object sender, TappedRoutedEventArgs e)
{
Int selectedItemValue = listbox1.SelectedItem.Value();
}
Therefore it would be really grateful if someone can help me, as I'm an Amateur.
not sure about the "TappedRoutedEventArgs", but I would do
private void listbox1_tapped(object sender, TappedRoutedEventArgs e)
{
var selectedSong = (Song)listbox1.SelectedItem;
if (selectedSong != null) {
var val = selectedSong.Value;
}
}
because SelectedItem is an Object (which doesn't know about the Value property), so you have to cast it to a Song first.
By the way, Value is a property, not a method, so you don't need the parenthesis.
Try like this:
Song song=listbox1.SelectedItem as Song;
or this:
var selected = listbox1.SelectedValue as Song;
Try something like this :
if (listbox1.SelectedRows.Count>0){
Song song=(Song)listbox1.SelectedItem;
int value=song.Value;
}

CheckedListBox and EventArgs

What am I doing wrong here? be gentle.
For CheckedListBox, I can update the items simply by using:
private void button3_Click(object sender, EventArgs e)
{
checkedListBox4.Items.Add("whatever"); //or use an object
}
works great, but what I want to do is send the CheckedListItem a set of items from method within another class
So, I set up another class something:form1 that has a delegate that points to a method that I call\invoke
The delegate calls\invokes this way:
public delegate void m_del(List<DirectoryInfo> ww, string rr);
somewhere else in the code:
m_del methtouse = new m_del(listme)
public void listme(List<DirectoryInfo> fi, string mypath)
{
foreach (DirectoryInfo j in fi)
{
mypath = null; //mypath used by another method
try
{
NewCheckboxListItem cb1 = new NewCheckboxListItem();
cb1.Tag = j.Name;
cb1.Text = j.Name;
checkedListBox4.Items.Add(cb1);
}
catch (Exception w)
{
MessageBox.Show(w.Message);
}
}
}
public class NewCheckboxListItem
{
// define a text and
// a tag value
public string Text;
public string Tag;
// override ToString(); this
// is what the checkbox control
// displays as text
public override string ToString()
{
return this.Text;
}
}
methtouse( a List<DirectoryInfo> ww, a string rr)
{}
What happens is the Item collection in the checkedListBox4 is updated and has as many value as i send it, but it will not draw the item\show the item
I have tried calling a checkedListBox4_datavaluememberchanged method and a few other checkedListBox4_changed events but once again the items in the collection are updated but they do not appear in the CheckedListBox
I think its something to do with it not having eventargs
Is there an easy way to do a side by side comparison of ALL the attributes, events, properties of a successful CheckedListBox with that of an unsuccessful CheckedListBox (programmatically)
Note: The class inherits from form1 where the CheckedListBox is located, and the methods access is set to public.
What you are missing is to tell the checkedListBox what the value is supposed to be for that object.
checkedListBox4.ValueMember = "Text";
That tells the CheckedListBox to use the member matching that exact string as the display value.
https://stackoverflow.com/a/2023338/455536
http://msdn.microsoft.com/en-us/library/3yx132k0(v=vs.110).aspx
A string that specifies the property of the data source from which to draw the value.

Categories

Resources