473,395 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

User control Name

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.

Nov 17 '05 #1
1 3711
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


Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
4
by: patrick_a | last post by:
Hello, I am trying to insert multiple instances of a custom user control into a placeholder on an aspx page, based on the records retrieved from the database. I use the datareader to loop through...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
5
by: Marcel Gelijk | last post by:
Hi, I am trying to create a User Control that is located in a seperate class library. The User Control contains a textbox and a button. The page generates an exception when it tries to access...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
1
by: tony | last post by:
Hello! I just want to find out how the system find the name to set on a assembly User control dll. I have done this. 1. Create a user control - Here the namespace was set by the system to...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
9
by: abprules | last post by:
Can somehow tell me the best way for multi user development to occur in MS Access? The situation is: We are creating a new database for a small company. There are 2 of us who want to...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.