How can i create an OpenFileDialog with Radiobuttons? [duplicate] - c#

I am working on winforms application in C#. What I want to achieve is to get a file from user for which I am using the following code:
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string sFileName = dlg.FileName;
//my code goes here
}
Now, everything is working fine but I want to put 3 radio buttons in the same dialog box, meaning I would now get two things from this dialog box
string sFileName = dlg.FileName; //same as in case of traditional dialog box
//some thing like this which tells which radio button is selected:
dlg.rbTypes.Selected
How do I achieve this?

Yes, that's possible, I did the same kind of customization with SaveFileDialog successfully and it's pretty interesting.
Follow the following links:
http://www.codeproject.com/KB/dialog/OpenFileDialogEx.aspx
http://www.codeproject.com/KB/cs/getsavefilename.aspx
http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx
Also my own questions too will help you:
Change default arrangement of Save and Cancel buttons in SaveFileDialog
How to stop overwriteprompt when creating SaveFileDialog using GetSaveFileName
You have to use the WinAPI for this and you need to write the ShowDialog method in your own calling the GetOpenFileName windows function inside it, instead of calling .net's OpenFileDialog. The GetOpenFileName will create the windows OpenFileDialog. (Refer to http://msdn.microsoft.com/en-us/library/ms646927%28v=vs.85%29.aspx). This together with writing the HookProc procedure and catching events such as WM_INITDIALOG, CDN_INITDONE inside it will help you do what you want.
To add radio buttons etc., you have to call the windows functions such as CreateWindowEx and SendMessage....
The 2nd link has the exact direction to the customization...
Ask for any clarifications...

On XP you need to use the hook procedure method and the GetOpenFileName API. On Vista and later this will result in a horrid looking file dialog with limited utility, e.g. no search. On Vista you should use IFileDialog and to customise the dialog you need the IFileDialogCustomize interface. Because the new Vista dialogs are exposed as COM interfaces they are quite easy to consume in .net.

Related

Controlling and copying text from another application in Windows CE

I've been using a windows CE application that produces a text in its textbox when its button was clicked. I need this text for my C# app. How can I trigger its button and copy its text to my application.
private void button1_Click(object sender, EventArgs e)
{
Process myProcess = new Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "\\Program Files\\kangkong\\Barcode2005.exe";
myProcess.Start();
// minimize Barcode2005.exe and the code goes here...
}
catch
{
throw;
}
}
Anyway, I'm using C# in Windows CE
Thanks :)
This is similar to your other question c# how to get handle over a specific mainmenu using coredll.dll
You need to define some p/invokes to be able to use the native FindWindow, GetWindowText functions.
First use FindWindow with the class name and/or title of the foreign window to get a handle to the main window. You may also use the processID (pID) to identify the foreign window.
Then enumerate all child windows of the main window until you find the window of the textbox (in native API an TextBox is using the EDIT class and is just a window like everything else you see). Then you can use GetWindowText using the found handle of the EDIT window.
If the foreign window is a dialog, you have to add GetDlgItem to your p/invoke list. Then you can use the EDIT dlg item's control ID to identify the input field and then use GetWindowText using this handle. AFAIR the control IDs are available in the resources of an windows exe file (use ResHacker to get the IDs).
BTW: where comes barcode2005.exe come from? Can you upload a copy for analysis? Is this a barcode graphic generator or decoder? If so, there are easier ways to get barcode graphics or decodes in C#.

how to display textBox control in MessageBox?

Any idea how to display textBox control in MessageBox.
I'm working on winforms projcet c#.
Thank you in advance.
You can't. MessageBox is a special container designed to only show a message and buttons. Instead, you can create your own Form with whatever controls you want, and use .ShowDialog() on it.
You can simply add an Input box from VB.NET into your C# project.
First add Microsoft.VisualBasic to your project References, then use the following code:
string UserAnswer = Microsoft.VisualBasic.Interaction.InputBox("Your Message ", "Title", "Default Response");
And that should work properly.
It will be better to add a new Form in you application which you can customize the way you want.
and just call it from where ever required.
you could create a classic win form that looks like a message box and opening it as a modal form
perhaps using Form.ShowDialog
more info at
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
You cannot customise the MessageBox, its better you use a popup designed using Windows Form separately and use its instance to invoke.
customPopup popup = new customPopup();
popup.ShowDialog();
Place your controls on the popup form and set their access modifiers to public if you want to access the textboxes or labels etc in your previous form.
customPopup popup = new customPopup();
popup.msgLabel.Text= "Your message";
popup.ShowDialog();
As I know there is no way to do that.
You can create a winform change it's style to look like a MessageBox and add your own controls.
Yes, as krillgar mentioned,you should create your own form. And
1. Encapsulate the form in a static class or function, so you may just call MyMessageBox.Show().
2. The textbox should have readonly=true, so the end users won't be able to change the text displayed, while they could select text and copy to clipboard.
Regarding to item 2, I believe many Windows build applications and MS Office use such approach.
using Microsoft.VisualBasic;//add reference
var Password = Interaction.InputBox("Message", "Title" ,"information in textbox", -1,-1);
In the variable "password" it receives the information that is entered from the text box.
Remember to add the reference "Microsoft.VisualBasic" in the solution explorer
Solution in here, you can create windows form and design it, set form is dialog, when you call form, it is auto show. In form you design, you set value some parameter static where other class in project, but you should set when you close form design that, OK, come back form init call show dialog, you create interval call when have == null return call, when != null you stop call back and using parameter in class static it !

C# WinForms Wait on form to proceed

I'm tired and hungry, so I might of missed it, but from what I can see no existing post covers this...
I'm writing a plugin for an application. My plugin loads a form to get some data specifically, it uses the webcam to scan for a barcode. Once it's found a barcode, the form hides itself (incase it's needed again later). This is how I currently call the form that does the barcode work:
string readData = null;
if (eye == null)
{
System.Windows.Forms.Application.EnableVisualStyles();
eye = new CamView();
}
eye.Show();
if (eye.found)
{
readData = eye.readData;
}
return readData;
So, my problem is that eye.show() doesn't block. It makes the form appear and carries right on before there's a chance for the barcode to appear. I imagine I need to use some form of threading or locking, but my crude attempts to do so have just frozen the interface completely.
The "eye" form is basically just a viewfinder for the webcam, and relies on the camera_OnImageCapture event to make it do it's image checks for the barcode.
Is there an elegant way to make the application calling the plugin wait for the form to finish? Or do I just need to add an accept button to the "eye form?"
Cheers. And humble apologies if this is in anyway a repost.
.ShowDialog();
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
"You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed."
You are on the right track. You change the code to show CamView as a modal dialog but do no add an Accept button. Instead change camera_OnImageCapture to close the dialog.

Force dialog close in C#

I'm writing a GIS application in C#. A portion of the application allows the user to select a KML file, and then the program will process that file. I'm using an OpenFileDialog, but the problem is that all of the code is executed before the dialog gets closed (and after the user has OK'd the file). It takes quite awhile because the program has to zoom and do other things. Is there a way to close the dialog programmatically before my code is executed?
EDIT: Some code for those who ask.
private void OnKMLFileSet(object sender, CancelEventArgs e)
{
Polygon polygon = KmlToPolygon(openFileDialog2.FileName);
// After this, I no longer need the file, but the dialog stays open until the end of the method
Graphic graphic = new Graphic();
graphic.Geometry = polygon;
textBox1.Text = string.Format("{0:n}", CalculateAreaInSqKilometers(polygon)).Split('.')[0];
textBox2.Text = string.Format("{0:n}", CalculateAreaInSqMiles(polygon)).Split('.')[0];
textBox3.Text = string.Format("{0:n}", CalculateAreaInSqKnots(polygon)).Split('.')[0];
Note polyInfo = new Note("Polygon with nautical area: " + textBox3.Text, polygon);
map.Map.ChildItems.Add(polyInfo);
map.ZoomTo(polygon.GetEnvelope());
}
It sounds like the dialog is actually closed, but it's still "visible" because the main window is busy and hasn't repainted itself yet.
Some ideas:
The easy way: call the Refresh() method on the main form where the dialog is still visible. Always call it immediately after ShowDialog returns.
If loading takes quite a bit of time, it might be desirable to create a pop-up "loading" dialog, possibly with a cancel button. Use the BackgroundWorker class to load the file in a background thread. When the worker is done, the file is loaded and the pop-up window can be closed. Remember not to change anything in the user interface from the background thread without proper synchronization.
EDIT: After looking at the code, I think I see your problem. You're handling the FileOk event. This will have the effect you are trying to avoid. Use the dialog like this:
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
// open file
}
Don't use the FileOk event. I've never had reason to use it before... Also it might be helpful to follow the advice I already gave.

Message box with Show Details Option?

I need to show a MessageBox with the Show Details option , like what we get during normal windows exceptions. When the user clicks the show details option, it has to expand and show the collection of details to the user. How to achieve it in Windows forms?
Thanks in advance
These dialogs are not exposed as part of any API and so you need to create your own dialog that behaves the same way.
A dialog is simply a standard form shown using the ShowDialog method:
DetailsMessageBox dialog = new DetailsMessageBox();
if (dialog.ShowDialog() == DialogResult.OK)
{
// Handle me
}
Before your dialog closes you should set the DialogResult property of the form is set to the desired dialog outcome - you can get buttons to automatically set the dialoig result (and close the dialog) for you when they are clicked by setting the DialogResult property of the button to the desired result.
You should also set the AcceptButton and CancelButton properties of your dialog to suitable buttons so that the dialog is closed when the use presses Escape or Enter.
These types of forms are only via API available on Windows Vista and higher. You'd best just create your own form. That way you can make it precisely how you want it :)
The simplest way would be to make your own messagebox. You can then add as much additional functionality as you'd like, including event/error logging etc.
You can try the TaskDialog API, see http://msdn.microsoft.com/en-us/library/windows/desktop/bb787471(v=vs.85).aspx

Categories