|
I'd say that if both work well, then choose the one you think works and looks best for your application. That's just my input.
Happy Programming and God Bless!
Internet::WWW::CodeProject::bneacetp
|
|
|
|
|
Sorry for the long decription...
Description:
I have a windows form which contains a ListView control. I'm automatically resizing the whole Form to fit the desktop. Basically, if the user dragged the form to the bottom of the desktop area, the ListView will shrink in size so that the whole window is still on the screen and none of the form extends past the Desktop bounds. To do this I have anchored the ListView control on all side and I change the Form's height during the LocationChanged Event, which in turn causes the ListView to change it's size due to the anchor. When the ListView shrinks the scrollbars are shown automatically which is what I want to occur. So everything works great up to this point.
Problem:
The problem occurs when I release the left mouse button after dragging the Form to the bottom of the desktop, the ListView Scrollbars disappear. It appears that the ListView is then resized past the bounds of the desktop, but I cannot be sure. When I click to drag the form the scroll bars reappear, but again disappear when I release the button.
Also, if I debug and step throught he code the scrollbars remain after the left mouse button is released....very odd
Any seems a similar problem ?
|
|
|
|
|
I *think* I know what it is, the LocationChanged Event is firing when I'm changing the size of the form from within the LocationChanged event handler, which is producing a recursion effect. Is there any way to stop the LocationChanged event from firing when I change the size of the form ?
|
|
|
|
|
Use state variables. If you programmatically change the location or size, set the state variable to some value, like true . In the LocationChanged event (and actually, if you're handler is in your Form class and is for that Form , override OnLocationChanged - calling base.OnLocationChanged - for better performance), if that state variable is set, don't execute your normal code but instead reset the state variable, like to false (if using a boolean).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I've tried this and I don't think its the answer:
protected override void OnLocationChanged(EventArgs e)
{
if(!SettingLocation)
{
if(frmFav!=null && frmFav.Visible)
{
timFavMover.Enabled=true;
MoveIdle=0;
}
IdleTime=0;
SetPlayListHeight();
}
base.OnLocationChanged (e);
}
private void SetPlayListHeight()
{
try
{
if(lvPlayList.Items.Count==0)
return;
SettingLocation=true;
ListViewItem LastItem = lvPlayList.Items[lvPlayList.Items.Count-1];
Rectangle ItemRect = LastItem.GetBounds(ItemBoundsPortion.Entire);
Screen CurScreen = Screen.PrimaryScreen;
Rectangle Desktop = CurScreen.WorkingArea;
if(this.Bottom > Desktop.Bottom)
{
if(this.Height <= PlayerRect.Height)
{
this.Height=PlayerRect.Height;
return;
}
while(this.Bottom > Desktop.Bottom)
{
int BottomDiff = this.Bottom-Desktop.Bottom;
if(BottomDiff==0)
break;
this.Height-= BottomDiff;
if(this.Height <= PlayerRect.Height)
break;
}
}
else if(!IsItemVisible(LastItem) && this.Bottom < Desktop.Bottom)
{
while(!IsItemVisible(LastItem) && this.Bottom < Desktop.Bottom)
{
int BottomDiff=Desktop.Bottom-this.Bottom;
if(BottomDiff==0)
break;
if(ItemRect.Height > BottomDiff)
this.Height+=BottomDiff;
else
this.Height+=ItemRect.Height;
}
}
}
catch(Exception Err)
{
MessageBox.Show("SetPlayListHeight Error: "+Err.Message);
}
finally
{
SettingLocation=false;
}
Correct me if I'm wrong but,
everytime I set the location or size of my form programatically a LocationChanged event is added to the queue, these messages then stack up until I release the mouse button and the form's message pump can process the messages in the queue.
I tried adding Application.DoEvents() in my finally block but that caused the form to not resize at all.
Any suggestions ?
|
|
|
|
|
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below
//foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox)
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
Would you please tell me why it does not work?
|
|
|
|
|
caheo wrote:
Would you please tell me why it does not work?
What error do you get? Does it just not update the colours? or does it throw an exception? or just does not compile?
That information is usually an excellent start to figuring out why something does not work.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
It does not give any error. The reason I ask is there are quite a few TextBoxes on the GUI, after doing comparision at the if statement it skips and do the next iteration. Seems to me the if statement never reaches.
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox) <- it does comparision but never gets inside the if-statement
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
Regards
|
|
|
|
|
Personally I would have written it like this:
foreach(Control ctr in Page.Controls)
{
TextBox t = ctr as TextBox;
if(t != null)
{
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
Are you sure your TextBox es aren't in other naming containers (like the Panel )? Just because all the TextBox es appear to be in the page, doesn't mean they're direct children of the page. You need to use a recursive methods like so:
private void SetReadOnly(Control parent, bool readOnly)
{
if (c != null)
{
foreach (Control child in parent.Controls)
{
if (child is TextBox)
{
TextBox tb = (TextBox)tb;
tb.BackColor = Color.AliceBlue;
tb.ReadOnly = readOnly;
}
SetReadOnly(child, readOnly);
}
}
} To call this, just pass the Page reference to this property:
SetReadOnly(Page, false); For more information, see the INamingContainer interface documentation in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Since you don't say what happens or what doesn't happen or give any kind of error message (other than "it's broke"), it's difficult to say what you're problem is. I can take a guess and say that you might be having trouble with the if statement, so how about making your if statement look like this:
foreach(Control ctr in Page.Controls)
{
if(ctr.GetType().Equals(GetType(TextBox)))
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
There you go again, Dave!
GetType is a VB.NET operator equivalent to C#'s typeof operator.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
DAMN! I can't keep anything straight nowadays!
Now where did my mouse go...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
See what VB.NET is doing to you? Break away now or be doomed...
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You aren't looping through all the controls on the form, just the direct children of the Page. To get all, you would need to recursively search through the control. Also make sure you are doing this in the PreRender event so you can make sure that all the controls have been created.
The architect has placed his bets,
but the odds are long
-Poster Children
|
|
|
|
|
Hi, I need to know when the user press the period key in the numpad.
In the keys enumeration I have Keys.NumPad1 .... Keys.NumPad9.
Im using the keyup event and its e.Keycod argument.
What Keys enumeration value I shuold to use?
Gracias!
La realidad no es más que impulsos eléctricos del cerebro - Morpheus
|
|
|
|
|
This is documented, though not very clearly - so I offer another suggestion...
If you're not sure about something, a really quick method is to write a small test app. I threw the following together in under a minute:
using System;
using System.Windows.Forms;
public class Test : Form
{
static void Main()
{
Application.Run(new Test());
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
Console.WriteLine(e.KeyCode);
}
} Running it yields that the decimal point on the numpad is Keys.Decimal and that the period is Keys.OemPeriod . Writing simple test apps is very handy - something to keep in mind.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you. Its a good sugestion.
La realidad no es más que impulsos eléctricos del cerebro - Morpheus
|
|
|
|
|
I wanted to create my own Menu type class for use with a drop down button so that when you click the button a window can pop up that will contain some other controls. At first I thought I could do it with a stripped down form, but I realized that when I create the form it will always be activated and take focus from my main window (I don't want this to happen). So what I'm wondering is what are similar things like context menus or the dropdown portion of the combobox based on? Or is it possible to make a form that won't steal the focus?
Thanks,
Jesse
|
|
|
|
|
Windows Forms controls (as well as many classes in the .NET Framework Class Library (FCL) encapsulate native APIs. You need to research and understand the Windows APIs and common controls to do what you want. For instance, read the Windows Controls[^] and Windowing[^] sections in the Platform SDK. You need to understand window handles, styles, and message pumps.
You might try searching CodeProject for existing managed samples (in C#, VB.NET, whatever - it all gets compiled to roughly the same thing), but you'll most likely find the majority of samples in VC++ or MFC. Writing custom menu bars and toolbars in VC++ or MFC is very common and there are plenty of samples both here on CodeProject and on sites like CodeGuru[^].
For example, you'll want to override the protected CreateParams property in your control derivative and use the WS_POPUP (0x80000000) window style. See the Platform SDK for more details about that, which should give you some idea of where to start.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi
I have been tring to cause a script timeout from an aspx page
(to understand the process better...)
I have tried these 3 ways but none worked:
A) on Page_Load -
Server.ScriptTimeout = 10;
B) in web.config -
<HTTPRuntime executionTimeout="10">
C) IIS->Default Web Page->Properties->Home Directory->Configurations->
set ASP Script timeout to 10
I have put a long file copying to make sure the page is not returned from the server before the script time out but still my page does not cause a script timeout.
Could Anybody help me,
Thanks.
Eli
|
|
|
|
|
|
Does anyone know how to copy a fixed string buffer directly into a struct in 50 lines or less? Can this be done in C#? I have been looking all over and I am unable to find a good example of this using c sharp. In vb6 I would use the CopyMemory function to do this but I can't in sharp.
I want to take this struct(not sure even if it the correct layout) and
pass my buffer string into it.
string buffer = "abcdefgh2655501";
[StructLayout(LayoutKind.Explicit)]
public struct MyStruct
{
[FieldOffset(0)] public string fname;
[FieldOffset(4)] public string lname;
[FieldOffset(8)] public string phone;
}
Diego
|
|
|
|
|
dgiljr wrote:
Does anyone know how to copy a fixed string buffer directly into a struct in 50 lines or less?
Why don't you tell us what you're trying to accomplish? Do you need to marshal this struct to unmanaged code, or just parse it? If you need to marshal it, then you also need to declare the character encoding. A field length of 4 bytes for each field is only 2 characters using the native Unicode encoding of .NET (strings are stored and manipulated using Unicode). You should also use the MarshalAs(UnmanagedType.ByValTStr) , and don't forget to set the CharSet field on the StructLayoutAttribute accordingly. Again, though, 4 bytes still isn't enough.
Now, to marshal this struct to unmanaged code as string reference, what you have would work on a 32-bit processor. Typically, you want to use LayoutKind.Sequential and make sure your fields are declared properly (like unmanaged long is actually a managed int - both 32 bits). Then, on a 64-bit OS (when .NET 2.0 is released that supports 64-bit processors) the string references would actually be 64-bits (the size of a handle, or unsigned int).
If you just need to parse a string, either use the String class methods (like Substring ) or use a StringReader (see the StringReader.Read(char[], int, int) method in teh .NET Framework SDK). There's other ways, as well; after all, this is just simply string parsing.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I need to send and receive unmanaged strings to a Unix box using sockets and because of our buffer protocols changing from time to time the best way to manage this is by using structs. Currently if I have 30 fields defined in my buffer with fixed lengths and I change the field length for field number 15 I now I have to go into all the properties for fields 16 to 30 and change their substring starting points which is a hassle. I find that using structs eliminates all that hassle since it will automatically adjust itself in that scenario. My character set will have to be unicode but I am struglling with the idea of Marshaling and I can't seem to find a good code example of this for c sharp. I have seen examples of copying structs to structs and arrays to structs but not strings to struct casting.
Diego
|
|
|
|