|
am creating an application (using c#) whereby to one of the processes of the Application is that it has to read ID Card details(name, Image on card and card number) store it in the database (using SQLite) each time a staff wants to take an item from the store. How do I get the application to interpret the data it gets from the card reader and send it to the database, pls help. thanks
|
|
|
|
|
Read the documentation for the card reader device; that is the only way you can find the answer to this question.
|
|
|
|
|
I have a Listbox on Form1. I want on click on Listbox item to display the item name in a TextBox on Form2. How can I do it?
|
|
|
|
|
public partial class Form1 : Form
{
public delegate void ListBoxItemClickHandler(string itemText);
private ListBoxItemClickHandler OnListBoxItemClick;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("monkey");
listBox1.Items.Add("nut");
listBox1.Items.Add("car");
Form2 frm = new Form2();
OnListBoxItemClick += new ListBoxItemClickHandler(frm.ShowItem);
frm.Show();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (OnListBoxItemClick != null) OnListBoxItemClick(listBox1.SelectedItem.ToString());
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void ShowItem(string itemText)
{
textBox1.Text = itemText;
}
}
|
|
|
|
|
Thanks a lot for example. It compiles and runs ok, but i see nothing in text box clicking on the items.
|
|
|
|
|
Sorry my mistake. It works like magic. Thank you very much.
|
|
|
|
|
One more question. What if i want to update item in listBox1 on Form1 by typing in textBox1 on Form2?
|
|
|
|
|
Alternative with Observable pattern (just for the fun )
public interface IObservable
{
void AddObserver(IObserver observer);
void NotifyObservers(object obj);
void RemoveObserver(IObserver observer);
}
public interface IObserver
{
void Update(object obj);
}
public partial class Form1 : Form, IObservable
{
public string ItemText { get; private set; }
private List<IObserver> _Observers = new List<IObserver>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("monkey");
listBox1.Items.Add("nut");
listBox1.Items.Add("car");
for (int k = 0; k < 3; k++)
{
Form2 frm = new Form2();
frm.Text = k.ToString();
AddObserver(frm);
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
frm.Show();
}
}
private void frm_FormClosed(object sender, FormClosedEventArgs e)
{
RemoveObserver(sender as IObserver);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ItemText = listBox1.SelectedItem.ToString();
NotifyObservers(this.listBox1);
}
public void AddObserver(IObserver observer)
{
_Observers.Add(observer);
}
public void NotifyObservers(object obj)
{
foreach (var observer in _Observers)
{
observer.Update(obj);
}
}
public void RemoveObserver(IObserver observer)
{
_Observers.Remove(observer);
}
}
public partial class Form2 : Form, IObserver
{
public Form2()
{
InitializeComponent();
}
public void Update(object obj)
{
ListBox box = obj as ListBox;
textBox1.Text = box.Text;
}
}
|
|
|
|
|
Awesome! And what if want to go visa-verse- to update item in listBox1 on Form1 by typing in textBox1 on Form2?
|
|
|
|
|
The kind of thing Joost has given you will work for simple use cases. If you want interaction between non-modal forms or multiple windows and controls, it gets messy quickly. Implementing a decent MVVM or model-view separation with controls on both forms data bound to an underlying data model is more effort initially, but if your application is likely to grow and the number of interactions become significant, it will help you a lot.
|
|
|
|
|
I'm afraid i don't have enough experience to implement " MVVM or model-view separation with controls on both forms data bound to an underlying data model"
|
|
|
|
|
Well let's have a look at that from the perspective of the second question you asked (typing on the second form updates values on the first).
First, what's the data model? It's a list of items (displayed in the list box) which have a name (editable on the second):
public class Item : BaseNotifier {
private string name;
public string Name {
get { return name; }
set { name = value; Notify("Name"); }
}
public override string ToString() { return name; }
}
public class DataModel {
public IList<Item> Items { get; private set; }
public DataModel() {
Items = new List<Item>();
}
}
We're just going for model-view separation here as there's no need for an intermediate view model layer, since the interaction between model and view is so simple. If you're wanting a rigorous MVVM type approach you can include a view model interface (IItem : INotifyPropertyChanged with a single property Name), but we don't need that. BaseNotifier is a simple implementation of INotifyPropertyChanged that you should put in a utility library somewhere, I've written it 10 times at least:
public class BaseNotifier : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string property) {
var handler = PropertyChanged;
if(null != handler) handler(this, new PropertyChangedEventArgs(property));
}
}
Now we have a data model, which you can populate on application startup:
DataModel dataModel = new DataModel();
dataModel.Items.Add(new Item { Name = "Item 1" });
dataModel.Items.Add(new Item { Name = "Item 2" });
Now, data binding. I'm going to skip over the details of data binding a list box because it isn't very good, in WinForms, and I don't think it's helpful to go into that in a short post here. Instead, let's just ignore data binding and populate the list control from our data model explicitly:
class Form1 {
private void PopulateListBox(DataModel dataModel) {
listBox1.Items.Clear();
foreach(Item item in dataModel.Items) {
listBox1.Items.Add(item);
item1.PropertyChanged += HandleItemPropertyChanged;
}
}
}
This introduces a small complication for adding/removing items; feel free to investigate BindingList and all that area if you want some wider reading. However, for this example and any where the list is fixed, it will work. There's some dull code in that event handler to update the list, due to the bad implementation of ListBox:
private void HandleItemPropertyChanged(object sender, PropertyChangedEventArgs e) {
Item item = (Item)sender;
int index = listBox1.Items.IndexOf(item);
if(index >= 0) {
if(e.property == null || e.property == "Name") {
int previousSelectedIndex = listBox1.SelectedIndex;
listBox1.Items.Remove(item);
listBox1.Items.Insert(index, item);
listBox1.SelectedIndex = previousSelectedIndex;
}
}
}
Call that method within the form's constructor, either constructing the data model in place or passing it in as a constructor parameter.
Now, onto the binding on the second form. Give it a property of the item it's editing:
class Form2 : Form {
private Item editingItem;
public Item EditingItem {
get { return editingItem; }
set {
textBox2.DataBindings.Clear();
editingItem = value;
if(null != value) {
textBox2.DataBindings.Add("Text", value, "Name");
}
}
}
}
(If your list box and text box have sensible names, switch out listBox1 and textBox2 for those names, obviously.)
At this point, the two forms are individually able to bind to the model (the whole model in the case of Form1, one Item in the case of Form2), but we don't have any linkage between them. This is where the concept of the view-model or controller comes in: which item is selected on Form1 is nothing to do with the model, but affects what the view is seeing from the model, so it sits between the model (DataModel and Item) and the view (Form1 and Form2). There are a number of ways to do this; my preferred one is to layer the code such that the view refers to the intermediate layer, and the intermediate layer knows nothing about the view, making it a VM not a controller if my understanding of those patterns is correct.
In this case the VM is very simple:
public class ApplicationVM : BaseNotifier {
private readonly DataModel model;
public ApplicationVM(DataModel model) { this.model = model; }
private Item selectedItem;
public Item SelectedItem {
get { return selectedItem; }
set { selectedItem = value; Notify("SelectedItem"); }
}
public IList<Items> Items { get { return model.Items; } }
}
Now you need a reference to the VM in the forms, not just the model. (It's possible to structure this so the view gets the item list directly from the DataModel, but it is better not to as the ApplicationVM class is supposed to represent the model from the point of view of what the view needs.) Update Form1's constructor and PopulateListBox appropriately.
Once again I'm going to skip over binding this directly to a list box's SelectedValue or DataSource and suggest that you just attach directly to the SelectedValueChanged event to update it:
Form1 {
private void listBoxSelectedValueChanged(object sender, EventArgs e) {
viewModel.SelectedItem = (Item)listBox.SelectedItem;
}
}
For this particular case we don't need to bind the other way but if the VM can be updated outside Form1 (for example, Next/Prev buttons on Form2) you might, so here's how to do that:
public Form1(ApplicationVM viewModel) {
InitialiseComponent();
PopulateListBox(viewModel);
viewModel.PropertyChanged += (s, e) => listBox1.SelectedItem = viewModel.SelectedItem;
}
Finally, in Form2, make it update the item it's displaying from the VM:
public Form2(ApplicationVM viewModel) {
InitialiseComponent();
viewModel.PropertyChanged += (s, e) => EditingItem = viewModel.SelectedItem;
}
Done
At the end we have 2 view classes (Form1 and Form2), two model classes (DataModel and Item) and one VM class (ApplicationVM). The views bind (either with 'real' data binding or event handlers) to properties on the VM and on model classes, without needing to know about what might need to change when they update those properties. You can now switch out views, add new views on different aspects of the model, leave several Form2s up looking at different Items, and serialise or save your model much more easily.
Now I appreciate that this is quite a long post for such a trivial problem. If all this application ever wants to do is pop up a secondary form when you select a different item in a list, it is probably overkill to do it 'properly'. If you write a real application where the view update relationships are complex, this layering will be very helpful. And hopefully this explanation will teach you some things and kick off some interest in further reading, even if you don't need it now.
Here's an article by me expanding on data binding and MVVM-type design in WinForms.
|
|
|
|
|
Many thanks! Great introduction. I'll try to implement this model.
|
|
|
|
|
i need program in pharmacy in c#
|
|
|
|
|
Please ask stupid questions/make stupid statements in Q&A please. This is my refuge.
Regards,
Rob Philpott.
|
|
|
|
|
No, it's already full of them.
|
|
|
|
|
|
1. Take your computer into the pharmacy
2. Load Visual studio
3. Create a C# program
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
Chris Quinn wrote: Take your computer into the pharmacy One with free wifi.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
You don't need wifi to program in C#
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
Are you sure?
How else do you get to QA? You so silly.
Actually, with VS online you would now.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
You need you're head examined if you think anyone is going to write your code for you.
|
|
|
|
|
This seems to be a general trend in this forum, (or is it just me?). There seems to be a plethora of entries from people with names like 'member xxxxxxx' where xxxxxx is a big number, asking for programmes to be written for them, usually written in bad english. What's going on here?
|
|
|
|
|
We are happy to help answer questions but you it appears you don't have a single question but need to hire someone. I would search google for sites where you can hire a developer.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Even if you PAID us to do it, we couldn´t. There is no specification whatsoever.
Alternitavely you can wait until telepathy is possible in which case we can read your mind and know what your talking about.
|
|
|
|