I am looking for a way to have some control over the shape of a simple MessageBox in Winforms. I would like to control where the passed in text wraps so that the dialog rect is narrower. Windows seems to want to make the dialog as wide as possible before wrapping the text. Is there an easy way to control the maximum width of the dialog without resorting to creating my own custom form?
You can embed newlines in the text to force it to wrap at a certain point. e.g.
"message text...\nmore text..."
update: I posted that thinking it was a win32 API question, but I think the principle should still apply. I assume WinForms eventually calls MessageBox().
There's really just two ways (sane ways)
1) Add line breaks to your string yourself to limit the lenghth of each line.
2) Make your own form and use it rather than messagebox.
What happens if you throw your own newlines in the string message you pass it? I'm pretty sure that will work if I recall correctly.
This, or alternatively create your own form and use that.
The \n newline chars will give you enough flexibility, then do this. I use this a lot. Eg. if I'm giving a warning, the first line will give the warning, and the next line will give the internal error message or further information as appropriate. If you don't do this, you end up with a very wide message box with very little height!
MessageBox only has limited variability - eg. the button types and icon. If you need more, then create your own. You could then do all sorts of things like add URLs, a Help button ,etc.
Related
I'm very confused with the TextBox, as I'm used to working with RichTextBox (I decided to switch because it seems faster..) and I have a problem:
The TextBox, unlike RichTextBox, doesn't seem to have a textBox.Redo(); function-- at all. Is there a way to implement a .Redo() function for TextBox, or is it impossible?
Thanks for your time!
In TextBoxBase.Undo(), a call is made via SendMessage with EM_UNDO as the message.
In the docs for EM_UNDO we can see the following:
Edit controls and Rich Edit 1.0: An undo operation can also be undone. For example, you can restore deleted text with the first EM_UNDO message, and remove the text again with a second EM_UNDO message as long as there is no intervening edit operation.
So calling Undo() again will redo.
I'm making a game, and it may have modal UI windows, like popover windows.
I encapsulated all the fiddling into my class and I just call one method to show such window on screen, for example a method to show a list of characters:
ShowCharactersTable(List<Character>list,
Action<Character>didSelectCharacter, Action<bool>didCancel)
Example in use:
ShowCharactersTable(someList, delegate(Character selectedCharacter) {
// code that runs after you picked a character
, delegate(bool didCancel){
// code that runs after you cancelled selection
});
This is fine. Everything is transparent and easy to use. But I get into trouble as soon as I need to nest this. For example, on character list I have few buttons on each character cell, so I need to provide delegates for that. This gets complicated. I also might need to show another modal window on top of that, with a call like this:
ShowActivitySelector(List<Activity>activitiesToShow,
Action<Activity>didSelectActivity,
Action<bool>didCancelPickingActivity);
And when I select activity I might need to show Alert if something goes wrong, like here:
ShowAlert(string alertText, Activity<bool>didAccept,
Activity<bool>didCancel);
So now, to call a simple table of characters I need to write a 3 page long method with nested delegates. Does not sound like fun. How can I modularize this and make easier/more maintainable?
I don't want to couple my interface to my model, as I will need to show those lists, selectors and alerts from many different parts of my game with different parameters.
How can this be fixed and what do I do about this?
Could some one explain what the SychronizedInputPattern does? I haven't been able to find any good examples of it being used.
Lets suppose you need to click something but before the click happens, the elements move (maybe due to resize …). In this case, some other element could get the mouse input.
To overcome this problem, SynchronizedInputPattern has been introduced.
buttonElement.GetCurrentPropertyValue(AutomationElement.IsSynchronizedInputPatternAvailableProperty);
Read more over here.. Example can be downloaded here.
Hai Guys,
How to add events like oncopy,onpaste to a dynamically generated textbox in a windows form applications...
I'd first consider sub-classing TextBox.
If there are "many" of these TextBoxes you will create; I'd think about whether they can and will be possibly removed at run-time, as well as added : whether there are some circumstances in which you would ever want to "dis-connect" one or all of the special events from being processed by one or all of these special TextBoxes.
While you could adopt a strategy of overriding the ProcessCmdKeys for the Form (with Form preview key enabled) to handle globally the issue of relevant keystate at the Form level, I wouldn't go there; I would want to isolate it to the special case textbox. The discussion of ProcessCmdKeys here :
link text
May be useful to you.
I'd want to create special class to handle instantiation and management of these special textboxes, and their "endownment" with these special event-raisers : you might want to keep a List<TextBox> of the ones currently being used to "track" them. Or, if you are sub-classing, List<SpecialTextBox>.
... note we're skipping over the whole issue of the possibility of a paste or copy being triggered by a selection from the TextBox default menu : are you enabling that ? Is that important to you ? Do you need to "suppress that menu" ? If you wish to still have that menu, and still "catch" the events : imho you are going to have to sub-class TextBox and define a WndProc and catch events like WM_Paste there. ...
There's a pretty complete example of code for sub-classing a TextBox and handling ProcessCmdKeys and trapping the kinds of events you are interested in here :
link text
But please note I haven't used or tested the above code myself, it was just filed away in my "archive" of snippets, but, at the very least, it will give you a basis for study ... if you have to sub-class.
How can I create a Delphi TSpeedButton or SpeedButton in C# 2.0?
Using a Button and setting the TabStop property to false only works when tapping through the form...
If you need (as I did) a button that does not get selected when clicking on it, there is only one way I have found to do it.
The way I did it, was to subclass the Button class and in the constructor calling the SetStyles and thereby setting Selectable to false, like so:
public class ButtonNoFocus : Button
{
public ButtonNoFocus()
: base()
{
base.SetStyle(ControlStyles.Selectable, false);
}
}
This worked out for me, and is perfect if you e.g. have a control-panel with buttons that perform actions to a selected object...
I'm wondering if you want to create a control like a TSpeedButton, or you just need same kind of end result ...
Programming one from scratch is certainly possible, but I'd only tackle that as a learning exercise.
Assuming you want to achieve a similar end result ...
Delphi's TSpeedButton had a differences from the standard TButton that developers found useful - it was flat, didn't take focus, and it consumed fewer resources than a regular button (because it didn't have an underlying Windows Handle).
Which of these are important to you?
If you just want a flat button that doesn't accept focus, use a regular Button with FlatStyle=Flat (or PopUp) and TabStop=false. You can configure a glyph by setting either the Image property, or a combination of ImageList and ImageIndex/ImageKey.
An alternative to this would be to look for an existing button component that comes close to your needs - one place to look might be the Krypton Toolkit (free to use, see http://www.componentfactory.com/toolkit_buttoncontrols.php).
If you're wanting to reduce the number of resources consumed by your application, it's likely you'll get a better return looking elsewhere.
Back in the days of Windows 3.1 (Delphi 1) and Windows 95 (Delphi 2), the number of available handles was strictly limited, with a maximum number available system wide. Today, with Windows XP and Vista, the number of available handles is far far higher, and the number is per process, not system wide. Unless you're creating thousands upon thousands of buttons, you're very unlikely to come anywhere close to running out.
Does this help? Looks like you would have to handle the OnPaint event, and not take focus...
The regular .net 2.0 button supports part of what a TSpeedbutton Does:
The Glyph: Image
Flat : FlatStyle
It does not handle:
Down
Group
These two are related, you could inherit from the button, and ownerdraw it, adding Down and Group features.
Codeproject has an example of ownerdraw buttons.