Hello my babies! So, I've been helping some friends with a few things relating to C# and I learned something new myself! And now I want to share it with you all 🙂
So, let's say you have multiple numericUpDown boxes (this can be done with anything, but numericUpDown's are easier to demonstrate) Now, most people think you need to have a seperate private void for each and every numericUpDown to make it all work correctly. Well what I am about to show you will compact your coding for any number of these boxes into a single private void!! and it's super easy!!
[hide]
Alright, so we need to start by making your numericUpDown boxes
Yeah, that looks good. Nice and neat
Now we get to the coding portion 😛 Best part of all this is that you don't need any special .dll or nothing like that!
uint[] Offsets = new uint[] {//offsets will go here and need to reflect each of your numerical boxes in the order of which they were created!! private void NumericBoxes(object sender, EventArgs e) { var Das_Box = (NumericUpDown)sender; int select = Convert.ToInt16(Das_Box.Name.Split('_')[1]);//this identifies which box we are using PS3.Extension.WriteInt32(Offsets[select], Das_Box.Value); } //This will also work with Xbox using my MultiLib :D
Now, we need to associate void to all of our boxes. that too is super easy!
Select all of your boxes, then go to your Event tab under Properties. Find the Value Changed option (It'll be different if you are using a button or something, for buttons you'll want to use Click, textboxes Text Changed etc.) click the drop down arrow and select the private void we built. That's it!
Ok, this part is important. We will be using the string Split() function to grab the numerical value that identifies each box. The SMART way to do this is to change the name of each box as you are building it. but things happen, best laid plans etc. So here is an easy method 😉
Go into the Form1.Designer hold Control(Ctrl) and hit 'H' this will bring up your Replace dialog. Now, the pneumonics of VS just adds a single digit to the end of the name, so whatever the name is, put "objectname in the top and then "objectname_ in the bottom. We are adding the '_' to each and every object name. notice in our private void, the Split is designed to look for the '_' 😉
Alright, so something else to note. the uint[] that we built needs to be done in a certain way. it has to match sequentially with the numerical value of our objects. so if your first numericUpDown (which is now labeled numericUpDown_0) is for character health, then the first uint in that array MUST be the offset for character health.
This happens because of our int select. it is grabbing the number on our object name and using it as a pointer in the uint[]. Hope that makes sense haha But either way, there are LOADS of different things you can do with this method, and it should help keep your code nice and clutter free, plus updating and troubleshooting is gonna be a breeze!
[/hide]
Hope you all enjoyed!
And as always, Happy Modding 😉