Open a C# form (.dll) with VBA (Access 2010) - c#

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.

Related

In C#, how can I print something to the console after clicking a button from Windows Form?

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...

How to show message box without console or form in background?

I am new to programming (c#). I have seen in VBScript we can show the message box (standalone). I would like to do the same in C#, just for learning, but the code when executed shows a Console or Form in the background along with a messagebox. Is it possible to only show the messagebox when the program is executed?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
MessageBox.Show("My message");
}
}
}
For Console projects, you can hide the console as follows:
Right click the project in the Solution Explorer
Select Properties
Go the the Application tab in the project properties.
Change the Output Type combo box from "Console Application" to "Windows Application".

MessageBox.Show() does not work outside debug

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.

Why isnt my proxy changing when I change it in the textbox? C# .NET

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

File/Folder browser dialog appears twice

I am making a program with C# using Visual Studio 2013 Express that launches an application (in portable mode), and makes a virtual drive out of a folder (using the subst command or a C# equivalent)
At the moment it's meant for the popular media center XBMC, but it can eventually also be used for other programs
On the first screen the user can select the path to the XBMC.exe file and the directory containing the media collection.
However, while testing, both dialogs appear twice: once after clicking on the "Browse" button, and once again after clicking on either the "OK" or "Cancel" button (in the dialog). So I have to click "OK" twice every time before the selected path/file is inserted into the textbox.
I have no idea why that's happening, and I'm a bit of a noob when it comes to programming myself...
XBMCPortableLauncher.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace XBMCAttempt1
{
public partial class XBMCPortableLauncher : Form
{
public XBMCPortableLauncher()
{
InitializeComponent();
}
private void btnSelectMediaFolder_Click(object sender, EventArgs e)
{
// Opens "Browse for media folder" dialog
fBDBrowseMediaFolder.ShowDialog();
if (fBDBrowseMediaFolder.ShowDialog() == DialogResult.OK)
{
// Deletes any existing text in the textbox
tbMediaFolder.Clear();
// Inserts the selected path into the text box if user selects "OK"
tbMediaFolder.Text = fBDBrowseMediaFolder.SelectedPath;
}
}
private void btnSelectXBMCexe_Click(object sender, EventArgs e)
{
// Opens the "Select XBMC.exe" dialog
oFDBrowseXBMCexe.ShowDialog();
if (oFDBrowseXBMCexe.ShowDialog() == DialogResult.OK)
{
// Deletes any existing text in the textbox
tbXBMCexe.Clear();
// Inserts the selected file into the text box if user selects "OK"
tbXBMCexe.Text = oFDBrowseXBMCexe.FileName;
}
}
}
}
Can anyone tell me what I'm doing wrong, and what I have to do to fix the problem?
(if you need more information, just ask)
But you're calling ShowDIalog twice. Can't be that easy:
fBDBrowseMediaFolder.ShowDialog();
if (fBDBrowseMediaFolder.ShowDialog() == DialogResult.OK)
Remove the first call.
ShowDialog is blocking and in fact it does show your dialog and returns a DialogResult. So, keep only the second call where you actually use the result. And do that for both dialogs (fBDBrowseMediaFolder and oFDBrowseXBMCexe).

Categories

Resources