Joel Reid

Joel Reid

  • NA
  • 35
  • 0

Accessing controls on a form from a separate class

Dec 12 2011 10:05 PM
I am developing a small app to burn CD\DVD's and one of the problems I am facing is accessing form controls from within my class.  I realize I can pass in a control by creating a public variable with the control type, such as label or textbox when creating my object.  But what if I wanted it to be more flexible and have the option of setting that Control variable in the main class to be a combobox or listbox etc without having to declare it as that specific type?

Essentially I have 3 classes, 2 of which are "options" classes that contain variables for different options when burning.  The first is the BurnerOptions, the second is BurnerDisplayOptions. 
The third class is just Burner and that's where all the magic happens.  It has 4 constructors.  One is blank, one will initialize just burn display options, one will initialize just burner options and one will initialize them both. 
There are a few methods in the Burner class that use the display options.  DetectMedia(), UpdateCapacity(), and then the backgroundworker events, DoWork, ProgressChanged, RunWorkerCompleted. 
Someone from another forum suggested I implement interfaces but I am unclear of how I could implement them at this point.

Basically I am looking for an alternative to declaring everything as Labels or ProgressBars in my BurnerDisplayOptions.  I want to know if there is a way to declare them as Objects or Controls and then somehow determine the type of the control and use it accordingly in my UpdateCapacity and DetectMedia methods.

Here's my BurnerDisplayOptions class, how that class is intialized, and one of the methods that use the options.


public class BurnerDisplayOptions
{
        public Label _label_BurnStatus_Display = null;
        public Label _label_MediaType = null;
        public Label _label_DiscDetectedStatus = null;
        public Label _label_TotalDiscSize = null;
        public Label _label_SizeOnDisc = null;
        public Label _label_DiscStatus = null;
        public ProgressBar _progressBar_BurnStatus = null;
        public ProgressBar _progressBar_DiscCapacity = null;
        public ListBox _listbox_Files = null;
        public ComboBox _combobox_BurnerList_Display = null;
        public BurnerDisplayOptions()
        {

        }
}



/// <summary>
///
Private method to initialize the burner display options
/// </summary>
/// <param name="burnDisplayLabel"></param>
/// <param name="burnDisplayProgressBar"></param>
private void InitBurnDisplayOptions(Label burnDisplayLabel,
ProgressBar burnDisplayProgressBar,
ProgressBar discCapacityProgressBar,
ComboBox burnerListComboBox,
Label mediaType, Label discDetectedStatus, Label sizeOnDisc, Label totalDiscSize, ListBox fileListBox)
{
        bdOptions._label_BurnStatus_Display = burnDisplayLabel;
        bdOptions._progressBar_BurnStatus = burnDisplayProgressBar;
        bdOptions._progressBar_DiscCapacity = discCapacityProgressBar;
        bdOptions._combobox_BurnerList_Display = burnerListComboBox;
        bdOptions._label_DiscDetectedStatus = discDetectedStatus;
        bdOptions._label_MediaType = mediaType;
        bdOptions._label_SizeOnDisc = sizeOnDisc;
        bdOptions._label_TotalDiscSize = totalDiscSize;
        bdOptions._listbox_Files = fileListBox;
}



private void UpdateCapacity()
{
//
// Get the text for the Max Size
//
if (this.TotalDiscSize == 0)
{
        bdOptions._label_TotalDiscSize.Text = "0MB";
        bdOptions._label_SizeOnDisc.Text = "0MB";
        bdOptions._progressBar_DiscCapacity.Value = 0;
        bdOptions._label_MediaType.Text = "";
        return;
}

bdOptions._label_TotalDiscSize.Text = this.TotalDiscSize < 1000000000 ?
string.Format("{0}MB", this.TotalDiscSize / 1000000) :
string.Format("{0:F2}GB", (float)this.TotalDiscSize / 1000000000.0);

//
// Calculate the size of the files
//
Int64 totalMediaSize = 0;
foreach (IMediaItem mediaItem in bdOptions._listbox_Files.Items)
{
        totalMediaSize += mediaItem.SizeOnDisc;
}
bdOptions._label_SizeOnDisc.Text = totalMediaSize < 1000000000
? string.Format("{0}MB", totalMediaSize / 1000000)
: string.Format("{0:F2}GB", (float)totalMediaSize / 1000000000.0);
if (totalMediaSize == 0)
{
        bdOptions._progressBar_DiscCapacity.Value = 0;
        bdOptions._progressBar_DiscCapacity.ForeColor = SystemColors.Highlight;
}
else
{
        var percent = (int)((totalMediaSize * 100) / this.TotalDiscSize);
        if (percent > 100)
        {
                bdOptions._progressBar_DiscCapacity.Value = 100;
                bdOptions._progressBar_DiscCapacity.ForeColor = Color.Red;
        }
        else
        {
                bdOptions._progressBar_DiscCapacity.Value = percent;
                bdOptions._progressBar_DiscCapacity.ForeColor = SystemColors.Highlight;
        }
}
}












Answers (2)