I have a simple programme that allows input of details about a job and assign dates. Im using the project to learn c# and believe I have come a long way.
I am trying to achieve a user friendly date range input system. and because I'm using the experience to learn the programme I have decided to produce a class that draws a calender on forms when needed.
The class works very well for its initial requirements. It takes in an object (the form normally) and a point. The class then searched the database for any dates within a given range (starting with today() but can be manipulated by the form) and shows a month style block of boxes with the date and any information contained with the database.
But now I want to go the next step, and allow people to click on a specific date to select it and for a specific piece of code to be fired off. .. the only issue is! :
How do I programme a click event from the form, related to a label/drawn box that isn't there at design time...
since the labels are dynamically created at runtime I don't yet have a way to directly assign to them. I could recode the class for this, but would rather not if another way is possible.
Ideally im looking for a solution (if one exists) where when the label is clicked a method is passed the label.text. Could anyone point me in the right direction as to how to code the click event for anyone of the 50 labels that isn't created until run time?
Thanks for time to read, and even more thanks to those that reply.
Kind Regards
Paul
this.Controls["myDynamicallyCreatedControl"].Click
+= new EventHandler(MyEventHandlerMethod_Click);
private void MyEventHandlerMethod_Click(object sender, EventArgs e)
{
// Handle click event here.
}
Related
I want to make a application let me show how it works.
PRODUCT
STOCK
PRICE
PHONE
2000
10
TEA
3000
15
CHICKEN
3000
20
I'll write textbox1.text to 100 for example, the app gonna tell me you need 5 chickens for textbox1.text's value or 10 phones or 6 teas and 1 phone for let it equal 100.
I need to start search but i don't know where to start and keywords. I want to make a smart calculator (?)
Can you guys show me the way or algorithm ?
I'm pretty beginner at coding please be nice :)
I am trying to build a smart calculator and then looking for algorithm or pseudo code.
I've look for some giving changes tutorials, but i still dont know where to start. I'm still looking for it.
I guess that you're working in Windows Forms, so you can bind your textbox event OnTextChanged to method that will automatically calculate things you need.
How to bind: select your textbox, then click "events" in right bottom of visual studio, place the mouse in the field right of "TextChanged", and click 2 times. A private void textBox1_TextChanged(object sender, EventArgs e) will automatically created at the Form1.cs file. Here you can write your logic to calculate things.
I'm currently programming Hangman in C# using windows form and I've got a fair way through it. I'm using an array to store the words that can be guessed - a bit limited I know, but I don't know much about databases. I use a random number generator to search through the array to select a word.
Is there a way to pass parameters to different methods within the SAME windows form?? So far, when I create a parameter inside a button click method the design form disappears and says I cant edit it. I have different methods set up which consist of button clicks, and I would need to send each method the selected word to check whether it had any of those specific letters in it.
Any help appreciated!!!
You don't need to pass extra parameters to click handlers. Just add a private field to form which will contain selected word and every method in the form will have access to it.
How do I display Price value($) in textbox next to a label when user checks the radio button, select one of item from listBox, and then clicks the button?
Is there way to do it on the form1.cs[Design] using the properties? Or do I need to set them up in code level?
listBox item example
EDIT: Solved this in Code-level.
I don't think you can do it without any code.
Also I'd recommend you to check out this page - just a few simple rules can make your chances to get a good answer on this site much better.
And there is not much of coding needed to solve your problem. Take a look on the Events tab in Property view in form designer. A few event handlers to process user input and some fields inside your class to store the data - I assume it is not some serious business app you're dealing with, so all the code you need for this to work would be like 20 lines tops -)
I'm relatively new to C# and Windows forms so excuse me for what may seem like an easy question.
I have a windows application which has a dialog box which when opened contains textboxes with some default values. These can be changed depending on what the user wants to use. The values in this dialog box together with the content on the main form are then used to create an XML file. The issue I have is if I go to open the dialog box again to change any values in the same session, the original values are there and not any of the new values.
How do I get it to keep the values that have been changed in that particular session?
If you want to see the user's values the next time you open the dialog box, you'll need to save those values somewhere, and then re-load them the next time the dialog is displayed (usually on Form_Load or Form_Show). And of course you'll need to save the values (probably in Form_Close?) before exiting.
Where you save those values is up to you. You can save them in some static variables in the form class if you want it to be just for that run of the program. Or you can store in a configuration file, the registry, isolated storage, etc. if you want to re-load those settings the next time the program is run.
If I'm understanding the question correctly it sounds like you need to make use of background variables and TextChanged events (although I prefer KeyDown events and my code uses that instead). For instance, let's call your textbox TextBox1. You can then make a global variable called string Temp and use it like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
temp = textBox1.Text;
}
Once your dialog box is closed you can use that variable elsewhere, in your case it sounds like you want to send it to an XML.
Another option is to use the keydown event to have a temporary XML file that retains the value of your text. This is obviously more computationally expensive but it's not really that big of a deal unless this is going to be used in a processor limited environment.
The last thing I'd mention is that you may run into trouble if you're using multi-threading and passing the value of that temp value. Look into using variables on other threads than you started with for help with that.
If you want to keep the values the user entered the last time he used your dialog, you need to keep a reference to your dialog somewhere.
Also if you set some data in your dialog on the Load event it might erase the data previously entered by the user. Without seeing your code I can't tell more at this point.
I am creating an interface for an XNA game and can't seem to figure something out. I'm new to programming, and feel like I have to be missing something obvious.
I'm creating a grid of levels, much like something you'd see in Angry Birds.
The number of levels will be variable, so I don't want to statically program them.
All of the buttons I use for level icons are created dynamically at runtime, based on a list of level objects. As I create the buttons I set up all of the click events to point to one method that is supposed to determine which button they clicked on, and load that specific level.
My problem is I can't seem to figure out a reliable way to actually tell which button they clicked on and associated that with one of my level objects in the list. I feel like I must be missing something extremely obvious.
Things I've tried so far:
As I generate the buttons dynamically I add them as children to a grid. So I tried using the index number of the sender as the index number in my list of levels (because they should both have the same number of elements).
For example:
App.CurrentLevel = PuzzleLevelsGrid.Children.IndexOf(sender as Button);
This worked great the first time I navigate to the level picking screen, but whenever I come back to it the children of my grid gets reset to a count of 0 for some reason, so it breaks down.
I've set break points and I can't explain how it gets set to 0. I load the children in my onNavigatedTo(), and sometime between the end of that and me pressing a button to load a level it gets wiped.
The other thing I tried was setting up a button object inside my actual level object, then when dynamically creating the level buttons I actually make changes to the button property in the appropriate level.
Then when I need to find out which button was the sender I just loop through all levels and match the sender to the button property. This method actually worked pretty well... until I started trying to load my levels using a background worker thread. The worker thread can't deal with the Button because it's a UI thread thing, and crashes.
Like I said, I'm a new programmer, so I welcome any and all feedback.
Thanks in advance.
The button, like almost every UI control, has a "Tag" property. This property has been designed for you, and only for you, so that you can put any value you like to identify the control.
For instance, you can put your level object in the Tag property of the button, then just read this value back in the click event.