|
|
The main window of my application contains a large GroupBox, which itself contains a large "grid" of controls.
The number of "rows" and "columns" of controls varies, and sometimes I need to shift whole rows of them up and down as I insert new rows. I may have as many as 100 rows of controls, 10 controls to a row.
During this process, I'd like to delay redrawing of the Group Box while I reposition the controls. How can I do this?
I've checked out the GroupBox properties and methods; SuspendLayout() and ResumeLayout() are similar, but they only impact the Layout engine, not the repainting engine.
What's my best approach, here?
|
|
|
|
|
Create a inherited control from groupbox. Add a property to control the delay and if it can be repainted and on Paint overrided method check these properties...
Is this a valid solution? Ask me if you don't know how do it
Visit my blog at http://dotnetforeveryone.blogspot.com/
|
|
|
|
|
Well, it's beyond what I *want* to do, yes... . I was certainly hoping that I was missing an obvious built-in solution. Perhaps setting a flag while I'm doing my layout, and putting some code into the existing GroupBox Paint event handler...
I'm probably not up to creating an inherited control at this point. Or too lazy to struggle through reading up on it, implementing it, and debugging it...
Thanks, though.
|
|
|
|
|
Did you try setting the visible property to false, rearrange the controls, then set it to true? This may produce a flicker though.
Are you laying out the controls once (e.g. during the load) or many times (e.g. based on user interaction)?
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
That causes the whole area of the GroupBox to flash at least once.
|
|
|
|
|
Try using the SuspendLayout and ResumeLayout methods. Call SuspendLayout before you add all of the controls and then ResumeLayout once you're done.
|
|
|
|
|
That's why I mentioned it in my post. It doesn't prevent painting, it prevents the Layout events from being fired. Since what I am doing doesn't normally cause Layout events for the group box (unless I cause it to autogrow), this doesn't prevent the controls from being visibly redrawn...
|
|
|
|
|
Sorry about that. I must not have seen the reference in your original post. You mention that you are placing controls in the group box in a "grid", what are you using to do this?
It sounds like you're doing this in order to prevent the control from flashing as you add new rows. Without handling the paint method yourself, I don't think there will be a way to do this.
You can do this pretty easily by creating your own GroupBox control. It should look something like this:
public class DelayPaintGroupBox : GroupBox
{
private bool paintAllowed;
public DelayPaintGroupBox()
: base()
{
}
protected override void OnPaint(PaintEventArgs e)
{
if (paintAllowed)
{
base.OnPaint(e);
}
}
public bool PaintAllowed
{
get
{
return this.paintAllowed;
}
set
{
if (this.paintAllowed != value)
{
this.paintAllowed = value;
}
}
}
}
|
|
|
|
|
Can anyone explain to me a simple way to write to controls from a thread that didn't create them. I've viewed the MSDN article and I just can't get it to work.
|
|
|
|
|
Here is an example using anonymous delegate:
public Boolean SafeEnabled {
get {
if (true == this.InvokeRequired) {
Boolean enabled = false;
MethodInvoker method = new MethodInvoker(
delegate {
enabled = this.Enabled;
});
Invoke(method);
return enabled;
}
else {
return this.Enabled;
}
}
set {
if (true == this.InvokeRequired) {
MethodInvoker method = new MethodInvoker(
delegate {
this.Enabled = value;
});
Invoke(method);
}
else {
this.Enabled = value;
}
}
}
Basically, you need to "Invoke" the method on the thread that created the control. You can use the Invoke method or the BeginInvoke/EndInvoke pair. The latter works like PostMessage in C++, the former works like SendMessage (if you are familiar with those).
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Use delegates to change control text.
public delegate void ChangeControlTextDelegate(string _NewText, Control _Control);<br />
<br />
private void ChangeControlText(string _Newtext, Control _Control)<br />
<br />
<br />
if (control.InvokeRequired)<br />
{<br />
ChangeControlTextDelegatedCallback dCallback = <br />
new ChangeControlTextDelegate(ChangeControlText);<br />
<br />
this.Invoke(dCallback, new object[] { "newtext", control});<br />
}<br />
else<br />
{<br />
control.Text = "newText";<br />
}
Visit my blog at http://dotnetforeveryone.blogspot.com/
|
|
|
|
|
I do what the other two have said. I used delegates, and I use BeginInvoke().
The reason I use BeginInvoke is - if your method which contains Invoke gets called again before the first Invoke has returned, your application can hang.
BeginInvoke() is asynchronous. So if you aren't super worried about performance, or don't need complete synchronicity, I recommend this approach.
|
|
|
|
|
Thanks everyone, this seems clearer. For some reason microsoft's examples just confuse me. This is probably because I'm so inexperianced. Thanks again.
|
|
|
|
|
|
// How do I make the current directory the
//same as where the program resides.
static void Main(string[] args)
{
int mode=0;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
{
MessageBox.Show(Environment.CurrentDirectory);
// Current directory is where the program resides
// which is what I want
Application.Run(new Form1(mode));
return;
}
if (args.Length == 1)
{
MessageBox.Show(Environment.CurrentDirectory);
// Current Directory becomes \Documents and Settings....
// or \Program Files\Common Files\System\Mapi\NT and I want
// the CurrentDirectory to be the location of the program
mode = 1;
Application.Run(new Form1(mode));
return;
}
}
|
|
|
|
|
I think what you are looking for is:
Application.ExecutablePath
|
|
|
|
|
TNX
I was able to solve my problem although I still don't understand why the directory changes when there is a command line arg[0].
|
|
|
|
|
The current directory isn't set based on the location of the EXE. For command-prompts, it's based on the current directory. So if you have a command-prompt open to say "C:\Temp", then run your application like "C:\Blah\myApp.exe", then the current directory is "C:\Temp". For Windows short-cuts, you can specify the "Start in" directory, which is the same thing.
You can get the path to the entry assembly (the EXE in the general case) like so:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();
Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(assembly.Location);
Or check out the other response, which talks about Application.ExecutablePath
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
TNX Tom
Application.Executable Path solved my problem is there any disadvantage to doing things this way?
|
|
|
|
|
Not at all. If you use Reflector[^] you can view the code for this property. Internally, it uses GetEntryAssembly(), but also handles other cases. So Application.ExecutablePath would probably be the best way to go.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
This is why you never write code that uses the "current" directory. You ALWAYS write your code to generate fully qualified path names when working with files.
|
|
|
|
|
Hy
Can anybody tell me what is the difference between KeyPress event and OnKeyPress override? Why we need both? Why don't work in the same time, only one run?
thx
|
|
|
|
|
The KeyPress event can be used by the associated class or other classes. For example, a Form can listen for key pressed on a TextBox that it contains. The OnKeyPress method is used to fire the KeyPress event. This can only be used by dervied types. So if you create a custom TextBox (say MyTextBox), then you can override the OnKeyPress to catch key presses, alter when the KeyPress event is fired, fire another event, etc.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hello,
Way 1)
OnKeyPress ist the virtual method of a class, which can be overriden from an inherited class.
Way 2)
For handling the KeyPress event, you do not have to inherit a class.
You instanciate a class and handle the delegate.
All the best,
Martin
|
|
|
|