Passing parameters to button click methods within the SAME windows form - c#

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.

Related

C# Windows Forms

I am new here and startig to learn C#. Before I programed in C and Windows.Forms in VBA.
Now I have a Project where I receive serial port data from a device, that sends the contents of various C-structs (some nested). I want to display these Datasets in Windows.Forms each labeled, edit the values and send it back to the device. So it can easily copy the received data into its structures.
Now I don't know an easy way to get the structured data inside a c#-byte-list into various text boxes(?) that have describing labels to it. I would like to design the layout in Form Designer, so not want to create the controls dynamically. But maybe this is the best way?
Does someone know a good strategy / a best suited control to achieve this?
Thanks in advance.
You could use a DataGridView with control buttons inside each row or external buttons that handle selected row(s). In order to see and change the details a corresponding dialog form comes in handy in which I would implement a hex editor with the byte/hex values on the left and the readable value on the right.
To display the byte data just convert it to a hexadecimal string so you can display it in one column inside the DataGridView.
For the nesting part, you could either implement child elements inside the DataGridView itself (you need to either code this yourself or use some 3rd party extensions like this or Telerik, e.g., which also has the possibility of including button columns) or open a conditional dialog form which again shows a DataGridView for the child elements and so on (so you would open the same form as child form ad infinitum).
Thereby you are as flexible as possible and do not rely on fixed text box elements which you have to add or extend on further information.

Populating C# combobox from SQL and restrict results based on choice

I am binding some data from a SQL table into a C# ComboBox. What I want to achieve is the following:
Say we have 3 tables in SQL
Room (Bedroom, Bathroom, Hall)
Stuff (Bed, Bath, Wardrobe)
StuffProperty (DoubleBed, HotRunningWater, Clothes).
My goal is to restrict the second and the third ComboBox values, based on the choice of the first ComboBox, i.e:
The first ComboBox contains Bedroom, Bathroom, Hall. If the user chooses Bathroom, the second ComboBox contains only Bath and not Bed or Wardrobe and the third ComboBox only contains HotRunningWater, not DoubleBed or Clothes
How can I send a query back to SQL without pushing a button, 'live'?
Also, should I use Stored procedures to map Room, Stuff and StuffProperty values together and invoke them in C# code (this is what I would prefer)? Or should I write the whole filtering stuff in C# (for example using switches)?
Really clueless on this one and not even sure if there's any way to 'inform' the SQL of a recent choice without pushing a button. The columns I used are only to illustrate the problem.
Also, I'm aware this question has been asked here a couple of times, but all I found were VB code and I need it to be sorted with a Stored Procedure and C#, or C# only.
You would need to hook onto an event of the combobox, most likely selectedIndexChanged or similar. This is how you get the "live" effect. You would need to hook onto the event of the first combobox and then populate the second.
Then in the event of the second you need to populate the third.
How you go about this using metro wizard I am not to sure but hopefully with a gameplan you can find the required code.

Retain values in dialog box after closing - windows forms c#

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.

How can I handle text boxes that are created by the client/javascript, on the server

I have a textbox in a form that collects a name. If the client decides he/she wants to add more names they would click add more and another textbox would appear.
If the client created 15 text boxes how can I work with their values on the server? Let's say that I name the first textbox, txtbox1, and each additional textbox would have an incrementing number.
Could I use the same naming convention that Asp.net assigns, figure out how many textboxes are on the page, and loop through them, or would they have to be initially rendered by the framework to begin with?
The HttpContext.Current.Request.Params have the parameters of all submited values, even those you created using javascript, as long as the value of the text box is sent from the client to the server of course... I mean, you need to add the client-created textbox to the main form element being submited.
You can make a naming convention yourself, but take care not to collide with Asp.Net conventions as that could be trouble to you... lets say: MyTextBox_1, MyTextBox_2.
You can then iterate the HttpContext.Current.Request.Params, and see wich keys start with "MyTextBox_" and read all of them.
EDIT: better yet you can use HttpContext.Current.Request.Form, because this contains only params sent submited in the form.

c# sending a click event from a run-time drawn object

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.
}

Categories