I have embedded an audio file (high.wav) in the resx file for my Windows Form (by just double clicking on Form1.resx in solution explorer, Ctrl+F4, then clicking 'Add Resource'), but using
Stream embeddedfile = WindowsFormsApplication2.Properties.Resources.ResourceManager.GetStream("high");
SoundPlayer sp = new SoundPlayer(embeddedfile);
sp.Play();
only plays a system sound, not the embedded one. I have tried all of the combinations of changing the Persistence from 'Linked at compile time' to 'Embedded in .resx', and Build Action from 'None' to 'Embedded Resource'. I have also tried
Stream embeddedfile = WindowsFormsApplication2.Properties.Resources.high;
which doesn't even compile, saying that 'WindowsFormsApplication2.Properties.Resources' does not contain a definition for 'high'
Sorry is this is a dumb question, I am just starting out. If it makes any difference, I am using Windows 8.1 N.
EDIT: I guess I fixed it. Apparently you cannot add resources by going (formname).cs --> (formname).resx . You must go right click the application name, go to properties, and on the sidebar thing go to resources. I don't know what the difference is, as things added the way that works shows up in the place where it doesn't work to add it.
For adding resource right click on the project name from project explorer then click on resources after add resource arrow ==> add Existing file from anyware
and this is code for playing this file added:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Media;
namespace play_Wav
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SoundPlayer sp = new SoundPlayer(play_Wav.Properties.Resources.myFile);
sp.Play();
}
}
}
Related
I was trying to create a round button. For this, we need to create a class, here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace CircularButton
{
internal class CircularButton :Button
{
protected override void OnPaint(PaintEventArgs pevent)
{
GraphicsPath g = new GraphicsPath();
g.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
this.Region = new System.Drawing.Region(g);
base.OnPaint(pevent);
}
}
}
After creating and coding this class, we need to rebuild the solution, after rebuilding, we must see the new CİRCLE BUTTON tool in the TOOLBOX:
However, I do not get neither this circule button option nor the "applicationName Components" tab:
I do not have both. How can I solve this problem?
Source of the images I use is this video:
https://www.youtube.com/watch?v=HG7hi9s7YhQ
Edit: If I right-click toolbox and click on "show all" ,it gives my tab which is named as "HarryPotter" and there is "CircleButton" tool in it, but I can not drag it to Forms because it is inactive(the relevant icon seems dark):
Change to public class, then rebuild (possibly Clean Solution first, then Rebuild solution) and reopen designer for a form. It should now appear in toolbox.
Before doing these steps, if you right-click on tool box and select show all it will show the tool tab you created and the custom tool you created with you class, but it will be inactive and appear with dark name and dark icon.
I have a problem with fo-dicom in C# form.
When I use fo-dicom to update a DICOM tag the form changes size and everything gets smaller.
The problem appear when I use dataset.Add
I created an example with only a simple form with a button, this is all the code:
using System;
using System.Windows.Forms;
namespace test_DICOM_view_problem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Dicom.DicomDataset dataset = new Dicom.DicomDataset();
dataset.Add(Dicom.DicomTag.Rows, "1024");
}
}
}
This is the form before clicking the button, and this after.
The problem appear only when I execute my form from Visual Studio GUI in Debug/Release mode, but it does not appear if I run the exe directly.
I uploaded my code here.
My configuration is:
Windows 10
.Net Framework 4.7.1
Visual Studio 2017 last version (15.6.5)
fo-dicom 3.0.2
Thanks for your help,
Davide
What i want to achieve is this: I have a solution with 3 projects inside. I've added these projects to the explorer folder, solution explorer and made a reference from projectA to projectB & projectC.
Now what I want is this, when I click a button on a form of projectA (the first project the user sees when he opens the EXE) like this:
private void projectbButton_Click(object sender, EventArgs e)
{
this.Hide();
projectB.form1 li = new projectB.form1 ();
li.Show();
}
that projectB form opens up.
This works but then the problem is that my other project is just hidden. Now is it possible to add something here that my projectA is fully closed and my projectB starts indepently. So that all the paths etc that are declared in my projectB forms like this:
inputStream = File.OpenText("gevaar17.txt");
are still valid. (because now I get a filenotfoundexeption since it's looking in the debug folder of projectA). And that when I search for a textfile in projectB form1 for example it doesn't go looking in projectA/bin/debug folder for the textfile. But that it goes looking in the debug folder of projectB?
thanks in advance.
private void projectbButton_Click(object sender, EventArgs e)
{
Process.Start("Project2.exe");
Application.Exit();
}
This will start the other project in a completely independent environment and the paths will work. Also, Process.Start() returns Process which you can use to do something if you want, just a tip. Add this to the usings:
using System.Diagnostics;
Currently I load an HTML string into a webBrowser control's contents and tell the user to print-screen it.
I'd like to somehow either grab the webBrowser contents as an image, or somehow render this HTML to an image file that can be saved. Are there any free libraries to do this?
I have been curious as to how this is accomplished myself. I have not tried to do this, but here is a link to a CodeProject article that seems to describe the process quite well:
HTML to Image in C#
I use a program called Webshot and I absolutely love it. It has a freeware version with upgrade available. It even has a callable command line interface which can be used in an automated fashion.
Link here.
Well, there's bound to be a better solution than this, but I'm pretty sure it's better than your solution (I haven't checked out the other answers), so...
I have a working C# program that saves a bitmap image of a control. Currently, the control is a textbox, but you could easily make it a web browser control. The entire program is below. The form has a button, button1, and a textbox, textBox1.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(textBox1.Width,textBox1.Height);
textBox1.DrawToBitmap(bm,new Rectangle(0,0,bm.Width,bm.Height));
bm.Save("image.bmp");
}
}
}
How to auto save all properties winforms when closed and auto load all properties winforms when load ? C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < Controls.Count; i++)
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Controls[i]));
Stream stream = File.Open("test.xml", FileMode.Create);
x.Serialize(stream, Controls[i]);
}
}
}
}
Your question is a little unclear, but
If you require saving/loading of the Form Layout have a look at
Windows Forms User Settings in C#
If you require saving/loading an object/class have a look at
Load and save objects to XML using serialization
EDIT:
This will show you how to persist certain settings for form properties.
Save and Restore Setings of a .NET Form using XML
Also have a look at The Application Automation Layer - Using XML To Dynamically Generate GUI Elements--Forms And Controls
All these will guide you in the direction you need to go.
I think the main objective here is
Figure out when to save and when to
load, and where to store/retrieve
these settings.
Are you storing these settings per
user? In a database? In a xml file?
Next you need to identify which
properties you will be
saving/restoring per control. Simple
location/size settings might not cut
it as controls will have various
complexities (Button, TextBox,
Gridview, ListView)
Now you need to figure out how to
iterate ALL controls on the form.
Buttons, Textboxes, Panels, Controls
in Controls (controls in panels), and
maybe even your User Controls. This
can be done using recursion.
Now you need to decide on the
structure of the xml file (if you opt
to use xml). This should pretty much
look like a tree structure, as you
would look at the form, and its
controls, and their controls, as a
tree structure.
I'm not aware of any automatic method built into the Form base class, but adding it yourself isn't hard.
You could tap the window load and close events to cache off all relevant properties to a backing store and then reload them later.
Register an event handler to the Form.Load and Form.Closing event handlers. When Form.Closing occurs, save the forms state to a file or database. When Form.Load occurs, check to see if a saved state is present and if so, reload the from from the saved state.
You have to manually code which properties to be saved.
A convenient method is to svae these personalized settings to Windows Forms Application Settings.
Sample code snippet:
//save the winform position and size upon closing
private void Form1_FormClosed(
object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.FormPosition = this.Location;
Properties.Settings.Default.FormSize = this.Size;
Properties.Settings.Default.Save();
}
//load the winform position and size upon loading
private void Form1_Load(object sender, EventArgs e)
{
this.Size = Properties.Settings.Default.FormSize;
this.Location = Properties.Settings.Default.FormPosition;
}
More references:
Using Settings in C#
Any good examples of how to use Applications settings
Use Windows Forms Application Settings to Personalize Your Applications
The process of turning objects like forms into something that could be saved is called serialization. Unfortunately I don't think there's an out-of-box way to serialize forms in WinForm. I did find How to Clone/Serialize/Copy & Paste a Windows Forms Control, and since forms are also controls, you might be able to serialize the properties using the code.