C# - Drag and Drop files in win form [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make like a container application where you can drag and drop files of any kind on the form and afterwards to be able to open it from there. I found some solutions where you can drag and drop files to a list view and you get it's path.. but is not how I want.. I want to have on my form in a panel or what ever is better like a shortcut of the file, an image or something to be able to see the file icon like is in explorer.
Have someone ever done something like this or point me to the right direction?

Set "Allow drop" property to "true" on your control and make use of Control.DragDrop event - it's exist on all controls, and it's invoked after drag'n'drop anything on anything(if "Allow drop" is true of course).
It this event-handler you can add new item to this or another control(ListView fits nicely to your needs), and for example to some "Dictionary" where you will store "Item and filename mapping".
Also you need to make handler for item click'ing - for ListView there a ItemActivate event. Inside this handler you can click execute default shell-action for this file by using Process.Start

Related

C# Code splitting [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I am using Visual Studio in C# (.Net 4.5). I have a lot of code behind one button and it gets confusing. Can anyone tell me how I can split that code and put some of it in a separate file and link the file to the main one? I don't wish to create classes and all, operations behind that button should execute in a sequential mode. This must be simple but I can't find the solution on the web. Perhaps it is even too simple... Forgive me, I'm a total noob!
There is nothing wrong with having class at all, and it makes thing less convoluted and clear. It's actually exactly what you want to address, as you're not happy with the code you have.
Try this, and if you're not happy with outcome, keep having long, "pythonish" function
Split your button big logic into semantically pronounced pieces, even with comments and moving code up and down
Create a class
Move pieces created at 1st step into separate distinct functions
Call them sequentially, as you want, into the "big" function of a class you've created in 2nd step
Call that single function of newly created class from your Button click handler
Make your class Partial and move different functions created on 3rd step into different files
This is very bad practice. This anti-pattern is magic pushbutton. Changing the user interface (or adding a new interface) becomes difficult, for example, deleting a button may result in the removal of related methods.
More information: https://en.wikipedia.org/wiki/Magic_pushbutton
Perhaps this page in the visual studio documentation is what you are looking for.
In Microsoft Visual Studio, Solution Explorer, right-click the
project to which you want to add an existing item and click Add
Existing Item.
In the Add Existing Item - dialog box, browse to and select the file that contains the item you want to add.
Click Add to create a copy of the selected file, or click the down arrow to the right of the Add button and click Add As Link to create a link to the file instead of creating a copy of the file.
https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUID-9C205780-8091-4AFE-9480-5BD69711B806-htm.html

How to hide elements from the form editor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
so I'm making a chat room, and it has several different scenes. However, for maximum efficiency, I need to have a clean workspace. However, the start here:
First scene. Is hard to have in the background when I'm placing stuff around. I know how to hide it once it runs, but is there any way to keep it alive, but invisible when I'm editing?
No there is no that possibility but there are 3 others (second one i do not prefer and would NEVER use but it is possible)
So you want to hide some elements in editing process because you are doing something behind them. From picture i see that first one is some kind of login form. So these are solutions:
1. Creating another form
So most common and practical is creating another form.
Simply create form with right click on your solution explorer > new > Windows Form
Then call it with buttons
Calling:
NewForm nf = new NewForm();
nf.Show();
this.Close();
2. Temporary moving it off
So idea is to move controls to the side in editing mode and then when you run program you set their location by code with simply doing button1.Location = new Point(15, 15);
3. Using TabPage
Create TabPage with 3 tabs.
First one are buttons, Second is if user want to create third if want to join.
Add this to your tab so it looks better and have no buttons at top (to user swich tabs)
tabControl.Appearance = TabAppearance.FlatButtons;
tabControl.ItemSize = new Size(0, 1);
tabControl.SizeMode = TabSizeMode.Fixed;
Then on button go to specific tab and boom.

Is this possible to create many form designs and swap between them? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've just started building up a simple game in Visual Studio using c# (It is my first low difficulty game). As I open it , it shows me a background image and 2 buttons, "Play" and "Instructions."and I want the form to get changed as I click the play button and open a new menu interface , like : " Game Difficulty / Character Appearence "(those 2 are buttons) , with a new background image. When I click one of them, another menu, etc.
Can I create multiple designs( Form1.cs[Design1/2/3]) for my from and swap between them ? If not, then how can I do something similar ?
In my opinion, the easiest way would be to make each menu a "User Control" that is the size of the main form, and then you can add a user control for whichever menu you want to show up as you go along.
For example, in the solution explorer I would create a folder and name it "Menus". Then I would create a new "User Control" in there. Copy the Width and Height of the form it will be placed on and apply that to the new control. Then you can add in all the buttons and images you like. Once you're finished, in your main form, you can add the "main-menu" control you made in the designer. For any submenus you want, create user controls for them too. Then, if you want a submenu to show up, capture a click event for the button you want and programmatically add it to the parent form using FindForm().
Pretend that DifficultyScreenControl is the screen that shows up after I click a play button located in my main-menu user control.
Inside of a menu user control
private void PlayButton_Click(object sender, EventArgs e)
{
DifficultyScreenControl DiffScr = new DifficultyScreen();
this.FindForm().Controls.Add(DiffScr);
}
Also it is a good idea to either Dispose() or remove these screens one they can't be revisited again as it can free up resources being taken up by them.
Feel free to ask me questions or comment on things to change!

C# ParentForm.ActiveControl.Name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
if (Operators.CompareString(this.ParentForm.ActiveControl.Name, this.Name, false) == 0)
{
base.Focus();
}
From What I have experience in VB6,the above code doesn't work because they are never equal if the users didn't change to the same name.
From Example, the UserControl Name is ucCalendar, When I drag to my From, the name will automatically change to ucCalendar1,even though I can change to ucCalendar but usually we won't do that.
I think the coder want to compare whether the UserControl is the only control or ActiveControl on the Form so that he can force to focus it.
I don't know this C# works or not. Please tell me.
There is nothing in the WinForms code saying that two controls can not have the same name. The reason you think that is that you're looking at it from the designer perspective, when you use the designer it won't let you have two controls with the same name just because it uses there as field names for them in the code, and as you probably know there can not be two fields / properties / variables with the same name in the same scope. As a matter of fact there is no need for the Control's Name property to be anything.

Replace all string values with another within a project - C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In my current project I want to implement a checkbox that, if checked, replaces all strings in labels, tabs, etc. currently being shown on a form with a different string.
For example, If checked, all instances of the word "car" would change to the word "truck" all through out the program.
I'd rather not go through a do a .replace on every single string in the code. I was wondering if there was some way to "intercept" output strings and replace them on the fly; something like making a string-listener. Any help would be appreciated!
I am no GUI/WinForms program but this would be my personal approach. Add all of these UI elements to a List<T> in the forms constructor. Then in the "replace box checked event handler" you can just iterate over the list applying the same change to all the items. It's by no means a perfect solution but it does mean you only have to statically reference each of the items once. After they're in the list you can operate on all of them very easily.

Categories