Referencing an object - TextBox - ComboBox - by its name storeded as string
-
Hi all, I want to store the contents changes of my windows controls in a database. Therefore I determined the name of the object in the control_enter_method and save the name and value as string. Later in the case of canceling the changes I want to reassign the stored values from database back to the controls. How can I reference the control object by it's name (stored as string) ? My idea was
object obj; //
((TextBox)obj).Name = stored_obj_name; //
((TextBox)obj).Text = stored_value; //but this does not work. what's to do ? Tnx in advance Frank
-
Hi all, I want to store the contents changes of my windows controls in a database. Therefore I determined the name of the object in the control_enter_method and save the name and value as string. Later in the case of canceling the changes I want to reassign the stored values from database back to the controls. How can I reference the control object by it's name (stored as string) ? My idea was
object obj; //
((TextBox)obj).Name = stored_obj_name; //
((TextBox)obj).Text = stored_value; //but this does not work. what's to do ? Tnx in advance Frank
If you control name, you can access Controls property of a Form class using an indexer that takes string as an input.
Giorgi Dalakishvili #region signature my articles #endregion
-
If you control name, you can access Controls property of a Form class using an indexer that takes string as an input.
Giorgi Dalakishvili #region signature my articles #endregion
-
If you control name, you can access Controls property of a Form class using an indexer that takes string as an input.
Giorgi Dalakishvili #region signature my articles #endregion
-
Hi Giorgi, thanks for the reply. Is that the right solution to get the index ? I tried this
int index = this.Controls.IndexOf((TextBox)sender);
but get always index = -1; Where is my fault ? Tnx Frank
-
int index = this.Controls.IndexOf(textBox1);
works for me, where is your code, what is the sender?
int index = this.Controls.IndexOf(null);
gives me -1, so it sounds like you are not passing in a textbox that is in the Controls collection
The sender comes from the Textbox control enter method The Enter_Control method is in the Form class where also the Textbox exists.
private void Enter_Control(object sender, System.EventArgs e)
{
try
{
switch(sender.GetType().ToString())
{
case ("System.Windows.Forms.TextBox"):
int index = this.Controls.IndexOf((TextBox)sender);
this.Controls[index].BackColor= Color.Blue;
break;
}
}
catch{}
}Tnx Frank
-
The sender comes from the Textbox control enter method The Enter_Control method is in the Form class where also the Textbox exists.
private void Enter_Control(object sender, System.EventArgs e)
{
try
{
switch(sender.GetType().ToString())
{
case ("System.Windows.Forms.TextBox"):
int index = this.Controls.IndexOf((TextBox)sender);
this.Controls[index].BackColor= Color.Blue;
break;
}
}
catch{}
}Tnx Frank
You can do it like this:
private void Enter_Control(object sender, System.EventArgs e)
{
if (sender is TextBox)
{
((TextBox)sender).BackColor = Color.Blue;
}
}Giorgi Dalakishvili #region signature my articles #endregion
-
You can do it like this:
private void Enter_Control(object sender, System.EventArgs e)
{
if (sender is TextBox)
{
((TextBox)sender).BackColor = Color.Blue;
}
}Giorgi Dalakishvili #region signature my articles #endregion
Hi Giorgi, I had to break the discussion yesterday ... My code should only test that I have got the right index of the selected control and set the backcolor in this "long way" as test that it works. But the problem already exists, that I don't get an index != -1. This is main funcion of my method
int index = this.Controls.IndexOf((TextBox)sender);
tnx Frank
-
Hi Giorgi, I had to break the discussion yesterday ... My code should only test that I have got the right index of the selected control and set the backcolor in this "long way" as test that it works. But the problem already exists, that I don't get an index != -1. This is main funcion of my method
int index = this.Controls.IndexOf((TextBox)sender);
tnx Frank
Is the textbox on the form itself or is it on the panel or groupbbox or any other container? If yes, then instead of this.Controls you should have the container that contains your textbox.
Giorgi Dalakishvili #region signature my articles #endregion
-
Is the textbox on the form itself or is it on the panel or groupbbox or any other container? If yes, then instead of this.Controls you should have the container that contains your textbox.
Giorgi Dalakishvili #region signature my articles #endregion
Oh yes, that's the problem. Due to the hierarchical structure of my Form each control is a container of controls. I tested my method and it works, for one container, but it doesn't help me with the problem. my intention was to reference a control by its name (as a string) independant of the location of the control. But the index is not unique and the prerequisite is the known of the hierarchical structure of my form. Changing the form would change the indices. Isn't there an other way ? txn Frank