Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simulate selecting a text box and pressing return

Status
Not open for further replies.

MJV57

Programmer
Apr 18, 2009
87
CA
I have created some code to enter a value in a bindingNavigatorPositionItem box but I don't know how to simulate selecting it and pressing return in code. Can anyone help me on this. I know it sounds simple but nothing i tried has worked. Thanks in advance
 
you wouldn't "simulate" this at the GUI. you would create a object to do the actual work, and wire the GUI events to execute the public members of this object.

example say your code looks like this
Code:
//this is a wired button event
public void Button_Click(object sender, EventArgs e)
{
    var some_text = MyTextBox.Text;
    //code to save some_text to database.
}
extract the database code into a new object and have the Button_Click handler call the new objects code
Code:
private SomeService service = new SomeService();

//this is a wired button event
public void Button_Click(object sender, EventArgs e)
{
    var some_text = MyTextBox.Text;
    service.Save(some_text);
}
Code:
class SomeService
{
   public void Save(string text)
   {
       //code to save some_text to database.
   }
}

SomeService is not coupled to the GUI. You can test what happens in SomeService using a tool like nunit, mbunit or mstest.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason but this item is not going to be saved to the database. I replacing the number in the table row number field with the row numbe I want and once I put this in hear and press return it updates all fields on my form to fields in the row that corresponds to the row number I have selected.
 
the actual work done by SomeService doesn't matter. What your gaining is abstraction and loose coupling. make the modifications that are necessary, then rebind the data to the GUI.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
MJV57 - when you hit enter on a field on a form, what event fires?

hint: If you have a default button assigned, it'll be it's click event.

So if you were to call the click event yourself (it's a regular instance method), wouldn't that have the same net effect as if someone had hit enter?

Chip H.



____________________________________________________________________
www.chipholland.com
 
@chipH : yes, but your view (Form) is still tightly coupled to the logic. by decoupling the logic from the view we have
1. a separation of concerns
2. a reusable object, to reduce duplicate code
3. reduced coupling between the presentation logic
as an added bonus, we can begin write automated tests against this new object.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I agree, but it sounds like those might be too difficult for him to implement.

Chip H.


____________________________________________________________________
www.chipholland.com
 
YEs you are right too difficult for a novice like myself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top