OpenFileDialog closes form - c#

I have a weird error, when I try to open a filedialog it goes back to visual studio and that's it, it just closes the form.
Here is my entire code, its just a test form with 1 button on it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Week7_Oprd1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog ofd = new OpenFileDialog();
private void button1_Click(object sender, EventArgs e)
{
ofd.ShowDialog();
}
}
}

So after messing around with it for endless time I asked one of my teachers to help me.
The problem was that the debug was set to run with any cpu.
after setting it to x64 it worked.

Related

Changing the background image of a form when a progress bar expires

I am trying to make a program that once a "fake" progress bar expires, the background image of the form changes to another one. This is my first time using forms as a GUI, so I would really appreciate the help. If possible, please explain how the code works. I'm using Visual Studio 2017. Here's how the code looks like.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Form_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
}
}
}
in this event you have to check if the progressbar value is 100
it'll be like :
private void timer1_Tick(object sender, EventArgs e)
{
if(progressBar1.Value<100) // set progressBar max value to 100
{
// if the value smaller than 100, will increment.
this.progressBar1.Increment(1);
}
else{
timer1.Stop(); // Important to stop the timer
// here you change the background image of the form.
this.BackgroundImage = // choose ur image location.
}
}

Why form.exe freezes after clicking button 3 times?

I launch the C# Form (Via EXE file) on the form I have a button which executes a URL with perimeters (without opening a browser) if the button is clicked more than 3 times (can be at any time) it just completely freezes the program
How can I stop this from happening?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "apiID=2794187564564";
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(
"http://fms.psrpc.co.uk/apistate2.php?" + postData);
// Send the data.
myRequest.GetResponse();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
}
}
Don't forget to call Dispose on all IDisposable types. Although it is not clearly visible here, this applies also to the result of GetResponse.
// Send the data
myRequest.GetResponse().Dispose();
Also note that you should put it inside try-catch-finally because GetResponse throws an exception when that http server returns something different than 2xx OK return codes.

data not saving in textbox properties.settings

I am working on a project where I want to save the text that is in the textbox when im closing the application, and when I open the application again I want it to be there.
I am trying to save the text data in the app.config as you can see in the code below but when I close the application and run it, the text I put in is not there.
Im really confused because when the form loads it runs this
textbox1.Text = Properties.Settings.Default.SavedText;
and it should pull up the "saved text" which is being saved when the form is calling the Closing event
Properties.Settings.Default.SavedText = textbox1.Text;
Properties.Settings.Default.Save();
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework;
using MetroFramework.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : MetroForm
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textbox1.Text = Properties.Settings.Default.SavedText;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.SavedText = textbox1.Text;
Properties.Settings.Default.Save();
}
}
}
I was also getting these errors before and I tried showing the potential fixes and use them but they didnt solve the issue with the text not saving.
This were the potential fixes.
I solved the issue by going into my project properties and adding this to the project settings. SavedText > String > User.

How do I save the first text in a textbox and not overwrite it C# .NET

I am working on a very basic project where I can copy something to the clipboard and it saves it in a RichTextBox in my application. I've made it loop through and check the clipboard every 0.5 seconds with a timer but how do I make the first copy stay in the TextBox because what it does now is:
-I copy something to the clipboard
-It sends it to the TextBox
-When I copy something else it overwrites it
How do I make them add one after the other?
This is what I got so far;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CBR
{
public partial class mainFrm : Form
{
public mainFrm()
{
InitializeComponent();
}
private void mainFrm_Load(object sender, EventArgs e)
{
}
private void clipboardUpdater_Tick(object sender, EventArgs e)
{
richTextBox1.Text = Clipboard.GetText();
}
}
}
Seems like this is what you're looking for;
private void clipboardUpdater_Tick(object sender, EventArgs e)
{
if (!richTextBox1.Text.Contains(Clipboard.GetText()))
{
richTextBox1.Text += Clipboard.GetText();
}
}
If you want to seperate each paste, replace the statement with this;
richTextBox1.Text += " " + Clipboard.GetText();

Check if all elements on a form is fully loaded C#

I am developing a C# application using Visual Studio 2015
it has 2 forms, on form1 I have a button that when clicked
shows form2, now what I would like to do is print form2
after it has fully loaded, I am using the printform control
on form2 to do this, if I use this on the the form_load event
it prints a blank page and then shows the form, I have
also tried using it on form_Shown, however this prints a box
where the elements are but not the element itself as if they have
not finished loading, there is probably a better way to do this
but I am new to C# so still learning
Below is an example of the code that I have on form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyApp
{
public partial class Form2: Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.Shown += new System.EventHandler(this.Form2_Shown);
}
private void Form2_Shown(object sender, EventArgs e)
{
printForm.Print();
}
}
}
The Shown event fires a wee bit too early. The form's frame and background is painted but the rest follows later. Painting is a low priority task that occurs only when nothing else needs to be done, firing the Shown event happens first.
The workaround is simple, just ask the form to finish updating itself by calling its Update() method. Fix:
private void Form2_Shown(object sender, EventArgs e)
{
this.Update();
printForm.Print();
}

Categories