Reputation: 333
I'm new to tinkering with C#, and so far that's the extent of it. I'm just tinkering for a small project I have.
The redundancy is driving me crazy, though. Writing long lists of similar code taking up line after line just doesn't sit well with me.
I have a few tabs with checkboxes and radio buttons in them, that's where I noticed the duplication the most. Unfortunately I... don't quite grasp every aspect of C# as well as I should, yet. So I was hoping to learn from you folks.
Example:
//setup checkbox
checkBox1.AutoSize = true;
checkBox1.Checked = false;
checkBox1.CheckState = CheckState.Unchecked;
checkBox1.Location = new Point(5, 102);
checkBox1.Text = "Check Box 1!";
checkBox1.UseVisualStyleBackColor = true;
//set radio 1
radio1.AutoSize = true;
radio1.Checked = true;
radio1.Location = new Point(5, 33);
radio1.Size = new Size(20, 20);
radio1.Text = "Radio-e-o-e-o";
radio1.UseVisualStyleBackColor = true;
//set radio 2
radio2.AutoSize = true;
radio2.Checked = false;
radio2.Location = new Point(5, 56);
radio2.Size = new Size(18, 20);
radio2.Text = "Option 2!";
radio2.UseVisualStyleBackColor = true;
My instinct would be to set up some arrays with the variable data for things like the names and the distinct data. But as I said, I'm very new, I've only been tinkering... and the resources I've come across tend to either not match what I'm looking for, or add layers of complexity I'm probably not ready for.
Upvotes: 3
Views: 1647
Reputation: 14122
If I got your question right: you can make a method:
private CheckBox DoSomethingWith(CheckBox checkBox, Point location, string text)
{
checkBox.AutoSize = true;
checkBox.Checked = false;
checkBox.CheckState = CheckState.Unchecked;
checkBox.Location = location;
checkBox.Text = text;
checkBox.UseVisualStyleBackColor = true;
return checkBox;
}
then pass a checkbox to it checkBox1 = DoSomethingWith(checkBox1, new Point(10,10), "My Text");
Upvotes: 4
Reputation: 543
Maybe you could reduce the redundancy by creating methods to initialize your controls passing the controls as parameters (you could also create the control inside the method itself). For example:
public void setupCheckbox (Checkbox checkbox, boolean autoSize, boolean checked, etc.)
public void setupRadioButton (RadioButton radiobutton, boolean autoSize, boolean checked, etc.)
And then create the controls using the above methods:
setupCheckbox (checkBox1, true, false, etc.)
setupRadioButton (radio1, true, false, etc)
setupRadioButton (radio2, true, false, etc)
Anyhow, the visual attributes of the classes you are referring to normally are defined within the IDE (e.g. Visual Studio) so normally you don't care about them. Maybe you could consider storing the Text in an array for the initialization of the different controls if you find it useful.
Upvotes: 1
Reputation: 36339
If all you're trying to do is tinker, and not set up a whole application architecture, and you're writing this yourself rather than trying to mess w/ the designer-generated code, then using some kind of structure you can loop over is probably you're best option. LINQ has some really nicer operators for doing simple transformations like that.
For example,
var formElements = myArrayofElementInfo.Select(e => CheckBox(e.Point, e.Text))
That also assumes you're using something like the CheckBox method presented by @Sean87 to consolidate construction.
Upvotes: 1
Reputation: 49261
You can make your own class that inherits from RadioButton
, set your default settings, and use it.
public class MyRadioButton : public RadioButton
{
MyRadioButton()
{
UseVisualStyleBackColor = true;
AutoSize = true;
}
}
Then you just add this control instead of RadioButton
.
Upvotes: 4
Reputation: 101614
So...you want to refactor the Form1.designer.cs file?
Anything you do to "clean up" would place an unnecessary burden on the CPU just for the sake of "clean code". Just let the designer be (unless you're talking repeat designs, in which case a user control may be the way to go).
Those are my impressions on this, anyways
Upvotes: 2