How can we access dynamically created controls in C# windows application? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am designing C# windows application using 3-Tier architecture, Basically i am making management system for super market in which i have created a form for generating BILL. I have used FlowLayout panel for generating 4 ComboBoxes, 3 TextBoxes and 2 Numeric UpDown respectively to hold Bill values accordingly. I have supplied add button with which we can create these controls dynamically on each button click, so, for example, when user clicks on the add button to add new item, new row will be generated with 4 CBs, 3 TBs and 2 NUpDowns. Having mentioned the scenario, I have following queries:
How can i access each and every of the control in each row in turn? So, for example, I want to access 3rd ComboBox of 5th row, How can I access that in particular.
I am using 3-tier application architecture to design the application. I have placed their functions in BusinessLogicLayer and have called them in UI in button event. Am i alright with this approach?
I want each ComboBox, in each row, to be connected with the first one. for example first one indicates main category when user selects anything from that, second combo box(sub category) should show items connected with the first one and so on. How can i do that?
Thanks

I don't know the very architecture of your application, but let me try and help you. Assumed you have some sort of Customer, Product and Order objects for your business logic the workflow could be the following
void ButtonFindCustomer_Click(object sender, EventArgs e)
{
m_order.Customer = Customer.Find(TextBoxFirst.Text, TextBoxLast.Text, TextBoxCustomerID.Text);
}
void ButtonAddProduct_Click(object sender, Event args)
{
m_order.AddArticle(Product.Find(TextBoxArticleNumber.Text), NumericUpDownArticleAmount.Value);
}
void ButtonSubmit_Click(object sender, EventArgs e)
{
m_order.Place();
}
I used the prefix for the member variable here just for clarity, for the lack on context. In your real code you should avoid it. Furthermore I have omitted the check if the user exists in ButtonFindCustomer_Click, this case should be handled, too. In ButtonAddProduct_Click the case a product is added is handled. Again the existence of the product is assumed, thus you'll have to introduce some error handling here, for example by using something like
if(Product.Exists(productNumber))
{
// add to order
}
else
{
// emit error message
}
The actual transaction is performed in the ButtonSubmit_Click handler. The order is validated and then sent to the data access layer (for example written to a SQL-Database). Once again the error handling is missing, please keep that in mind.
To get to your actual questions:
1) You'll somehow have to keep track the controls you created. If you are always creating them in the same groups, you should consider a user control, which avoids keeping track of which controls belong together. The control could - for example - contain a drop down box for categories, a drop down box for products and a numeric up down for the amount.
class ProductSelector : UserControl
{
... //add controls to user control
public event EventHandler CategoryChanged;
public event EventHandler ProductChanged;
...
public void PopulateCategories(list<string> names, list<string> ids)
{
...
}
public void PopulateProducts(list<string> names, list<string ids)
{
...
}
}
Now any time anything about the product is changed, you'll receive an event and can directly access your user control and all necessary data (if you wrote the functions in ProductSelector). If you want to access the controls in turn, you can either get all controls of type ProductSelector from MainForm.Controls or create a list of all ProductControls you added.
2) Yes, I think you'll be alright with this approach. Somewhere you'll have to access the functions of your BLL and UI event handlers are a good starting point for.
3) Please see me answer to 1).
I hope my explanation met your requirements and answered you questions. Feel free to ask, if I may help you any further.

Related

Display data on same page or separate page? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm working on my first mobile app. I'm getting input from the User and having them click a button before retrieving data into a dataset. All that is working well but getting the data displayed has stopped me in my tracks. Can/Should I display it in the same page or create a 2nd one? How do I call a second page from my c# code? I don't know best practices for Xamarin and mobile so any assistance or direction would be helpful.
There are multiple ways of doing this and reasons for doing it, it all depends on your app. I assume you're not using xamarin forms, so I'll give default android/iOS answers.
Can/Should I display it in the same page?
If you're making the usual search control it helps displaying the search results in the same page as the one you are typing in.
If you have a list of items, where selecting one shows more details, then the usual way of doing it is to navigate to a new page that displays the information. So you'll have to create another page.
There are multiple ways of doing this it all depends on your app.
How do I call a second page from my c# code?
You don't call the second page from your first page, you rather launch the page and pass the information you want.
Android: You'll create a new Activity with it's layout file and use an intent to launch the activity from your Action (clicking a button in your case).
You can use this tutorial to get up to speed with miltiscreen Xamarin applications for android. Your code should look similar to the following.
Button yourButton = FindViewById<Button> (Resource.Id.YourButtonId);
yourButton.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(SecondActivity));
StartActivity(intent);
};
To pass information to the second page you can use one of the intent.PutExtra() methods. Similar to
intent.PutStringArrayListExtra("YourKey", myList);
Which passes a list to the launching activity through the intent, storing the value under YourKey. In your SecondActivity you can get the data from your intent using
intent.GetExtra("YourKey")
iOS: Not much code involved so I'll list the steps to do this briefly. You can use this tutorial to get started with multiscreen iOS applications.
You'll create a new controller in your Main storyboard, using Interface Builder or Xamarin Studio.
On your first view, place your button.
press Ctrl and drag it to the new ViewController. On releasing your click select Show (push) segue.
In your FirstViewController override prepareForSegue and set the data you want to use in your SecondViewController.
Something like below.
public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue (segue, sender);
// set the View Controller that’s powering the screen we’re
// transitioning to
var secondViewController = segue.DestinationViewController as SecondViewController;
secondViewController.YourData = "Your data you want to pass over";
}
Let me know if this answers your question. Goodluck.

Reload/Refresh data from DB periodically using entity framework 5 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using entity framework 5 on my winform application. I have a datagridView on my form which contains data from my database:
public partial class Form1 : Form
{
etudiantEntities cont = new etudiantEntities();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cont.etudiant1.Load();
etudiant1DataGridView.DataSource = cont.etudiant1.Local;
}
Right now, every thing is perfect.
Now, i want to reload data when there is an update happened in other forms. I want to reload it periodically.
Is there a way to do that with entity framework?
Thank you!
You have like five problems at once. Writing a proper answer would require a book (which I would suggest you to go read anyway), so this answer is a summary you can use to learn more.
First, you need to elaborate on "when there is an update happen[ing] in other forms". You need to detect this change. How to do that, depends on how that form works. Hopefully it does using data binding and INotifyPropertyChanged, see Raise an event whenever a property's value changed?.
Then on these "other forms", you subscribe to their model's PropertyChanged event and propagate that as an event on each form. Be sure to unsubscribe when appropriate as well. In the PropertyChanged event handler of your form, you raise an event that's specific to that form, like MyModelChanged.
Now you have a form that can notify interested parties of events, by subscribing to that event.
Something like this:
var yourEditForm = new YourEditForm();
yourEditForm.MyModelChanged += this.YourEditForm_MyModelChanged;
yourEditForm.Show();
Now where you place this code is pretty crucial. When working with multiple forms you want to communicate with each other, you need some kind of "controller" (or give it a name) that knows about all forms and their events that are relevant to your application, and ties it all together.
So in your controller you now have the above code and this event handler:
private void YourEditForm_MyModelChanged(object sender, EventArgs e)
{
}
Now in that event handler, you can let your aptly named Form1 reload its data. You can do so by exposing a public method that does just that:
public void RefreshGrid()
{
cont.etudiant1.Load();
etudiant1DataGridView.DataSource = cont.etudiant1.Local;
}
There's your "refresh". You can call form1.RefreshGrid() in the event handler shown above.
Note that all of this is pretty much hacked together. Go read a tutorial or two about data binding in WinForms to let this properly be handled, because doing it manually is going to be a pain to maintain.
You can start by reading Data Binding and Windows Forms and Change Notification in Windows Forms Data Binding on MSDN.

How to implement TreeMaps DrillDown on Button Click?

I am using the treemaps for an application development. The problem I am facing is that I want to use a button to remove the level I have traversed. I have tried but there is no use. I am new to WPF programming and I am not sure about all the methods that can be used. I have put a button and I have been able to retrieve the level number and reduce the level number onClick. but the level is the same. I used drilldown property for the tree maps. But I am not sure of implementing it by use of a button. Sample is mostly preferred as a beginner. Hope for a reply sooner.
Scenario:-
I was using a squarified treemap for visualising a data that consisted of 4 levels. I drillDown the map to view certain details. For example, I have a world map that shows the continents separately. I have clicked on any of the continent, say Asia, I will get another level that shows the countries as a treemap level and further clicking on the country leads me to the cities or states. Like wise I can move backwards without an problem. Now, I am using a button to remove the levels I am currently located in. I am not able to do so and could you please help me with a code snippet for the button
I am partially there, but a detailed research will help me. It took a long time for me to settle in this.
private void Back_Click(object sender, RoutedEventArgs e)
{
int level = TreeMap.Levels.Count;
if (level >= 1)
{
TreeMap.Levels.RemoveAt(level-1);
}
}
This code can only be used to change the level, not the visual of the same level. It will not get you back to the desired screen, but you can notice the level change once you debug and use Watch.

Can ASP.Net Drop Down List Control Be Used to Navigate to Another Page in Same Site

For an outside of the box idea on a particular site I'm building, I would like to use a drop down list control, which has two inputs: Sponsor1 and Sponsor2. My goal is that when a visitor chooses one of the options, they will go a page for that particular sponsor, and enter a pre-defined code via a textbox w/ a "Next" button, which will take them to yet another page to enter more info.
I'm sort of dividing the site up to have branches, as the sponsors will have visitors (i.e., customers) and the sponsors can keep track of these visitors via the input that will go to a database yet to be made.
As I'm creating this in Asp.Net/C#, I cannot find any examples of this being done, outside an old reference being done with JavaScript - yet the end concept is not the same:
Creating a drop-down list that links to other pages
So is this possible to code something in C# within the code-behind to make this behave as I wish, or must I scrap this idea and just do it another way? Thanks to all in advance!!
Have you tried calling an event when the user selects something on the drop-down list?
Edit: Added more context to where everything goes.
protected override void OnInit(EventArgs)
{
dropDownList.selectedIndexChanged += new EventHandler(ddlIndexChanged);
base.OnInit(ea);
}
//Your Page_Load Here
private void ddlIndexChanged(object sender, EventArgs ea)
{
//This is called when the index is changed, you could redirect here
}

ASP.NET - How to check if one or more field values have been changed

I have a web form where I register a new employee. There're 3 parts in the form: Personal info, Address info, Special Status. But there's only one button for the whole form. When I submit the form all the information is updated to the database. So three Update statements are executed against the database. The methods are UpdatePersonalInfo, UpdateAddressInfo and UpdateSpStatus. Is there a way to check if there's been a change in any field in the certain part and run update method only if it's true. So something like this:
if (There's been any change to the personal data of the employee)
{
UpdatePersonalInfo;
}
if (There's been any change to the address information of the employee)
{
UpdateAddressInfo;
}
Sure I know, I can save all the previous values in a session object in PageLoad and then compare them one by one before running the method. But I thought maybe there's a magic way of doing this more easily.
Not sure that this is a better solution than any of the alternatives you already mentioned, but you could create a default handler to attach to the TextChanged, SelectedIndexChanged, etc events of your controls to keep track of which ones have changed.
List ChangedControls = new List(Of, String);
private void ChangedValue(object sender, System.EventArgs e) {
WebControl cntrl = (WebControl) sender;
ChangedControls.Add(cntrl.ID);
}
Then on your button click scour the ChangedControls list for the relevant controls.

Categories

Resources