Custom Message Box That Blocks - c#

How can I re-implement a modal message box in WP7.1 to display custom design?
I saw number of questions with answers, both here on SO and on the Internets as well.
However, all the solutions I saw so far was non-blocking. In my app, I sometimes use message boxes to confirm something, so I need the solution that blocks the UI thread just like the system-provided MessageBox class, i.e.
if( CustomMessageBox.confirm( "orly?" ) ) { ...
Update: decompiling System.Windows.dll from the simulator image revealed that MessageBox class is merely a thin wrapper around MessageBox_ShowCore in agcore.dll, which apparently calls MessageBoxW from coredll.dll..

You can use the Popup to design your own messagebox. Just like #MengMeng mentioned above, you can dive into the source code of the MessagePrompt in coding4Fun, then you can fnd out how to implement it.

Related

Is there a way to implement a Redo() function for TextBox? C#

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.

Red Cross in DevExpress XtraGrid

Sometimes an XtraGrid gets a red cross instead of data.
( http://www.devexpress.com/Support/Center/p/CB4246.aspx , http://www.devexpress.com/Support/Center/p/A884.aspx and so on )
I have enabled to break on exceptions (Ctrl+D, E, selected all exceptions) - but am not sure if anything else can happen so that the red corss comes.
Is there an easy way if you are in the state "XtraGrid shows red X" that the XtraGrid can get to work again (as far as i seen only closing a form and reopening it helped).
I'm searchign for something like
GridView gridView = this. GridViewXYZ(objectview);
// objectView is refreshed
gridView.RefreshData(); // <- this can thorw an exception that data is not available / UI will get an red cross
// ??? do something to reset the gridView in a workig condition
From my previous GDI experience once you're in this state you're stuck. You've done something that has caused a problem within GDI and it's not managed to throw an exception/recover from it.
The act of closing/re-opening the Form causes the un-managed GDI object for it to be released and re-created, and hence GDI is fully operable again, until the same error condition occurs. If you can't prevent this error, you could have a look at trying to re-create a new GDI object for the form/control but I'm not sure how you'd go about doing so.
Really this is a bug which the DevExpress team should probably solve.
You could try to call BeginInvoke on the Grid.
Action a = () => gridView.RefreshData();
gridView.GridControl.BeginInvoke(a);
Whenever fiddeling with the datasource, you could try wrapping it in a statement like this.
I have expirensed something simular, and you be supprised how often some like this works.
Is there an easy way if you are in the state "XtraGrid shows red X"
that the XtraGrid can get to work again
Yes if you never get into the error state in the first place. Otherwise workaround it by closing/reopening.
The article you link to has info on what causes the "Red Cross of Death" to appear, either a unhandled exception occurs during painting, you handle a control's CustomDraw event and an exception occurs or methods are being called asynchronously.
If you have difficulty in determining the cause of the problem, please try to recreate the issue in a sample project and send it to the DevExpress Support Team.
Having same problem sometimes with Visusa Studio extension CodeRush (also provided by devExpress) when showing references dialog. So it seems to be a internal problem. Hope the next version of devexpress will fix the problem.

AccessViolationException when interacting with RichTextBox

I'm aware there is a bug with .Net 2.0 with LCG which generates AccessViolationException when certain IEnumerable<> and similar objects are used; and that there is a hotfix.
I have a project in .Net 4.0 using WinForms running on Windows 7 x64. I have a very simple form - nothing but a form and a RichTextBox docked to take up the whole client size.
From a background thread, I periodically call an update method on my form that does something similar to the following:
public static void Log(string text)
{
Invoke((MethodInvoker)(() => {
lock(richTextBox) {
richTextBox.Text += text;
}
}));
}
I do actually have both the inside of the invoke and the outside in a try/catch (Yes, I know this is horrible!) and I'm doing some additional things like putting the caret to the end and scrolling to it. I'm also using StringBuilder but this is all beside the point:
Rarely, but inevitably, while debugging, my IDE detects an AccessViolationException somewhere in the code that's updating the text box. It's sometimes in the bit that updates the text, it's sometimes at the bit that makes the selection, and it's sometimes at the bit that scrolls to the caret. If I press F5 to continue, I usually don't see the problem for some time and the application continues as before.
There is nothing fancy happening with this text box. There is no race condition (first of all because I have a lock), but also because there is simply nothing in my code that would try and write to the text box at the same time as something else is trying to.
Any idea why this may be happening? I'm sorry, I don't know what other information I can put here, as I'm not even sure why this is happening in the first place.
Not sure what the rest of your code is doing but I reckon the problem is with lock(richTextBox). If the GUI thread is doing something with the RichTextBox, the lock will not work Or if you are accessing this RichTextBox in some other thread.

AxAcroPDF swallowing keys, how to get it to stop?

The AxAcroPDF swallows all key-related events as soon as it gets focus, including shortcuts, key presses, etc. I added a message filter, and it doesn't get any key-related messages either. It's a COM component, could that be relevant?
Is there any way to catch these before the control starts swallowing them?
Hans is correct, the Acrobat Reader spawns two child AcroRd32 processes which you have no direct access to from within your managed code.
I have experimented with this and you have three viable options:
You can create a global system hook, and then look for and filter out / respond to WM_SETFOCUS messages sent to your child AcroRd32 windows. You can accomplish some of this from within C# by using a wrapper library, such as the one here: http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
You also need to identify the correct processes as there may be more than one instance of your application, or other instances of AcroRd32. This is the most deterministic solution, but because your application will now be filtering messages sent to every single window in existence, I generally don't recommend this approach because then your program could negatively affect system stability.
Find an alternate PDF viewing control. See this answer for a few commercial components: .net PDF Viewer control , or roll your own: http://www.codeproject.com/KB/applications/PDFViewerControl.aspx
Find an acceptable hack. Depending on how robust your application needs to be, code such as the following may be suitable (it was suitable for my case):
DateTime _lastRenav = DateTime.MinValue;
public Form1()
{
InitializeComponent();
listBox1.LostFocus += new EventHandler(listBox1_LostFocus);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axAcroPDF1.src = "sample.pdf"; //this will cause adobe to take away the focus
_lastRenav = DateTime.Now;
}
void listBox1_LostFocus(object sender, EventArgs e)
{
//restores focus if it were the result of a listbox navigation
if ((DateTime.Now - _lastRenav).TotalSeconds < 1)
listBox1.Focus();
}
I might finally have a ridiculously simple answer. So far in testing this is working.
Having suffered from this problem for quite some time and having built a complex system of each custom control recording which of them last had focus and using a timer to flip focus back (when acropdf grabbed it) I revisited this problem and read a great number of answers (looking for recent solutions). The information gleaned helped me with the idea.
The idea is to disable the (acropdf) control whilst it is loading as in the following example (code reduced for clarity)
AxAcroPDF_this.Enabled = False
AxAcroPDF_this.src = m_src
Then on a timer, after say 1 second.
AxAcroPDF_this.Enabled = True
Basically the idea is to tell Windows not to let users use the acropdf control until allowed, so asking Windows to prevent it from getting focus (because users are not allowed in there).
So far this is holding up, I will edit this if anything changes. If it doesn't work completely for you then maybe the idea points into a useful direction.
It is an out-of-process COM component, that's the problem. Completely in violation of Windows SDK requirements as laid out in SetParent(). Once its window gets the focus, the message loop in the acroread.exe process gets all the messages, your message filter cannot see any messages anymore.
Technically it is fixable by using SetWindowsHookEx() to inject a DLL into the process and monitor messages with WH_GETMESSAGE. But you can't write such a DLL in the C# language.
Major suck, I know. There never seems to be any lack of it with that program.
For some reason Tim's answer, disabling the AxAcroPDF control directly, didn't work in my case. The Leave event on the previously-selected Textbox would never fire, either.
What is working is nesting the AxAcroPDF control inside of a disabled GroupBox. Since the users of my application need to only see the PDF, not interact with it, the GroupBox's Enabled property is set to False in the designer.

Shape of a Winforms MessageBox

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.

Categories

Resources