This is code for windows forms aplication:
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 WindowsFormsApplication6 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
How to create a file, file.txt, when you click on the button?
You have many methods you can use on File, like File.Create. This simply creates or overwrites a file in the local running folder:
private void button1_Click(object sender, EventArgs e)
{
if (!File.Exists("file.txt"))
{
File.Create("file.txt");
}
}
Use File.WriteAllText Method which creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.
private void button1_Click(object sender, EventArgs e)
{
File.WriteAllText("file.txt","your text ");
}
Related
Iam very new to coding so please pardon my lack of knowledge.
I made a program "Music player" that uses webBrowser to play video from youtube but from what i understood from other posts it cant work due to webBrowser being IE7. I dont neccessarily have to use webBrowser but it seemed like the best way.
Is there a way to "update" IE or do i need to use different method?
code:
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.Text.RegularExpressions;
namespace MusicPlayer_V4._1
{
public partial class Form1 : Form
{
string _ytUrl;
public Form1()
{
InitializeComponent();
}
public string VideoId
{
get
{
var ytMatch = new Regex(#"youtu(?:\.be|be\.com) / (?:.*v(?:.*/|=)|(?:.*/)?)([a-zA-Z0-_]+)").Match(_ytUrl);
return ytMatch.Success ? ytMatch.Groups[1].Value : string.Empty;
}
}
//user input (URL)
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
//play button
private void button1_Click(object sender, EventArgs e)
{
_ytUrl = txtUrl.Text;
webBrowser.Navigate($"http://youtube.com/v/{VideoId}?version=3");
}
//help button
private void button2_Click(object sender, EventArgs e)
{
}
//browser
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
The way moving forward is to use WebView2 which is basically the same thing as a WebView but based on the chromium-based edge. It will be always up to date on windows 10/11 machines and it works down to Windows 7.
https://developer.microsoft.com/en-us/microsoft-edge/webview2/
I am currently using WinForms Application and it seems that I have ran into a problem. I have a button that when pressed it outputs a link in a RichTextBox. However, the link is highlighted in blue but when I click it does not open in my browser. Here is the code(Note that https://something.com/ represents an actual link.):
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = "https://something.com/" + textBox1.Text;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
I am trying to make a room request program and have two windows that I need to connect to the database (which I know how to do). The two windows are called EMERGENCY and JANITOR. My issue is I don't know how to store information in a database. Like let's say someone logs in as EMERGENCY and sends a request, the information needs to be stored and once the JANITOR logs in, he can see the requests. So far I have the database but not sure about storing information in it without manually adding them. If anyone has any guides or links on how to do this, I'd appreciate it!
EMERGENCY window code:
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.Data.SqlClient;
namespace LOGINPAGE
{
public partial class EMERGENCY : Form
{
public EMERGENCY()
{
InitializeComponent();
}
void Fillcombo()
{
}
private void EMERGENCY_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'roomInfoDataSet6.Table' table. You can move, or remove it, as needed.
}
private void xButton1_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
private void xButton2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Search_Click(object sender, EventArgs e)
{
}
}
}
JANITOR code:
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 LOGINPAGE
{
public partial class JANITOR : Form
{
public JANITOR()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void JANITOR_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'roomInfoDataSet3.Table' table. You can move, or remove it, as needed.
this.tableTableAdapter.Fill(this.roomInfoDataSet3.Table);
}
private void xButton4_Click(object sender, EventArgs e)
{
}
private void xButton2_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
}
}
EMERGENCY
JANITOR
You can use Ms access db
You will find connecting to acess from c# to really easy with lots of YouTube video tutorials
I can have a table in it database that is updated by a "save emergency" button and a load emergency button by the janitor will just populate a datagridview with items from the same table filled with the first button
So for some reason when I code this.Close(); into my program it won't actually close it.
I have the program open a different .exe file then close, but I open my task manager and it actually is still running in the background. I have this issue with the "Close" option on my context menu also.
Any ideas why?
EDIT: Even when I exit with the button, it's still in the background.
EDIT:
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void launch_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(Environment.ExpandEnvironmentVariables("%AppData%\\program.exe"));
this.Close();
}
Only actual two codes that are closing the program. But it's still running in the background.
Even when I exit through the actual x button, it still runs in the background.
You must close the process that you've created. Create a variable to save the process that has been created and when you close the form or press another button close process. See: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.close.aspx
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
private Process _process;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this._process = Process.Start("notepad.exe");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this._process != null) {
this._process.CloseMainWindow();
this._process.Close();
}
}
}
}
As menstion in above comment add using System.Diagnostics; to your code and try this code
public Form1()
{
InitializeComponent();
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
}
void Application_ApplicationExit(object sender, EventArgs e)
{
Process.GetCurrentProcess().Kill();
}
I have the following form. The first button opens a text file and displays the file in the rich text box in the form. The second button opens another window. What I want is for that window to be pre-populated with the data that is in that text file...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HomeInventory2
{
public partial class Form2 : Form
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Run(new Form1());
}
}
}
The form that needs to be populated
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HomeInventory2.Domain;
using HomeInventory2.Business;
namespace HomeInventory2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void submitButton_Click(object sender, EventArgs e)
{
CreateInventory create = new CreateInventory();
create.ItemAmount = textBoxAmount.Text;
create.ItemCategory = textBoxCategories.Text;
create.ItemProperties = textBoxValue.Text;
create.ItemValue = textBoxValue.Text;
InventoryMngr invtryMngr = new InventoryMngr();
invtryMngr.Create(create);
}
}
}
Create a new constructor which takes a string overload. When you open the new Form, pass in the text date and fill the textbox.
//in the new form that opens up
public Form1(string prepopulated)
{
InitializeComponent();
myRichTextbox.Text = prepopulated;
}
And call it from your click event like this:
//in the first form
private void button2_Click(object sender, EventArgs e)
{
Application.Run(new Form1(richTextBox1.Text));
}
If your content is more complicated than a simple text file, you can use RichTextBox.Document instead and pass that instead of the string. Change the overload to
Form1(FlowDocument prepopulated)
and call it like this
Application.Run(new Form1(richTextBox1.Document));