|
You cannot use, display, or even access a Disposed control - Dispose is there to tell the system that the object is done with, finished, caput: once you call it on an object you will get an error (if you are lucky) when you try to use it again.
Once you call Dispose, you will need to recreate a new instance of the ToolTip if you want to show the same information again.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
It sort of depends on how you're calling "showTooltip".
You can return a reference to "ToolTip" from your "show" routine and dispose of that (at the right time).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hi,
you seem to be creating several tooltips, one for each control that needs a tip.
That is not what WinForms is suggesting.
The more natural way to use ToolTip class is by creating one ToolTip for a Form, then use that ToolTip for each and every Control on that Form that needs a tip. When you are done with the Form, that is the right moment to dispose of the ToolTip. Look at the example code in ToolTip.SetToolTip(Control, String) Method (System.Windows.Forms) | Microsoft Docs[^].
|
|
|
|
|
I have an interface and a type implementing it:
public interface IValueWithUnit {...}
[Serializable]
public struct DoubleValueWithUnit : IValueWithUnit { ... } At a different place, I have a string with the name of the type, and must find out if the type derives from above interface:
string name = "DoubleValueWithUnit";
Type t;
if (TypeHelper.TryFindType(name, out t))
{
m_IsValueWithUnit = t.IsSubclassOf(typeof(IValueWithUnit));
m_IsValueWithUnit = t.IsSubclassOf(typeof(DoubleValueWithUnit));
m_IsValueWithUnit = t.IsInstanceOfType(typeof(IValueWithUnit));
m_IsValueWithUnit = t.IsInstanceOfType(typeof(DoubleValueWithUnit));
m_IsValueWithUnit = t.IsAssignableFrom(typeof(IValueWithUnit));
m_IsValueWithUnit = t.IsAssignableFrom(typeof(DoubleValueWithUnit));
m_IsValueWithUnit = t.GetInterface(nameof(IValueWithUnit)) != null; The TryFindType function is from c# - Type.GetType("namespace.a.b.ClassName") returns null - Stack Overflow[^].
You see the many "false" comments? DoubleValueWithUnit is neither a subclass nor an instance of DoubleValueWithUnit or IValueWithUnit , and is also not assignable from IValueWithUnit , but at least it is assignable from DoubleValueWithUnit .
I am confused now: my brain threw a TypeOverflowException .
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
modified 15-Nov-19 6:47am.
|
|
|
|
|
What is the relation between the name variable, and the m_Setting.Type property?
Here's what I can understand from the results you get:
DoubleValueWithUnit is not a subclass of IValueWithUnit . An interface implementation is not considered as inheritance.DoubleValueWithUnit is not a subclass of DoubleValueWithUnit . It is the very same class.DoubleValueWithUnit is not an instance of IValueWithUnit . It is the type itself, not an instance of the type.DoubleValueWithUnit is not an instance of DoubleValueWithUnit . Same comment as above.DoubleValueWithUnit is not assignable from IValueWithUnit . It implements the interface, but that does not mean that every implementation of the interface should allow to cast to actual type. There could be some properties/methods of the class which are not part of the interface.DoubleValueWithUnit is assignable from DoubleValueWithUnit . This one seems logical.DoubleValueWithUnit implements IValueWithUnit . Thus IValueWithUnit is an element of the list of interfaces implemented by DoubleValueWithUnit .
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
phil.o wrote: What is the relation between the name variable, and the m_Setting.Type property? Oh, I forgot to replace that; question updated.
Thanks for your explanation!
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
|
Thanks. Good to know that.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
I'm have a listBox of categories that are added through user input via a label and a checkedListBox. I'm trying to get a way to add panels dynamically based on the number of categories in the listBox. I need to use hide/show for each panel since I want the information to switch between each panel.
I have this for adding the new panel dynamically based on each new category in the listBox:
private void button2_Click(object sender, EventArgs e)
{
for(int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
if (!listBox1.Items.Contains(checkedListBox1.CheckedItems[i]))
{
listBox1.Items.Add(checkedListBox1.CheckedItems[i]);
newPanel.Name = "panel " + (i+2).ToString();
newPanel.Location = new Point(405, 10);
newPanel.Parent = this;
newPanel.Size = new Size(341, 333);
newPanel.Visible = false;
newPanel.Controls.Add(nameLabel);
newPanel.Controls.Add(testButton);
listPanel.Add(newPanel);
newPanel.Show();
}
}
}
And then for whichever category is clicked on the sub-category it should open the respective panel using this code:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = this.listBox1.IndexFromPoint(e.Location);
if(index != System.Windows.Forms.ListBox.NoMatches)
{
if(index < listPanel.Count - 1)
{
listPanel[index++].Visible = true;
MessageBox.Show("" + listPanel.Count.ToString());
listPanel[--index].Show();
}
}
}
I just either get no respective panel opening or an out-of-range index at listPanel[--index].Show();
Thanks for your help
|
|
|
|
|
Use the debugger and find out what value index holds - I'd suspect that IndexFromPoint is returning ListBox.NoMatches , which is -1.
Since that by definition is less than ListBox.Count, that would explain your problem.
Then all you have to do is find out why - and that's probably down to the Location not being where you expect it to.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I will definitely try that, thanks.
|
|
|
|
|
jacko1123 wrote:
listPanel[index++].Visible = true;
listPanel[--index].Show(); I would suggest you remove those inline increment and decrement operators, since the net result is the same as:
listPanel[index].Visible = true;
listPanel[index].Show(); Since you never use the incremented value of index , those operators serve no purpose other than obfuscating your code.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Right, okay thanks, I'm not exactly very experienced yet so much in programming still learning so all the advice is appreciated thanks, just found a program that I could make that could help me in something in everyday life but it's been giving me a big headache so thought I'd come on a couple of forums for some help. Thanks
|
|
|
|
|
I have more comments to add:
1. assuming nameLabel and testButtons are controls, you are adding them to every Panel you create. However a Control can have only one Parent, so the last addition will win, your nameLabel and testButtons will only belong to and appear on the last Panel you created! If you want them to show on whatever Panel you make visible, you either have to create them for each Panel, or move them around from one Panel to the next that becomes visible. The easy alternative is to put them on your Form itself, not on a switching Panel.
2. your newPanel.Show(); will apply to every Panel you create; it also undoes your newPanel.Visible = false; You should decide how you make one and only one Panel visible: either have them all but one invisible (then don't use Show), or make them all visible and modify their z-order, see e.g. c# - How to set Z-order of a Control using WinForms - Stack Overflow[^] . (I would go for the former). Same applies to your click handler code.
1+2. you need a method that makes a panel visible, by
a) making the previous one (if any) invisible (Visible=false)
b) making the selected one visible, i.e. visible=true, and testButtons.Parent=this;
Your click handler would then call this method, as probably would your Load handler.
3. I would be inclined to create all panels at once (unless there are hundreds of them of course).
4. I see no need to specify a name for all these panels. If you later need to refer to a specific Panel, have a look at the Tag property, where you can store any number, string, Control reference, or whatever fits your needs.
|
|
|
|
|
i want to import database.mdf file in visual studio 2010 project, i done many solutions but its not work, the problem is access denied if copy from MSSQL13.MSSQLSERVER\MSSQL\DATA and paste into my project giving error Access is denied, i already done in SMS RIght Click on databese=>properties=>options=>changing Restrict Access in Single_User than ok, second to datch database, after go go to MSSQL13.MSSQLSERVER\MSSQL\DATA right click on mdf file and giving permission, i done yet, but still thats not work.
|
|
|
|
|
You shouldn't be trying to use a MsSQL database file directly - MsSQL is serving that file to multiple users and you do not have the user permissions on the folder that holds it for good reasons!
You need to work out exactly what you are trying to do: are you trying to use MsSQL in your app - in which case you shouldn't try to touch the file directly; are you trying to use a MDF file in yoru app for a single user - in which case you need to get MsSQL to "release" the file first; are you assuming that you just copy the DB files and MsSQL will "pick it up automatically" on a different computer - in which case you are in for a shock.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
i do copy paste and add through existing item both not work if i add existing item, its says path\database.mdf can not be opened because it is version 852, this server support version 655 and earlier.
|
|
|
|
|
So ... you are trying to persuade an earlier - lots earlier - version of software to open a datafile created by a later version and it is saying "I don't understand that data file, it's way too advanced for me"
Does any of that give you a clue as to why this is causing a problem?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
yes i use visual studio 2010, and Mssql server 2016.
|
|
|
|
|
And MsSql 2016 generates data in version 852 format, the server you are opening it on uses version 655 and earlier. So it looks at it, says "I don't know what the future holds, I'm not touching it, I'll damage something" and reports an error.
At a guess, you trying to open a 2016 file on SQL 2008 ... that's like trying to fit a 2019 Intel processor into a 2008 motherboard!
This is why you don't just ship an MDF file around: you create your DB on the "new" server using CREATE DATABASE, CREATE TABLE, and such like statements when you install your application.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have a c++ application code which I have attached below . I want to call the dll functions from a c# application . I am stuck with function pointers and explicit calling of function pointers using getProcAdress(). Please suggest a way
#include <cstdlib>
#include <iostream>
#include <windows.h>
//#include "ess_c.h"
/* Define functions prototype */
typedef unsigned long long(__stdcall *init)(int);
typedef int(__stdcall *serial)(unsigned long long, unsigned short *, unsigned short *);
typedef int(__stdcall *close)(unsigned long long);
typedef int(__stdcall *read2SWITCH)(unsigned long long handle, unsigned char two_switch, unsigned char * presence,
unsigned char * type, unsigned char * model, unsigned char * soft_vers, unsigned char * status, unsigned char * origin);
typedef int(__stdcall *readROTSWITCH)(unsigned long long handle, unsigned char rot_switch, unsigned char * presence,
unsigned char * type, unsigned char * model, unsigned char * soft_vers, unsigned char * err_code,
unsigned char * processing, unsigned char * position);
typedef int(__stdcall *setROTSWITCH)(unsigned long long handle, unsigned char rot_switch, unsigned char position, unsigned char direction);
int main(int argc, char *argv[])
{
/* Load DLL into memory */
HINSTANCE ProcDLL_ESS;
#ifdef _WIN64
ProcDLL_ESS = LoadLibrary(TEXT("ess_c_64.dll"));
#else
ProcDLL_ESS = LoadLibrary(TEXT("ess_c_32.dll"));
#endif
/* Declare pointers on dll functions */
init dll_init;
serial dll_serial;
close dll_close;
read2SWITCH dll_read2SWITCH;
readROTSWITCH dll_readROTSWITCH;
setROTSWITCH dll_setROTSWITCH;
/* Link dll pointers with functions prototype */
dll_init = (init)GetProcAddress(ProcDLL_ESS, "ess_initialization");
dll_serial = (serial)GetProcAddress(ProcDLL_ESS, "ess_get_serial");
dll_close = (close)GetProcAddress(ProcDLL_ESS, "ess_close");
dll_read2SWITCH = (read2SWITCH)GetProcAddress(ProcDLL_ESS, "ess_get_data_two_switch");
dll_readROTSWITCH = (readROTSWITCH)GetProcAddress(ProcDLL_ESS, "ess_get_data_rot_switch");
dll_setROTSWITCH = (setROTSWITCH)GetProcAddress(ProcDLL_ESS, "ess_set_rot_switch");
/* Define variables used for ESS */
unsigned long long essHandle = 0;
unsigned short Serial = 0;
unsigned short Version = 0;
unsigned char position = 0;
unsigned char two_switch = 0; // 2-SWITCH position (0 is the 1st on the SWITCHBOARD)
unsigned char rot_switch = 0; // ROT-SWITCH position (0 is the 1st, position 'A')
/* SWITCH variables definition used by the shared library */
unsigned char Presence;
unsigned char Type;
unsigned char Model;
unsigned char Software_Version;
unsigned char Status;
unsigned char Origin;
unsigned char Err_code;
unsigned char Processing;
unsigned char Position;
unsigned int localCount = 0;
if (ProcDLL_ESS != NULL) { // If dll loaded
std::cout << "ESS dll loaded" << std::endl;
/* Initialize device */
if (dll_init != NULL) { // Check if function was properly linked to the dll file
/* Initialize the first SWITCHBOARD in Windows enumeration list */
essHandle = dll_init(0);
std::cout << "ESS session initialized" << std::endl;
}
/* Read device serial number */
if (dll_serial != NULL) {
/*Get the serial number of the SWITCHBOARD*/
dll_serial(essHandle, &Serial, &Version);
std::cout << "SWITCH BOARD SN: " << Serial << std::endl;
}
/* Get status of the 2-SWITCH (N°1 on the board) */
if (dll_read2SWITCH != NULL){
dll_read2SWITCH(essHandle, two_switch, &Presence, &Type, &Model, &Software_Version, &Status, &Origin);
if ((Presence == 1) && (Type == 1)){ // If 2-SWITCH detected
std::cout << std::endl << "2-SWITCH detected." << std::endl;
std::cout << "Model (1 if 2-SWITCH): " << int(Model) << std::endl; // Display model
std::cout << "Software version: " << int(Software_Version) << std::endl; // Display software version
if (Status == 1){ // Display 2-SWITCH status
std::cout << "Switch is ON." << std::endl;
}
else{ std::cout << "Switch is OFF." << std::endl; }
}
else{ std::cout << "No 2-SWITCH found on 1st position." << std::endl; }
}
/* Get status of the ROT-SWITCH (N°A on the board) */
if (dll_readROTSWITCH != NULL){
dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);
if ((Presence == 1) && (Type == 0)){ // If SWITCH detected
std::cout << std::endl << "ROT-SWITCH detected." << std::endl;
std::cout << "Model (1: M-SWITCH; 3: L-SWITCH): " << int(Model) << std::endl; // Display model
std::cout << "Software version: " << int(Software_Version) << std::endl; // Display software version
std::cout << "Switch position: " << int(Position) << std::endl; // Display ROT-SWITCH position
/* Change ROT-SWITCH position */
if (Position == 0){ // SWITCH changes from position 1 to 0 or from 0 to 1
dll_setROTSWITCH(essHandle, rot_switch, 1, 0);
}
else{
dll_setROTSWITCH(essHandle, rot_switch, 0, 0);
}
std::cout << "ROT-SWITCH position changed!" << std::endl;
/* Wait for processing flag to be active */
do {
dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);
Sleep(100);
} while (Processing != 1);
Sleep(100);
/* Wait for processing flag to be down */
do{
dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);
Sleep(100);
} while (Processing == 1);
Sleep(100);
// Read new position after change
dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);
std::cout << "New Switch position: " << int(Position) << std::endl; // Display ROT-SWITCH position
}
else{ std::cout << "No ROT-SWITCH found on position 'A'." << std::endl; }
}
/* Close ESS session */
if (dll_close != NULL) {
dll_close(essHandle);
std::cout << std::endl << "ESS session closed" << std::endl;
}
}
/* Release the DLL */
FreeLibrary(ProcDLL_ESS);
std::cout << "ESS dll unloaded" << std::endl;
/* Exit application */
system("PAUSE");
return EXIT_SUCCESS;
}
modified 12-Nov-19 1:57am.
|
|
|
|
|
|
I referred the platform invoke concept.
The problem is with the function pointers declaration of the above mentioned code in c# and calling them in main function
|
|
|
|
|
Using P/Invoke is the only simple method of accessing a C-style library from C#. You need to check the documentation for the various functions to see how to implement the calls.
|
|
|
|
|
Hiya all, I'm trying to get my code to allow me to login with details from a .csv file. The code is not splitting correctly and therefore says the user and pass is incorrect. I'm wanting it to open a second form.
Any suggestions? Here's 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;
using System.IO;
namespace --------
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Label1_Click(object sender, EventArgs e)
{
}
private void BtnLogin_Click(object sender, EventArgs e)
{
string Username = txtUsername.Text;
string Password = txtPassword.Text;
bool Found = false;
string FilePath = "Login.csv";
StreamReader fileReader = new StreamReader(FilePath);
String[] details = File.ReadAllLines(FilePath);
String[] splitted;
foreach (string line in details)
{
splitted = line.Split(',');
if (details[0] == Username && details[1] == Password)
{
Found = true;
}
}
if (Found == true)
{
Form2 Form2 = new Form2();
Form2.Show();
this.Hide();
}
else
{
AutoClosingMessageBox.Show("Incorrect Login Details Entered", "Error:", 5000);
}
}
}
}
public class AutoClosingMessageBox
{
System.Threading.Timer _timeoutTimer;
string _caption;
AutoClosingMessageBox(string text, string caption, int timeout)
{
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
using (_timeoutTimer)
MessageBox.Show(text, caption);
}
public static void Show(string text, string caption, int timeout)
{
new AutoClosingMessageBox(text, caption, timeout);
}
void OnTimerElapsed(object state)
{
IntPtr mbWnd = FindWindow("#32770", _caption);
if (mbWnd != IntPtr.Zero)
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
_timeoutTimer.Dispose();
}
const int WM_CLOSE = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
|
|
|
|
|