User control Name

J

juracan

I created a user control with a couple of radio buttons on it.
When the user clicks on one of the radio buttons an event gets passed
up along with the value of the radio button clicked.

I then created a form with several of these controls on it.

Each one of these controls effects another item on my form.
I can easily write a code for each of the Controls on my form but since
they all do similar things I'd like to create one function that handles
the events for all the controls.

My problem : I need to know the name of the user control given to in on
the form and not from the actual control. ie..
private void one_CheckedChanged(object sender, System.EventArgs e)
{
string lblname =((Control)(sender)).Name.ToString();

//get the item number
string txtnumber;
if(lblname.Length==8)
txtnumber=lblname.Substring(lblname.Length-1,1);
else
txtnumber=lblname.Substring(lblname.Length-2,2);

// get the user controls associated with that number
UserControl1 ucAlign = (UserControl1)
FindControl("ucAlignLN"+txtnumber);
Label lb = (Label) FindControlLabel("lbltextBox"+txtnumber);
TextBox tb = (TextBox) FindControlLabel("textBox"+txtnumber);

// changed the alignment on the screen
switch(ucAlign.Alignment)
{
case "LEFT":
lb.Text = tb.Text.PadRight(50,' ').Replace(" ","*");
lb.Text = lb.Text.Substring(0,50);
break;
case "RIGHT":
lb.Text = tb.Text.PadLeft(50,' ').Replace(" ","*");
lb.Text = lb.Text.Substring(lb.Text.Length-50,lb.Text.Length);
break;
}
}

Unfortunately this line gives me the name of the radiobutton from the
control and not the name of the control givien to it on the form.
string lblname =((Control)(sender)).Name.ToString();

So how do i get the name given to the user control on the form?

Thanks.
 
G

Guest

You might be better off doing it like this, its 'Bulletproof'

///User control
public class AlignmentOptionControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.RadioButton rbLeft;
private System.Windows.Forms.RadioButton rbRight;

public AlignmentOptionControl() {
InitializeComponent();
this.rbLeft.CheckedChanged += new System.EventHandler(this.AlignmentChecked);
}
//...
private void AlignmentChecked(object sender, System.EventArgs e) {
HorizontalAlignment alignment = HorizontalAlignment.Left;

if (rbRight.Checked) {
alignment = HorizontalAlignment.Right;
}
OnAlignmentChanged(new AlignmentEventArgs(alignment));
}

protected void OnAlignmentChanged(AlignmentEventArgs e) {
AlignmentChanged(this, e);
}

public event AlignmentEventHandler AlignmentChanged;

}

//events related handler and args
public delegate void AlignmentEventHandler(object sender, AlignmentEventArgs
e);

public class AlignmentEventArgs: EventArgs {

HorizontalAlignment alignment;

public AlignmentEventArgs(HorizontalAlignment alignment) {
this.alignment = alignment;
}

public HorizontalAlignment Alignment {
get {
return alignment;
}
}

}

//Now in the form, just wire up to the control event
public class MainDialog : System.Windows.Forms.Form {

private AlignmentOptionControl alignmentOptionControl;

public MainDialog() {
//...
alignmentOptionControl.AlignmentChanged += new
AlignmentEventHandler(ChangeAlignment);
}

void ChangeAlignment(object sender, AlignmentEventArgs e){
Console.WriteLine("Alignment changed: " + e.Alignment.ToString());
switch (e.Alignment) {
case HorizontalAlignment.Left:
//handle left alignment
break;
case HorizontalAlignment.Right:
//handle right alignment
break;
}
}

}

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top