|
I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
In the KeyDown event for the form, see if the key hit is enter and if so, write code to proceed
|
|
|
|
|
I do not know how to avoid sound.
But you can achieve the behavior by tracking keypress event
private void radioButton1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
Yourcontrol.Focus();
}
}
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
Thanks, Jinal! It works great!
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Hy,
Use the KeyDown event and compare:
if(e.KeyCode == Keys.Enter){
e.Handled = true;
e.SupressKeyPress = true;
txtYouNameIt.Focus();
}
Something like that should work
Just an irritated, ranting son of ... an IT guy.
At your trolling services
|
|
|
|
|
Thanks! This is a little more general solution, and good for a User Control intended for re-use.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
np
Just an irritated, ranting son of ... an IT guy.
At your trolling services
|
|
|
|
|
Hi Roger,
The answers give already, work perfectly well in circumstances where you know the Name of the control that you want to go to and are prepared to write an individual KeyDown /Keypress (select your poison) handler for each control. However, for circumstances where you want to simply follow the TabOrder you have already set and, where applicable, use one handler for several controls, the System.Windows.Forms.Control class has a built in method to do exactly what you want. Look up the System.Windows.Forms.Control.GetNextControl() method.
An example:
private void Comunal_KeyDownHandler(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
this.GetNextControl((Control)sender, true).Focus();
}
}
This way you can assign the handler to all the controls in your User Control, provided that you are not already using their KeyDown event for something else.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
Everything already said will work, but you might also want to consider the "KeyPreview" property of the Form.
This lets the Form see the key-press before it goes to any controls, so it allows you to create a handler at the Form level instead of a handler for each individual control.
Another advantage is that all your code will be inside a single handler instead of spread out among all the various controls.
|
|
|
|
|
Hello,
I would like to bin and zero-fill excel or text data using C#.
For example;
Raw data:
1 34
2
3 43
4 3
5
converted to:
1 34
2 0
3 45
4 3
5 0
Does anyone have code for this?
Thanks
|
|
|
|
|
Couldn't you just format the column to be a number that was an empty cell would display 0?
|
|
|
|
|
For text data, look for lines that don't contain an embedded space, then append " 0":
public List<string> CleanupText(List<string> inputLines)
{
List<string> outputLines = new List<string>();
foreach (string s in inputLines)
{
if (s.Trim().Replace('\t', ' ').Contains(" "))
outputLines.Add(s);
else
outputLines.Add(string.Concat(s, " 0"));
}
return outputLines;
}
If you want to modify the collection in place, you'll have to replace the foreach() with a for() and an index into the list - since foreach() loops disallow modifying the collection they are iterating.
To do this with Excel you'll have to iterate rows (the end condition being a cell in the first column that is empty), looking for cells in the 2nd column that are blank and inserting a '0' into those blank cells. I've not done enough MSOffice interop to be able to give you a code example.
|
|
|
|
|
Hi!
I have a question. I have two forms, Form1 and Form2. I add two textboxes to form1 and the same with form2 I fill in the textboxes on form1 and save the information to sql templatetable1.
I need to display a list of entries from form1 in a dropdonwlist and when the user selects an entry, form2 needs to be populated by the selected template.
How can I do this?
Illegal Operation
|
|
|
|
|
Assign the control to a new control in form2
|
|
|
|
|
|
my application is working properly on windows xp with both office 2003 & 2007
but when i am trying to run it on windows 7 an error message came up,
everything is ok. forms and reports, only creating an Excel file is giving the error.
i installed PIAs for office 2007 but i still have the same problem.
anybody can help me.
the error message is can not load library/dll. com object Excel
thanks in advance
|
|
|
|
|
This is vague.
It could be many things such as security permissions or 64 bit problem or...
Compile your app explicitly for x86. And try again.
Also try running as a admin.
Just an irritated, ranting son of ... an IT guy.
At your trolling services
|
|
|
|
|
if it is a exe, try right-clicking on it and select "run as administrator".
Jack Sparrow
--------------------------------------
Defeat is not the worst of failures. Not to have tried is the true failure.
|
|
|
|
|
Hi Everyone, I have an application that has at least two forms and a Class. how do I access variable between two forms without initializing all varialbes each time I access the class.
this is how I access functions form the Class
In Form1 I do this:
ClassA myClass = new ClassA();
myClass.Function_A();
// do something with it.
IN Form2
ClassA myClass2 = new ClassA();
myClass2.Function_B();
// do something with it.
how do i prevent ClassA funtions from reinitializing all over again. I want to treat the ClassA's funtions and variables like Global variable.
Thanks
"Show me the way I will get there"
|
|
|
|
|
This [^]one is a nice example
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
You could add a constructor to the form class that accepts a parameter that is another copy of the form. Then, write a clone method that copies the data from the speficied instance into the form you're creating. IMHO, this is a silly approach to pretty much anything, but it will do what you want.
If you're actually just interested in keeping just one instance of the form around and using it whenever you want, hide it when you're done with it, and show it again when you need it.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
modified on Wednesday, June 9, 2010 9:03 AM
|
|
|
|
|
Similar to John Simmons's answer, but let each form's constructor take a ClassA. Now, create the ClassA before creating either form, and pass it to the form before showing it. The important fact is that neither form 'owns' (or instantiates) the shared instance of ClassA, rather, some larger scope owns it and makes it available to both forms that want to use it.
An alternative to passing the ClassA into the constructor is to make a ClassA property on each form, and assign the property before showing the form. This is a very common pattern when presenting dialogs whose purpose is to modify data that is used elsewhere. Here's a snippet:
public void DoFormStuff(ClassA ca)
{
using (MyForm frm = new MyForm())
{
frm.ClassA = ca;
frm.ShowDialog(this);
}
}
|
|
|
|
|
HI LookSharp, I am not sure how to use it exactly, let me try first then I will ask you later.
Thanks for your help.
|
|
|
|
|
Hi - I'm implementing a data access layer in LINQ to SQL.
I have a DataContext MyDataContext which extends DataContext.
DataContext implements IDisposable.
However when I try to put MyDataContext in a using statement I get a compiler error:
'MyDataContext':type used in a using statement must be implicitly convertible to 'System.IDisposable'.
So is IDisposable not implicitly usable for derived classes or am I doing something wrong? Here is some sample code:
using (MyDataContext context = new MyDataContext())
{
//do some work
}
|
|
|
|
|
If your class inherits from DataContext, you get IDisposable for free. Could you post your class definition here so that we can take a look at it?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|