Click here to Skip to main content
15,888,148 members
Please Sign up or sign in to vote.
1.20/5 (5 votes)
See more:
hi all,
C#
focus =textbox1.text;  ???

I want to fill an object with focus?
Posted
Updated 15-Sep-12 10:47am
v2
Comments
[no name] 15-Sep-12 16:47pm    
What? That makes no sense. Maybe you want to set focus to the textbox? If you had a string variable named "focus" that would make sense...
samad blaj 15-Sep-12 16:51pm    
I do not have access to the object name.
Want to become a significant focus by pressing button1 text field object
[no name] 15-Sep-12 16:54pm    
That still makes no sense at all. I think what you want to do is TextBox1.Focus().
samad blaj 15-Sep-12 16:52pm    
controls.focus="hello";
[no name] 15-Sep-12 16:54pm    
That makes even less sense. Focus is not a property it is a method of a control.

This is an aweful way of doing it - you should not simulate tab keys like this. What you should do is create a List<TextBox> containing the TextBoxes you wish to set the text of then loop through that. E.g.

C#
List<TextBox> MyTextBoxes = new List<TextBox>();
MyTextBoxes.Add(textbox1);
//Add the other boxes here

string strnew = "hello";
int j = (int)Random.Next(0, MyTextBoxes.length);
for(int i = 0; i <= j; i++)
{
    MyTextBoxes[i].Text = strnew;
}


Hope this helps,
Ed
 
Share this answer
 
v2
Comments
samad blaj 15-Sep-12 17:17pm    
no,see my code.
Ed Nutting 15-Sep-12 17:25pm    
Well what on earth is Focused supposed to be then!? Do you mean you want YourForm.AcvtiveControl which gets the control in YourForm that currently has focus? See here: http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.activecontrol.aspx
samad blaj 15-Sep-12 17:29pm    
yesssss.
samad blaj 15-Sep-12 17:31pm    
yes,
YourForm.AcvtiveControl="samad"; ?
Ed Nutting 15-Sep-12 17:33pm    
Err.. you can't set a control to a string - you need to set the control's Text property. Assuming the active control is definitely a textbox:
((TextBox)YourForm.ActiveControl).Text = "Samad";
would work.
if you want to change the text of textBoxes in your form:

C#
public void Form_Load(
{
    foreach (Control control in this.Controls)
    {
        if (control is TextBox)
            (control as TextBox).GotFocus += delegate(object sender, EventArgs e)
            {
                 (control as TextBox).Text = "hello";
            }
    }
}
 
Share this answer
 
v2
Comments
samad blaj 15-Sep-12 16:55pm    
noooo, controls.focus="hello";
[no name] 15-Sep-12 16:59pm    
When and Where do you want to do that?
Please improve your question.
Ed Nutting 15-Sep-12 16:59pm    
focus is a method - not a Text Property! You cannot set it! What are you trying to achieve? Please improve your question to describe what you think that line of code should do? Use the Improve Question link that's in green at the bottom right of your question.

Thanks,
Ed
samad blaj 15-Sep-12 17:02pm    
//Example
string strnew="hello";
Random j;
for (int i = 0; i <= j ; i++)
{
SendKeys.Send ("{tab}");

Focused = strnew; //Important
}
[no name] 15-Sep-12 17:05pm    
Every line of your code doesn't make any sense.

Please explain to get your answer.
hi all, help me in code:

C#
for (int i = 0; i <= 5; i++)
            {
                SendKeys.Send("{tab}");
            }
            this.ActiveControl.Text = "hello"; ??????
            this.ActiveControl.Select();

paste string "hello" in focused?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900