|
Heyas all,
I have project that has a mainform and on it is a read-only textbox control. I have another source file that hold a class I made myself. I'm trying to access the textbox control that is on the mainform from my class file, and I keep getting erros that it's inacceble. The code wizard automatically creates the control as private. I have a method in my class that simply does this:
void vWriteToTextbox(void)<br />
{<br />
}<br />
and like I said it's not working. How can I access the control from my file (which of course is in the same project)?
Thanks,
John
|
|
|
|
|
John L. DeVito wrote: How can I access the control from my file
Why would you want to do this?
You can either make the control varialbe "textbox1" public , or create a get-function (or property) to return the textbox variable like this:
public TextBox box { get { return textbox1; } }
You also need to have a reference to your main form, you can't just do MainForm.something unless something is declared static
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Okay, the easy way to fix this is select the TextBox in the form designer and change the value of the Design >> Modifiers property to public .
While that's the easy way, it breaks the encapsulation principle of OOD. May I suggest creating a property in your main form that exposes the value of the TextBox ?
For example, in your main form's class declaration, provide the following declaration.
public string MyInterestingTextBoxValue
{
set
{
this.textBox1.Text = value;
}
} Then, you can change your method above to
private void vWriteToTextbox()
{
MainForm.MyInterestingTextBoxValue = "Hello";
}
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Thanks Curtis I like the idea of wrapping it in a property. I know I could have changed the access identifier but I really REALLY didn't want to do that. Appreciate the time, works great. Thanks again.
Thanks,
John
|
|
|
|
|
John, my pleasure. Hope all turns out well. Happy coding!
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Heyas Curtis,
I've actually come across a bit of a problem, VS is saying that in order for me to set this property value I must have an instantiated object. I don't understand that. the MainForm is going to be instantied when it runs. Here's my code:
In MainForms Class description I have...
<br />
internal String txtStatusValue{ set{ this.txtStatus.Text = value;}}<br />
and then in my class file I have..
<br />
internal void vMyFunction()<br />
{<br />
MainForm.txtStatusValue = "Blah Blah Blah";<br />
}<br />
I don't get it, could I trouble you for but more assistance?
Thanks,
John
|
|
|
|
|
sounds like your calling the txtStatusValue before the mainform is instantiated or before the variable you're using to call the mainform propery is initialized.
do something like this:
public class MainForm: Form
{
public static MainForm myMainForm = null;
public MainForm()
{
myMainForm = this;
}
internal public string txtStatusValue { set { txtStatus.Text = value; } }
}
public MyClass
{
public void someFunc()
{
MainForm.myMainForm.txtStatusValue = "foo";
}
}
hope this helps...
|
|
|
|
|
If I have a function in dll generated by managed C++, how do I pass arrays in C# so that I can get my output array back?
Any help is appreciated!
Devin
|
|
|
|
|
What do you mean? Your question keeps many details unsaid. If you have a C++.NET DLL with a function like
void YourClass::MyFunc( double [] dArray ){...}
Why can't you call that function from C# directly like
...<br />
double [] dArray = new double [ 10 ];<br />
YourClass yc = new YourClass();<br />
yc.MyFunc( dArray );<br />
...
If your question is more complex than that, you will get better responses if you elaborate...
Koushik Biswas
|
|
|
|
|
Thanks Koushik!
It is then pretty natural.
Devin
|
|
|
|
|
Hi all,
I have developed a dll in Win32. That dll finds the updated regions
of the desktop and then post these regions data to a C# application
using PostThreadMessage(). But problem is here i m not so much use to
with C# so i havent any idea how can i get it from the Application
message queue of my C# application.
Thanx
|
|
|
|
|
In C#, you typically don't deal with the Win32 message queue. The .NET framework is designed to abstract away the underlying operating system & hardware. So, if you're listening for when a user moves his mouse over your System.Windows.Forms.Form, just setup an event handler for the MouseMove event.
Form myForm = new Form();
myForm.MouseMove += new MouseEventHandler(MyMouseMoveFunction);
...
void MyMouseMoveFunction(object sender, MouseEventArgs arguments)
{
}
That said, if you want to get low level, past the abstractions, you can create a class that inherits from System.Windows.Forms.Form, then override the WndProc virtual method; that method is called every time your form receives a windows message.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
|
Dear u r quite right that i can track mouse movements. But here is the
different case my dll is a hooking dll and is responsible for getting
screen display updates. So here i m restricted to use that unmanaged
dll.
I have passed dll the managed thread id so that dll can post messages
of screen updates to my C# application.
Is it right to pass a managed thread id in PostThreadMessage()?
Thanx for ur response.
|
|
|
|
|
Does the WebBrowser control introduced in .NET 2.0 work fine with console applications? I'm facing problems doing something like
string x = "<HTML>abc</HTML>";
browser.DocumentText = x;
Unfortunately, x never gets set, browser.DocumentText always remains as <HTML> </HTML>
On a hunch, I tried doing an Application.DoEvents right after setting the DocumentText property and it worked. Does that mean there is no *easy* way to use the WebBrowser just for inspecting and manipulating the DOM tree?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
We've had trouble with WebBrowser controls without a parent; you may end up having to create an invisible form and add the browser to that form before you can do things with the document.
|
|
|
|
|
That did the trick, thank you. I dearly wish .NET 2.0 provided a class to do nothing else but parse HTML.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
S. Senthil Kumar wrote: I dearly wish .NET 2.0 provided a class to do nothing else but parse HTML.
I agree.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
Yeah, the WebBrowser control won't work properly without a parent form, which you don't have in a Console app. You'll have to create a form to put the control on, but just never show the form.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
|
Hi, I have a problem with the functions SetCommTimeouts and GetCommTimeouts of the Win32 API.
First of all, I want to make a synchronous read on a USB device with a timeout of 250 ms, that is, the ReadFiule function returns after 250 ms if no data is received.
This is the COMMTIMEOUTS Structure
<code>
[StructLayout(LayoutKind.Sequential)]
public struct COMMTIMEOUTS
{
public int ReadIntervalTimeout;
public int ReadTotalTimeoutMultiplier;
public int ReadTotalTimeoutConstant;
public int WriteTotalTimeoutMultiplier;
public int WriteTotalTimeoutConstant;
}
</code>
These are the 2 functions: GetCommTimeouts and SetCommTimeouts
<code>
[DllImport("kernel32.dll")]
public static extern bool GetCommTimeouts(IntPtr hFile, out COMMTIMEOUTS lpCommTimeouts);
[DllImport("kernel32.dll")]
public static extern bool SetCommTimeouts(IntPtr hFile, ref COMMTIMEOUTS lpCommTimeouts);
</code>
And finally, here is how I try to adjust the timeouts
<code>
FileIO.COMMTIMEOUTS timeouts;
// Create handle
hidHandle = unchecked( FileIO.CreateFile(path,
(uint)(FileIO.GENERIC_READ | FileIO.GENERIC_WRITE),
FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE,
ref security,
FileIO.OPEN_EXISTING,
0,
IntPtr.Zero));
if(hidHandle == FileIO.INVALID_HANDLE_VALUE)
{
success = false;
}
else
{
// Read timeouts
if(FileIO.GetCommTimeouts(hidHandle, out timeouts))
{
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
// Set them
if(FileIO.SetCommTimeouts(hidHandle, ref timeouts))
{
// I READ THEM AGAIN AND THEY HAVE NOT CHANGED!!!
FileIO.GetCommTimeouts(hidHandle, out timeouts);
success = true;
}
}
else
{
int lastError = FileIO.GetLastError();
}
}
</code>
I create a handle successfully, I call GetCommTimeouts, it returns true, I modify the timeouts and apply them by calling SetCommTimeouts, it returns true. But then, when I call GetCommTimeouts again, the timeouts are just like before! They haven't changed!
Where's my mistake?
Thank you!
|
|
|
|
|
is it possible to add an app.config in a class library project and use that to retrieve the appsettings?
from within a class in the same project?
If so then I can not see why mine does not retrieve the appsettings?
Thanks
|
|
|
|
|
.config files work per AppDomain. Class libraries do not get an AppDomain, so they will use the .config files of the loading applications.
However, Mike Woodring has an interesting solution with per-Assembly configuration files. You may want to take a look at his Per-Assembly Configuration Files.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
so do you mean that I can not add an app.config in my class library project ?
|
|
|
|
|
Correct. If you have the class library goo.dll, then it will not use the goo.dll.config file for its appSettings.
I haven't used .NET 2.0 and do not know if they have expanded support for this to Class Libraries. Maybe someone else on the board can answer that.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|