开发者

Advice on a simple Windows Form

I have a VERY simple windows form that the user uses to manage "Stores".

Each store has a name and number, and is kept in a corresponding DB table.

The form has a listbox of stores, an add button that creates a new store, a delete button, and an edit button.

Beside those I have text boxes for the name and number, and save/cancel buttons.

When the user chooses a store from the list box, and clicks 'edit', the textboxes become populated and save/cancel become active. When the user clicks 'add', I cr开发者_运维百科eate a new Store, add it to the listbox, activate the textboxes and save/cancel buttons, then commit it to the database when the user clicks 'save', or discards it when the user clicks 'cancel'.

Right now, my event system looks like this (in psuedo-code. It's just shorter that way.)

add->click:
    store = new Store()
    listbox.add(store)
    populateAndEdit(store)

delete->click:
    store = listbox.selectedItem
    db.deleteOnSubmit(store)
    listbox.remove(store)
    db.submit()

edit->click:
    populateAndEdit(listbox.selectedItem)

save->click:
    parseAndSave(listbox.selectedItem)
    db.submit()
    disableTexts()

cancel->click:
    disableTexts()

The problem is in how I determine if we are inserting a new Store, or updating an existing one.

The obvious solution to me would be to make it a "modal" process - that is, when I click edit, I go into edit mode, and the save button does things differently than if I were in add mode.

I know I could make this more MVC-like, but I don't really think this simple form merits the added complexity. I'm not very experienced with winforms, so I'm not sure if I even have the right idea for how to tackle this.

Is there a better way to do this? I would like to keep it simple, but usable.


use some type of identifier, i.e., the name of the store. Now, when the user presses Save, check to see if your collection of stores already contains one with the same name. If it does, it's an update, otherwise it is a new store.

I realize that using the name of the store may not be possible for you (i.e., a supermarket with many branches), but you get the idea.


Surely you have some kind of ID in the database? Your object would expose this primary key as a StoreID or ID property for example. If it's a GUID, it'll be Guid.Empty for a new object, if it's an auto-incrementing integer it would be zero. When you save it to the database, it should get a valid ID and then you can easily tell if the object is new or existing.


you could check some kind of unique identifier as previously mentioned, or in the framework my company uses (CSLA .NET) there is a boolean IsNew field on our objects, that when we create a new object the field is marked true. When data is read into the object from the database the IsNew field is marked as false. This gives you the ability to have some kind of use the default new constructor in all cases, then when you populate an existing item mark it false.


In your situation, you may want to look at storing more than just a simple string to the listbox. Do you have any domain objects which the listbox values represent? Add these to the listbox instead - make sure to override the .ToString() so that the listbox ends up visualizing something meaningful. When the user selects Add, create a new domain object and add it to the listbox, ensuring that the appropriate property is set to mark this as a new record for insert to the database. Conversely, any records persisted to the database, as well as all existing records fetched from the database, could be marked appropriately so that you can handle any necessary edits and deletes.


You can just play with the text of your save button, just in case if it's new store change its text to "Insert Store" and in case of edit it would be "Update Store".

add->click:
store = new Store()
listbox.add(store)
save.Text="Insert Store"
populateAndEdit(store)

delete->click:
store = listbox.selectedItem
db.deleteOnSubmit(store)
listbox.remove(store)
db.submit()


edit->click:
save.Text="Update Store"
populateAndEdit(listbox.selectedItem)


save->click:
parseAndSave(listbox.selectedItem)
if (save.Text.Equals("Insert Store"))
   db.InsertOnSubmit(listbox.selectedItem)    
db.submit()
disableTexts()


cancel->click:
disableTexts()

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜