After upgrading the code from .NET core3.1 to .NET6, and after starting using the VS2022 from VS2019, I am not able to see the default WinForms icon for my windows application.
Does anyone know how to solve this?
I tried looking for the icon properties for the project but could not find the default icon option in VS2022, which I can see in VS2019.
First you can search for properties:
Select the icon in the categorized:
The second method:
project->properties->Applications->win32 resources
Update:
You could use this to get it.
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Icon icon = this.Icon;
Stream output = new FileStream("saved_icon.ico", FileMode.Create);
icon.Save(output);
output.Close();
}
}
}
Related
I'm using Visual Studio 2019 and Windows Form (.NET Framework), I have a windows form that has a button. I want to print something to the console after clicking this button named 'btnPrint'. I don't know what codes to put in it.
I tried Console.WriteLine("Hello World!") but no console was shown. I wait for a few minutes hoping that something shows up, but it takes a long time, so I terminate the program.
This is my 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 Windows
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnPrint_Click(object sender, EventArgs e)
{
Console.WriteLine("Hello World!");
}
}
}
It looks like you're running a Windows Forms application. By default, the Console you're trying to access with Console.WriteLine() is not available. This command only works for Console Applications (See the description in the official documentation).
If you're running your application inside of Visual Studio you should see the Hello World! message in Visual Studio's output window.
Some ways to add output to your code:
AllocConsole
If you would really want a Console to be open for your Forms application. You can have a look at this answer.
This will open the Console so you can use it. However, if you close the console, your whole application will close.
This is the code, copied from the answer linked above:
using System.Runtime.InteropServices;
private void Form1_Load(object sender, EventArgs e)
{
// This will open up the console when the form is loaded.
AllocConsole();
}
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
RichTextBox
Another way to get sort of a Console for your Form application is to add an output to the form itself, like a RichTextBox. And write your message to the RichTextBox like so:
// Print the text
MyRichTextBox.AppendText("Hello World!\n" + output);
// Scroll the RichTextBox down
MyRichTextBox.ScrollToCaret();
Debug log
Instead of Console.WriteLine() you can also use Debug.WriteLine from System.Diagnostics if you attach a debugger like Visual Studio to the Form application. The output will be shown in the output window of Visual Studio.
This will work even after you've build the application.
Console Application
You could also create a Console application so the console will always be up. In the Console application you can then create a new Form to do all your From interactions with.
It's a bit of a workaround, but it should work.
You should make a label like this,
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 Windows
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnPrint_Click(object sender, EventArgs e)
{
Label Show_Text = new Label();
Show_Text.Text = "Hello World!";
Form1.Controls.Add(Show_Text);
}
}
}
Other preferences on c# Labels go to more about c# labels...
I'm new to C# and Visual Studio. I wrote a simple forms app that works great in debug. When I build and run the exe everything works except MessageBox.Show() does not pop up any message. So I built a new project with one form, one button, and one event handler.
Sure enough. The message box pops up when using Start With Debugging.
Nothing happens when I click the button with using Start Without Debugging.
Some research pointed me at:
[ComVisibleAttribute(true)]
public enum UIPermissionWindow { AllWindows }
I added that and still not pop-up from any message boxes.
The project is as basic as possible for troubleshooting.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
[ComVisibleAttribute(true)]
public enum UIPermissionWindow { AllWindows }
namespace Form462
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) => MessageBox.Show("Help!");
}
}
Windows corruption is clearly the culprit. MessageBox.Show() works perfectly normal on another server.
One other odd thing on this server, Can't seem to open dialog boxes in Visual Studio.
File --> Open ---> New Project Solution.
Nothing happens. I see the little Ready Icon in the way bottom left blink, nothing else happens. I have no updates for Visual Studio or windows.
I have a simple "login" screen in C# where the user will press Login and another window pops up saying welcome and is working when I build/run on Visual Studio.
Here is the 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 Login_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Welcome");
this.Close();
}
}
}
I also have a VBA form (Access 2010) where a user will click a button, then the C# form should pop up and in the future, instead of saying "welcome" it will redirect to a different form in Access.
Here is the code for the VBA button:
Private Sub Command284_Click()
Dim objLog As Login_Test.Form1
Set objLog = New Login_Test.Form1
End Sub
I have made the .dll, .tlb, used regasm on it, and referenced it on Access.
I get: "runtime error 429 activex component can't create object" when pressing the button.
Am I missing something?
Launch developer command prompt in Admin mode, run this synatx:
regasm.exe Login_Test.dll /tlb /CodeBase
You can keep Login_test.dll anywhere, just provide the full path in command.
I'm currently playing around with proxies, and its working just fine, everything is working perfectly according to plan, except for the small issue that my application doesnt seem to react to the textbox.
As you can see in the code below, im changing the proxy settings in the registry, and it works just fine when I have it like this.
registry.SetValue("ProxyServer", 201.48.34.200:3128);
But if I change it to this, and input the same IP and port in the textbox itself (proxyIP.Text) it doesnt change the proxy in the registry. any ideas what might be causing this? I tried debugging, but its not showing anything unusual.
registry.SetValue("ProxyServer", proxyIP.Text);
using Microsoft.Win32;
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.Runtime.InteropServices;
namespace ProxyTesting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", proxyIP.Text);
}
private void navigateButton_Click(object sender, EventArgs e)
{
this.webBrowser1.Navigate(navBox.Text);
}
}
}
You are doing the work on Form_Load(), which means it will only run the code when the form is loaded. Any changes made after the form is loaded, will not change the value.
You need to do the work on a different event, such as a button click (i.e. add a save button) or Form_Close
I am trying to extend the FlowLayoutPanel class in Visual Studio to give it some functionality that it does not currently have. I am having issues extending it in such a manner that I can then select it in the toolbox and add it to another form.
The three methods I have tried are to simply just create a new class that subclasses FlowLayoutPanel, also one that subclasses and then calls the : base() constructor. When the application is built the subclass shows up in the toolbox but when I go to add it to a form it says "Failed to load toolbox item, it will be removed from the toolbox."
I have also tried to add a new UserControl which I change its base class to FlowLayoutPanel instead of UserControl. After commenting out a line in the designer about AutoScaleMode I can then build the application. Once I try to place the panel from the toolbox I receive the same error above.
How can I properly subclass FlowLayoutPanel in Visual Studio so that I can use it from the toolbox?
EDIT:
Here is the entirety of my code, I added the OnPaint override based on amura.cxg's answer and I still receive the same error.
I have cleaned, rebuilt, closed, and reopened VisualStudio to no avail. I have also copied amura.cxg's code exactly and used it, again the same issue.
using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace UserInterface.SubclassedControls
{
class TestLayoutPanel : FlowLayoutPanel
{
public TestLayoutPanel() /* : base() */
{
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
}
}
EDIT2:
I have made my class public and again cleaned, rebuilt, closed, and opened Visual Studio and still have the same issue. I have also tried to add the control to a "brand new" form with the same results.
using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace UserInterface.SubclassedControls
{
public class TestLayoutPanel : FlowLayoutPanel
{
public TestLayoutPanel() /* : base() */
{
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
}
}
EDIT3:
So as a sanity check I started a new VS project and subclassed FlowLayoutPanel and added it to the default Form1. I then went back to my current project and tried to create a simple new form class to see if my current form was having issues, but I still received the same error when trying to add the subclass FLP to it. I then tried a UserControl that I made prior and have successfully added to other forms, which is now "broken" giving the same error message.
So what Project settings or Visual Studio settings could be preventing me from using these controls?
You should post your code so we can see exactly what you've tried as you may be missing something. Make sure you've built your project and try closing the re-opening the designer.
Below is an example of all you should (keyword) need to do to get it to work. I tested this in VS 2013 and the CustomFlowLayout control showed up in my ToolBox after a rebuild
public class CustomFlowLayout : FlowLayoutPanel
{
public CustomFlowLayout()
{
//Do things here!
}
public int MyCustomProperty { get; set; }
//Not needed to make anything work, added to show the code is working
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
StringFormat format = new StringFormat()
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
};
e.Graphics.DrawString("It works! Wooooo..!",
SystemFonts.DefaultFont,
SystemBrushes.ControlText,
new Rectangle(0, 0, this.Width, this.Height),
format);
}
}
Edit
Looks like you were missing the public keyword. When you don't specify the access modifier it uses the default, which is Internal. Below is a corrected version.
public class TestLayoutPanel : FlowLayoutPanel
{
public TestLayoutPanel() /* : base() */
{
}
}