C# (Visual Studio 2013) - Decoupling Bulk Of Code From UI?

Status
Not open for further replies.

MajinCry

Platinum Member
Jul 28, 2015
2,495
571
136
Figured I'd give some C# a shot, since Lazarus GUI wasn't all that pretty, and because why not. I've sort of got a nice gui going, but one thing that just murders readability, is having more-than-two lines of code in a gui event-type-thing, or whatever they're called.

Me question, is how would I decouple most of me code frae the gui? In Pascal, you can just have another .pas file in the same folder, type "uses WhateverTheOtherPascalFileIsCalled" at the beginning of your code, then you just call WhateverTheFuncThisIs(), which is a function in the...err, resource file?

That's the sorta thing I want to do in Visual Studio. For a real-world example, here's me Form1.cs file. http://pastebin.com/fBK6Qj20

The main piece of code in question, is this part:

Code:
  private void checkboxDoResizeAll_CheckedChanged(object sender, EventArgs e)
  {
  if (checkboxDoResizeAll.Checked)
  {
  checkboxDoResize01.Checked = true;
  checkboxDoResize02.Checked = true;
  checkboxDoResize03.Checked = true;
  checkboxDoResize04.Checked = true;
  checkboxDoResize05.Checked = true;
  checkboxDoResize06.Checked = true;
  checkboxDoResize07.Checked = true;
  checkboxDoResize08.Checked = true;
  checkboxDoResize09.Checked = true;
  checkboxDoResize10.Checked = true;
  checkboxDoResize11.Checked = true;
  checkboxDoResize12.Checked = true;
  checkboxDoResize13.Checked = true;
  checkboxDoResize14.Checked = true;
  checkboxDoResize15.Checked = true;
  }

  else
  {
  checkboxDoResize01.Checked = false;
  checkboxDoResize02.Checked = false;
  checkboxDoResize03.Checked = false;
  checkboxDoResize04.Checked = false;
  checkboxDoResize05.Checked = false;
  checkboxDoResize06.Checked = false;
  checkboxDoResize07.Checked = false;
  checkboxDoResize08.Checked = false;
  checkboxDoResize09.Checked = false;
  checkboxDoResize10.Checked = false;
  checkboxDoResize11.Checked = false;
  checkboxDoResize12.Checked = false;
  checkboxDoResize13.Checked = false;
  checkboxDoResize14.Checked = false;
  checkboxDoResize15.Checked = false;
  }

  }


What I want to do, is put that huge arse if statement in a separate .cs file, probably called through something like myResizeAllCheck(). What do and how?


Also, the hell does "private" mean anyhow? All the programming sites 'n' google results speak in that pretentious gobble-dee-beloved patriot that requires you to know what it already is in order to know what it is. I swear, programmers are worse than the guy that designs IKEA's furniture manuals.
 
Reactions: shortylickens

Merad

Platinum Member
May 31, 2010
2,586
19
81
This is GUI code (it literally interacts directly with GUI elements) so it wouldn't make a lot of sense to separate it from the GUI. You could indeed make another file with a method that performed this action, but by the time you passed around all the references needed for the GUI elements it would be uglier than what you have now, and like I said, it wouldn't make much sense. What I would do instead is use the class constructor to create an array or list with all of the checkboxes. Then you can do a simple foreach loop over the collection and set their values to whatever you want.

Also, the hell does "private" mean anyhow?

private = Visible only inside of this class. Used for fields that hold state for your class or helper methods.
protected = Visible inside this class and any class that inherits from it. Used when you want to let the inheriting class access your state, call your helper methods, or override helper methods to change how something functions.
internal = Visible to everyone, but only inside this assembly (DLL or EXE file). Mainly used when you are creating a library (DLL) and you need a helper function or something like that that multiple classes need to use, but you don't want people who are using the library to see it.
public = Visible to everyone, everywhere.
 
Reactions: shortylickens

MajinCry

Platinum Member
Jul 28, 2015
2,495
571
136
Yikes. That terminology. I think I get the Private one; any variables defined in a private block of code, cannot be accessed in another block of code. Essentially the same as defining a variable inside a Pascal procedure/function, instead of at the beginning of the code.


Anyhow, aye, seems like C# is burdened with having awful code organization, when the gui comes into play. Shame.

I tried looking around on how to add the gui elements to an array, but Google wasn't any help. The constructor documentation is just agony to read, and didn't cover that either.

Should I just create a couple of list variables at the beginning of the Namespace in a public procedure, and then add each part of the gui manually? I'll probably be wanting to have one list for the individual checkboxes, another list for the first column of combo boxes, another list for the second, another for the third, etc, then use a
Code:
for
loop to compare the values in each combo box.

Me current state o' the code can be gotten 'ere, if that helps at all: http://www.mediafire.com/download/3z4246h7p6wmbfe/WindowsFormsApplication1_02.7z


Thanks tae the lads on StackOverflow, I've managed to get code that obtains the height and width of each .dds file. Now I need to add a loop that checks if the checkbox is ticked. If it is, it gets the height (first column of combo boxes) to check against & the same for the width (second column).

If any of the combo boxes match the width and height of the .dds file, it will look at the adjacent combo boxes (third and fourth column) to get the "destination" width and height values, which are then chucked into texconv.exe along with the .dds file, resizing it.

And 'eres how it looks thus far. http://i.imgur.com/0TCU7tk.png


Gah. Gui stuff is hard.
 

sao123

Lifer
May 27, 2002
12,648
201
106
I believe what you are actually looking for is the fundamental concepts of object oriented progrmming https://en.wikipedia.org/wiki/Object-oriented_programming AND N'Tiered layer programming... these are not unique to C#, nor are they burdensome... since they are the backbone of an entire collective set of programming languages... (JAVA, C#, C++) to name a few... https://en.wikipedia.org/wiki/List_of_object-oriented_programming_languages

in C# everything is treated as an object using classes. Complex programming is usually done in abstract layers (GUI, Logic, Data), which each are composed of 1 or many objects interacting... The GUI calls methods of a logic object, the logic object calls the methods of the data layer, etc.
(https://msdn.microsoft.com/en-us/library/ee658109.aspx) - further reading



This is the current best practice model for complex programming in the software industry, and can (and should) easily be applied to the smallest project level.
 
Reactions: shortylickens

MajinCry

Platinum Member
Jul 28, 2015
2,495
571
136
It's all greek to me, dude. Sorta like reading about those in-depth, low level graphics systems 'n' how they're implemented. E.g, robust static batching. Same sorta bemusement from my end o'er here.

In Pascal, ya could just have some procedure shoved into a separate .pas file with a couple input parameters, then in the GUI just call the function 'n' give it the variables it wants; the form, a string or two, maybe an int, that sorta thing.

But with C#, far as I've been told, you've got to have one big arse file with all yer code in it. Yuck.


Anywho, still no progress on working with the lists. Did the following at the beginning of the Namespace:

Code:
public List<CheckBox> listDoResizeCheckBox = new List<CheckBox>();
  listDoResizeCheckBox.Add(checkboxDoResize01);

But Visual Studio doesn't like that, no sir!

Error 3 'WindowsFormsApplication1.formProgramWindow.listDoResizeCheckBox' is a 'field' but is used like a 'type'

Error 4 'WindowsFormsApplication1.formProgramWindow.checkboxDoResize01' is a 'field' but is used like a 'type'
 

purbeast0

No Lifer
Sep 13, 2001
52,931
5,802
126
Based on your comments about not wanting to learn basic fundamentals of basic oo concepts, and it all being Greek without trying to understand it, I would suggest to not bother with programming. Not trying to be a dick just being blunt and honest.

If the concept of a private member seems too complicated for you to understand its going to get much more Greek very quickly.
 
Reactions: sao123

MajinCry

Platinum Member
Jul 28, 2015
2,495
571
136
Meh. It's like getting a foreign infant to learn English by givin' 'em a three hundred page essay about third party nominative non-inclusive depersonalized tenses. Ya teach a kid simple words, how ta speak 'em first. Then more complicated words. Then ya get 'em ta read in the same way, then ta write.

Ya dinnae drown the poor tyke with University end-game essays 'n' dissertations.

'Asides, seems that I've been able ta rustle up a wee doohickey o'er here, and it's chugging along fine so long as ya don't try and shove a toblerone into the saxaphone's spout.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Programming is a technical field. We use technical terms and words because they are the best suited to describing clearly what is meant. We use technical words because it would take 300 page essays if we had to break everything down into laymen terms.

Instead of saying "I don't get it" it would be easier for us to help you if you said something like: "When you say 'visible' what do you mean?". From there we can build a dialog that should help to improve your understanding.

Simply saying "I give up, this is too hard, I don't understand this stuff" is no way to program, because programming is very much based around learning and understanding new terms and methodologies.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,284
3,905
75
It sounds like you're familiar with procedural programming, but not object-oriented programming. You can usually get by with procedural programming for basic stuff in an object-oriented language, but writing programs really well takes a different mindset.

I suggest you go through a tutorial or short online class about object-oriented programming. Unfortunately, I don't know any good ones these days; maybe someone else does?
 

sao123

Lifer
May 27, 2002
12,648
201
106
Meh. It's like getting a foreign infant to learn English by givin' 'em a three hundred page essay about third party nominative non-inclusive depersonalized tenses. Ya teach a kid simple words, how ta speak 'em first. Then more complicated words. Then ya get 'em ta read in the same way, then ta write.

Ya dinnae drown the poor tyke with University end-game essays 'n' dissertations.

'Asides, seems that I've been able ta rustle up a wee doohickey o'er here, and it's chugging along fine so long as ya don't try and shove a toblerone into the saxaphone's spout.


Uh... nothing I have given you is anything more than what a HS or College Freshman would learm in a Comp Sci 100 Course...
i would no more say you could use algebra without learning how to add, multiply, and do fractions.

C# and Java are no more difficult than pascal, but there are certain fundamentals you must learn in order to be able to do anything with the language.
 

MajinCry

Platinum Member
Jul 28, 2015
2,495
571
136
Eh. Didn't have ta read any of that overly complex Ikea-grade guff, and I've a couplea good proggies with C# under me belt now.

'Em fundamentals are ta use local variables whenever possible, 'n' to make sure ya make the program in steps, completin' the first 'afore movin' to the next.
 

sao123

Lifer
May 27, 2002
12,648
201
106
Eh. Didn't have ta read any of that overly complex Ikea-grade guff, and I've a couplea good proggies with C# under me belt now.

'Em fundamentals are ta use local variables whenever possible, 'n' to make sure ya make the program in steps, completin' the first 'afore movin' to the next.

Good is debatable.
Considering how you have bastardized the Kings English into something half Scottish/Irish, half Piratese... I can almost guarantee you have bastardized the C# OOP paradigm by writing in C# with a Spaghetti code pseudo-style. How many times were you tempted to use the GOTO statement?

If you really are wanting to learn the proper techniques, there are plenty of resources around here, including myself. I was, after all, a computer science instructor at Penn State for a number of semesters prior to the current job I have now.

As for going head strong into the wind, determined to continue doing things the uninformed (and potentially wrong) way, I'd be forced to agree with Ken G6. Unless you learn some fundamentals of Object oriented programming, there's not much help around here for that.
 

MajinCry

Platinum Member
Jul 28, 2015
2,495
571
136
Da hell is Goto? Ain't that some old command prompt stuff? Real bruvs use functions 'n' procedures, frae Assembly ta C#.

'Ere's me first project. https://github.com/MajinCry/Fallout-4-Texture-Resizer

AND IT RUDDY WELL WORKS! NO MATTER WHAT THE MATRONS SAY!

Like, bruh, when there's a ruddy technical essay usin' veteran programmy-not-made-for-humans-that-speak-normal-English English, ain't nothing good about it. Simple terms are best.

'Twould be like explaining the fundamentals of Spanish, to someone completely alien tae the language, by rattling off the Spanish history frae the fall of the Roman Empire, entirely in Spanish and making use of words that nobody ever uses. Like subdermatoglyphic. Or floccinaucinihilipilification.

Ain't nobody got time fer that.
 

nakedfrog

No Lifer
Apr 3, 2001
58,570
12,872
136
I understand your feelings about it, and by just whacking away at it you can get it to do eventually what you want, in the same way you can use a screwdriver to pound nails in if you work at it. It took me a while before I had a solid grasp of how and why you do the things you do in object-oriented .Net code.
 
Reactions: sao123 and Ken g6

MajinCry

Platinum Member
Jul 28, 2015
2,495
571
136
It's not like that. Well, it was during me first stint with Pascal, but all you got ta do is plan things out, break it down inta steps; the goal of the program, the source data you have to work with, and the data you want it to punt out.

Then break it down into wee steps, and google up on functions that can do what ya need done. If that fails, ask around.

But reading some convoluted essay written by some guy who wrote it for industry veterans, from the point of view o' the same sort o' lad, isn't going ta do a damn thing 'cept make yer eyes glaze o'er.
 

purbeast0

No Lifer
Sep 13, 2001
52,931
5,802
126
Da hell is Goto? Ain't that some old command prompt stuff? Real bruvs use functions 'n' procedures, frae Assembly ta C#.

'Ere's me first project. https://github.com/MajinCry/Fallout-4-Texture-Resizer

AND IT RUDDY WELL WORKS! NO MATTER WHAT THE MATRONS SAY!

Like, bruh, when there's a ruddy technical essay usin' veteran programmy-not-made-for-humans-that-speak-normal-English English, ain't nothing good about it. Simple terms are best.

'Twould be like explaining the fundamentals of Spanish, to someone completely alien tae the language, by rattling off the Spanish history frae the fall of the Roman Empire, entirely in Spanish and making use of words that nobody ever uses. Like subdermatoglyphic. Or floccinaucinihilipilification.

Ain't nobody got time fer that.

https://github.com/MajinCry/Fallout...er/WindowsFormsApplication1/Form1.Designer.cs

As mentioned, you can get things to work for you if you keep pounding away at it. But that right there is a prime example of bad programming practices. I won't bother explaining why since you don't want to learn anyways.
 

nakedfrog

No Lifer
Apr 3, 2001
58,570
12,872
136
Ouch. That is painful. Just as a fer-example...

Code:
this.comboboxSourceXRes02.FormattingEnabled = true;
this.comboboxSourceXRes02.Items.AddRange(new object[] {
"16384",
"8192",
"4096",
"2048",
"1024",
"512",
"256",
"128",
"64",
"32",
"16",
"8",
"4",
"2",
"1"});
this.comboboxSourceXRes02.Location = new System.Drawing.Point(40, 47);
this.comboboxSourceXRes02.Name = "comboboxSourceXRes02";
this.comboboxSourceXRes02.Size = new System.Drawing.Size(59, 21);
this.comboboxSourceXRes02.TabIndex = 4;
this.comboboxSourceXRes02.Text = "8192";

Instead of having this copy-pasta'd umpteen times, you could have written it once, inside a function that takes in a ByRef argument of the type System.Windows.Forms.ComboBox, and then just does all those things to the passed-in object.
Of course, I'm not sure why all this stuff is being done in the code-behind instead of in the form itself.
 

purbeast0

No Lifer
Sep 13, 2001
52,931
5,802
126
The lack of constants in that file is also mind boggling (for people who understand good programming practices that is).
 
Reactions: Ken g6

nakedfrog

No Lifer
Apr 3, 2001
58,570
12,872
136
D'oh. That's what I get for thinking before I've finished my second cup of coffee, should have caught the .designer in there.
 

Scooby Doo

Golden Member
Sep 1, 2006
1,040
18
81
That file was written by Visual Studio designer, it's done automatically as you. Making changes to it is kinda hopeless unless you don't wan to the use GUI Designer/Builder otherwise it get automatically updated. Also, that file should be coupled with the GUI, it is the GUI in code form.
 

cytg111

Lifer
Mar 17, 2008
23,551
13,116
136
For seperation in C# I really liked XAML and WPF using the MVVM pattern. Clear and Cut. And the XAML guys really have thought of about everything.
 

sao123

Lifer
May 27, 2002
12,648
201
106
Separation is as simple as making a new class and then declaring an object of that class and calling its member function.

But apparently since the OP can't be bothered with the convolution of learning object oriented programming concepts, I guess 2 lines of code is more than he can handle.
 
Status
Not open for further replies.
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |