TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ken
NA
110
0
How can I convert a Control to a specific type like Button?
May 29 2021 12:15 AM
Hello. I am building controls dynamically on a form based on some config data I read. Originally I had code like:
public
Button GetElementButton(
string
elementID,
string
text) {
Element elem = GetElement(elementID);
if
(elem ==
null
)
return
null
;
Button ret =
new
Button();
ret.Text = text;
ret.Location = elem.location;
ret.Size = elem.size;
return
ret;
}
public
Label GetElementLabel(
string
elementID,
string
text) {
Element elem = GetElement(elementID);
if
(elem ==
null
)
return
null
;
Label ret =
new
Label();
ret.Text = text;
ret.Location = elem.location;
ret.Size = elem.size;
return
ret;
}
Button myButton = GetElementButton("btn.score.p1", "Score");
myButton.Click += (sender, e) => { Game.Score(1); };
Controls.Add(myButton);
I hate repeating myself though. It occurred to me I could
(I thought)
do:
public
Control GetElementControl(
string
elementID,
string
text) {
Element elem = GetElement(elementID);
if
(elem ==
null
)
return
null
;
Control ret =
new
Control();
ret.Text = text;
ret.Location = elem.location;
ret.Size = elem.size;
return
ret;
}
Button myButton = (Button)GetElementControl(
"btn.score.p1"
,
"Score"
);
myButton.Click += (sender, e) => { Game.Score(1); };
Controls.Add(myButton);
But they all appear blank, no text. Then when I click the button, it crashes with:
System.InvalidCastException:
'Unable to cast object of type '
System.Windows.Forms.Control
' to type '
System.Windows.Forms.Button
'.'
So I guess one can go Button -> Control but not Control -> Button. Do I have to go back to a method for each type, or does someone know a way to achieve the dynamic creation so I don't have to repeat?
Thanks!
Reply
Answers (
2
)
Null Reference exception object reference not set to instance of an ob
ExcelJs Not working