Dynamically adding buttons and adding their source code - c#

Greeting,
I have already checked for my question with no luck of an answer or something similar.
So here it's my problem:
I have created a cafe cashier software for my boss that it has buttons for each product that cashier wants to add on a bill/table. If a button is clicked, for example btnOrangeJuice, on its click event, it call a function for example named: addOrder("Orange Juice");
Inside this function it does the simple follow code:
a) it connect to the database
b) returns price of product "Orange Juice" for example (select price from products where product_name = '" + product_parameter + "'")
c) take the result, and add it to the order list box with it's price and so on..
The problem here is, (lets go with the previews example) every time we have a new product, I have to manually customize the form by adding a button 52x52 pixels, name btnOrangeJuice, text name "Orange Juice", and on the click event to call my function addOrder("Orange Juice"), then upload the new version of my software to my online web hosting, and update the software from the cashier's pc. This is time consuming for me, especially when we make new deals with suppliers so i wanted to make my program to add dynamically buttons, to do all this by just having a form, type in the button properties, calling a function, and add a button by its self with its dimensions, check if its out of form space, if it is, the add the button bellow instead on the right, and of course add the button properties and its click event (that call my built-in function).
If you need any farther details regarding my software/problem please do ask.
So can you assist me on this please?

The standard approach is to assign each product a unique identifier and have one mechanism for adding an order which takes the unique identifier as the parameter. Your products already presumably have a unique identifier as they are persisted entities (a database primary key for example).
If you wish the add button to have the text of the product, then you can dynamically set this from the product name, which is again already stored in the database. There is no need to replicate this information in your UI code.

Related

How to add image according to a condition on SQL table

I'm working on a personal project
I have a windows form c# where I add people with a field where I choose the gender and name of the person.
When I click on the button, it adds a person to the table person of human database.
Then on another form I want to display the panel of all the people present in the database.
Display of the persons will be represented by a picture of male/female symbol
I have a person table, with name and gender attributes. And I would like that each time I add a person, automatically according to the gender of the person, a picture of symbol of a woman or a man is added to the table.
The final goal, like I said it before, is to display in a separate picturebox (Windows form C#) each of the persons in the table with their appropriate image(according to gender).
And I'll add a hover option on each picturebox thats shows name of the person when you use your mouse (I dont need help here, just to let you know how it will works).
I don't know either, if it's the right way to approach this problem or if the solution is more on the c# side with a condition on the image display (it seems much more complex to me).
Do I need to create another SQL table named picture and I link it with person but here again, I don't know how i can implement the image condition between picture and person table and make it works with C# form
SQL script:
CREATE TABLE persons(
id INT(100) auto increment, PRIMARY KEY,
name VARCHAR(1000),
gender VARCHAR(7)
);
Here arepictures of how it will looks...
Add form
Panel
I hope I made myself clear, thanks!
If the images are static i.e. They don't change when the user change and all males have the same picture and all the females have the same picture
I think it is not a good idea to put the image in the database. From the C# side you can check if the user is a male or a female and load it from some resources folder in your C# project.
Let me know if I got something wrong with your thoughts.

LightSwitch - Query based on a SelectedItem from another table

I have two tables, Customer and Address. One customer can have one or more addresses.
My view is a ListDetail with all my customers on the left as a list and the edition on the right.
Under the edition I have the "address area" with a list of addresses and an edition on the selected.
My problem is all my addresses are listed. I just want addresses with the matching customerId (selected on the first list).
Here is a drawing to help you see what I am talking about:
I can create a button on the first list that show a popup with the selected Id but I don't know how to put a parameter on my address collection.
Please tell me if you need more details.
Edit : A good example of what I want is the 'Roles' view created by default. I haven't found how to edit this view to see how it works but if you select a 'role' the list of users is updated to show only those that have this role.
If your two tables are related (meaning you've created a relationship between them in the table designer), then what you describe should happen automatically. Using the Add Screen wizard, you can tick the "related data" checkboxes for any related tables that you want to display for the selected item.
If you didn't tick the checkbox for a table, you can still drag the navigation property (created when you added the relationship), which is on the left side of the screen designer (with a + next to it).
To do it manually, you need to create a modeled query (a query based on a table, or on another query), to which you add an integer parameter, then add a filter based on that parameter.

MVC 4 Submit button in each row of a "grid" - how to?

How can I have a submit button in each row of a "grid", where, when that button is clicked, I can POST three pieces of data in the row to the controller?
Here's the scenario:
A screen shows my system's users in an html table "grid". One link in each row says "Associate Customer". This goes to another screen showing a list of customers that this user can be associated with. Their UserID is in the URL like AssociateCustomer/14
On the second screen, I want to display in an html grid, CustomerID, CustomerName, and a link/button that either:
Says "Associate" if they aren't associated with that customer, or
Says "Disassociate" if they are already associated with that customer
I got that much working, but I don't know the next part: when the user clicks a button on the grid, I need to pass CustomerID, the UserID, and "Associate/Disassociate" to the controller. How can I do that in an MVC way?
Something like:
# Html.ActionLink("Click Me!","Associate","SomeController",new{userId=item.UserID,customerID=item.CustomerID})
You don't have to post the form to do what you are asking (though that would be one way to do it).
Generally you won't post the form in this case.
I would create Associate and Disassociate actions in your controller that accept the parameters you require as querystrings. Then build an ActionLink for each row of your grid that builds that appropriate URL.

How to create a dynamic form for adding a collection of data?

To put simple:
I have a database with two tables...
I use a datamodel to connect to the database
I create two classes which store my methods and properties for the two tables
I create one form for gathering information on table 1 and click next (hyperlinks to another page)
I pass the table 1's ID through and then I have a problem. The data submitted for table 2 is inter-changeable meaning it will vary how much data will be submitted for table 2 based on the id of table
My question is, how can I create a dynamic webform which will allow users to click an add button and it adds another textbox or a copy of the one that is currently on the page? I then want to put them into a list and then send them off to the table 1 database using an insert method from the datamodel.
To make it simple for point [5] say you have a book, name, author, publisher etc. That could be an example for table 1. But for table 2 I then want the user to put each page number into seperate textboxes... Now i know this is a bad example but am trying to get the point accross in that you couldn't just have a given set of textboxes static to the page because each book has different amount of pages.
Any examples would get great.
use a reapeater control and put a place holder in the item template and bind the ids from table1. Use Itemdatabound event to capture each row and add the necessary number of controls (using loadcontrol or if its just textbox then create an instance of it) from table2 and add it to the place holder.

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 create 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!

Categories