|
Upon rereading your post, a light went on in my head. You might be assuming that *I* am creating the console in which my code is running. The console is a standard DOS shell (cmd.exe) that is run by the user. *My Application*, while gui-based, is being run from that command shell as a commandline. I'm trying to get my app to print text to the shell from which it was called. In C, the closest thing would be a printf().
Hope that helps....
|
|
|
|
|
If you don't create the Console window, or at least grab a handle to an existing console window, you can't write to it.
smarr wrote: I'm trying to get my app to print text to the shell from which it was called. In C, the closest thing would be a printf().
Since you created a Windows Forms app, there is no such thing as a console that is attached to it automatically. Your app has to create one, or find an existing one. This was easy in C because your C apps were console-based, non-GUI apps and ran in the context of the DOS-emulator running in that console window. The consoles Standard In/Out/Error streams are at the disposable of the console app that is launched. This concept doesn't apply to Windows Forms apps since it has no use for those streams.
You can create your own Console window using:
Declare Auto Function AllocConsole Lib "kernel32" () As IntPtr
.
.
.
AllocConsole()
Console.WriteLine("This is a test...")
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hate to keep repeating it, but I cannot create the console: It needs to be an existing shell in which the user executes the gui app, then the string is returned to the (same) shell. It's like the event handlers injected into a gui app disable the "console-ness" of it.
|
|
|
|
|
OK. Well, the console you launch your app from is not attached in any way shape or form to a Windows Forms app. In other words, the parent process of the Windows Forms app is NOT the console process that launched it. There is no way for a Windows Forms app to know which console window launched it. It's exactly the same as if you launched it by double clicking the icon or typed its command line into Start/Run.
So, the only way to get the console window attached to your app upon launch is if you write your app as a Console Application, then create and launch your Form using Application.Run() . You'll have to create public properties in your form so you can get any return values out of it. Something like this:
Imports System
Imports System.Windows.Forms
Module Module1
Sub Main()
Dim myForm As New Form1
Console.WriteLine("Application startup...")
' Application.Run is a blocking call...
System.Windows.Forms.Application.Run(myForm)
' so, this statement won't execute until the form is closed.
Console.WriteLine(myForm.ReturnValue)
End Sub
End Module
Public Class Form1
Inherits Form
Shared Sub Main()
End Sub
Private _returnValue As String
Public ReadOnly Property ReturnValue() As String
Get
Return _returnValue
End Get
End Property
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Console.WriteLine("You can write from the console from inside your form.")
_returnValue = "This is the return value stored in the form..."
End Sub
End Class
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I now have an alternative.....Dave, this may be similiar to what you are suggesting, although it does seem to work despite some of the issues you raised. Someone on another list suggested changing the apptype to Console App *after* the app has already been created.
I didn't want to change the app drastically and get involved with the whole main/app.run stuff. Plus, I have several utility programs that can benefit from this. Apparently by creating the app normally (as a windows application) and using console.writeline to write to the IDE console for debugging can be the starting point. Afterwards, the app type can be changed (in the IDE Properties for the project) to Console Application. At this point, all the existing infrastructure still works, but console.writeline will now output to something other than the IDE console window. If the app is run from the IDE, it will automatically create a shell window and output to that. However, if the app is run from an existing shell, output will be sent to that window (as expected since this is the shell window that spawned the app) and the app runs as a gui-based application.
The best of both worlds.
The only bad side is that if the app is run from an icon, it will open a shell for it's output. This is acceptable for me since the app will always be run from an existing shell app.
Thanks for the help Dave!
|
|
|
|
|
You can do that. All you're doing is changing the startup code environment. How you're launching the form code (automatically in your case) is very similar to what I put up in the last post. The results look exactly the same, right down to the console window popping up if you double-click the icon. Now you know how to do it yourself instead of relying on a procedure that might not work in the next version of VS.NET.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Sir,
I want to apply password to the file/folder with the help of my application .Please suggest some tutorials for that.Last time i was told to get the knowledge up to black belt, device drivers, file extentions.But i was not suggested where to study these things online.Please suggest any online link .I want to start from the scratch.
Thanks
|
|
|
|
|
This is really a black art, and resources on the topic are few. Try typing "windows driver development[^]" into Google.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
how to use vb.net changing the registry of HKEY_CURRENT_USER "Control panel\colors" for each color?
Thank you.
Beginner
|
|
|
|
|
All of the values in that key are merely strings that represent RGB values to build a color. The first number in each string is the Red component, in the range 0 to 255. The second number is Green and the thrid number is Blue. ALl you have to do is build a string with those numbers and update the value in the registry.
You can learn more about using the Registry and RegistryKey classes here[^].
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Thank you very much.
if i use this function to run that always hava an error message:
RegCreateKeyEx(HKEY_CURRENT_USER, "Control Panel\Colors", 0, vbNullString, _
REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, vbNullString, hKey, 0)
error message:
Additional information: Error: PInvoke item (field,method) must be Static.
how to fix the problems? or use the different the funcutions?
Thanks.
-- modified at 14:22 Monday 18th September, 2006
Beginner
|
|
|
|
|
Why are you P/Invoking Win32 functions when the Registry and RegistryKey classes already do all this for you?
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Thank you very much...
I use this two lines coding that works very well.
regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Colors", True)
regKey.SetValue(StrSysColor(i), colorStr)
Beginner
|
|
|
|
|
Not sure if this is best forum but...
I am trying to convert somebody elses old C component into a VB.NET version and in the process add some more functionality. Silly me... I don't know any C at all but having an educated guess. This following bit has me stumped. Can anybody help with the conversion.
<br />
protected void onMenuItemClick(Object sender, EventArgs e) {<br />
MenuItem item = (MenuItem)sender;<br />
if (MRUItemClicked != null) {<br />
string fileName = (string)_itemMap[item];<br />
MRUItemClicked(this, new MRUItemClickedEventArgs(fileName));<br />
}<br />
}<br />
Cheers
Tim
|
|
|
|
|
This is obviously not C code, do you mean C# ? This code is C#.
This is an event handler for a menu item click. It converts the senser to a MenuItem, which is what it is. MRUItemClicked is obviously a member variable, null is called Nothing from memory in VB land.
_itemMap is obviously a dictionary/hash table member variable.
MRUItemClicked appears to be a delegate.
Which of those things do you not understand ? Do you know what a delegate is ? I assume you have _itemMap set up from converting the rest of the code ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Probably this:
Protected Sub onMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)<br />
Dim item As MenuItem = CType(sender,MenuItem)<br />
If (Not (MRUItemClicked) Is Nothing) Then<br />
Dim fileName As String = CType(_itemMap(item),String)<br />
MRUItemClicked(Me, New MRUItemClickedEventArgs(fileName))<br />
End If<br />
End Sub
Best Regards,
Apurva Kaushal
|
|
|
|
|
Hi,
dr1='8/16/06'
dr2='11:45'
finalstring = dr1 & " " & dr2
How to convert this string to date? Tried::
DateTime.Parse(finalDateString)
Resulting in error - String was not recognized as a valid DateTime.
Thanks.
-- modified at 10:03 Monday 18th September, 2006
|
|
|
|
|
What is the date 8/16/20? Did you try 8/16/2006?
|
|
|
|
|
Sorry - that was a typo - corrected it!
But bascially want to convert STRING to DATE!
Thanks.
|
|
|
|
|
It doesn't recognise the string as a date because it expects the format of the current culture. Specify the culture you want to use in the call to Parse.
---
b { font-weight: normal; }
|
|
|
|
|
|
Hi
I design a component and I want to add prperty that suport Collection Editor by my own properties.
How should I implemet this property?
Thanks
Qofrani
|
|
|
|
|
Hi
I solved my problem by myself but I have another one.
When I set my control properties every thing is ok but after I run my project I lose my colllection properties value.
How can I solve this problem please help me.
Here is my code :
<br />
Imports System<br />
Imports System.ComponentModel<br />
Imports System.Drawing<br />
Imports System.Drawing.Design<br />
Imports System.ComponentModel.TypeConverter<br />
<br />
'My properties class<br />
Public Class ToolBoxItem<br />
<br />
#Region "Properies"<br />
<br />
Private m_IName As String<br />
<br />
Private m_Value As Integer<br />
<br />
Public Property IName() As String<br />
Get<br />
<br />
Return m_IName<br />
<br />
End Get<br />
Set(ByVal Value As String)<br />
<br />
m_IName = Value<br />
<br />
End Set<br />
End Property<br />
<br />
Public Property Value() As Integer<br />
Get<br />
<br />
Return m_Value<br />
<br />
End Get<br />
Set(ByVal Value As Integer)<br />
<br />
m_Value = Value<br />
<br />
End Set<br />
End Property<br />
<br />
#End Region<br />
<br />
End Class<br />
<br />
<br />
'My collection class<br />
Public Class ToolBoxItemCollection<br />
<br />
Inherits CollectionBase<br />
<br />
Public Sub Add(ByVal MyItem As ToolBoxItem)<br />
<br />
list.Add(MyItem)<br />
<br />
End Sub<br />
<br />
Public Sub Remove(ByVal index As Integer)<br />
<br />
If index > Count - 1 Or index < 0 Then<br />
<br />
System.Windows.Forms.MessageBox.Show("Index not valid!")<br />
<br />
Else<br />
<br />
List.RemoveAt(index)<br />
<br />
End If<br />
<br />
End Sub<br />
<br />
<br />
Public ReadOnly Property Item(ByVal index As Integer) As ToolBoxItem<br />
Get<br />
<br />
Return CType(List.Item(index), ToolBoxItem)<br />
<br />
End Get<br />
<br />
End Property<br />
<br />
End Class<br />
<br />
'My control class<br />
Public Class ToolBox<br />
<br />
Inherits GroupBox<br />
<br />
Private m_Items As ToolBoxItemCollection<br />
<br />
Private m_Count, m_ItemWidth, m_ItemHeight As Integer<br />
<br />
<br />
Public Property Items() As ToolBoxItemCollection<br />
Get<br />
<br />
Return m_Items<br />
<br />
End Get<br />
Set(ByVal Value As ToolBoxItemCollection)<br />
<br />
m_Items = Value<br />
<br />
End Set<br />
End Property<br />
<br />
<br />
Public Property Items(ByVal Index As Integer) As ToolBoxItem<br />
Get<br />
Return m_Items.Item(Index)<br />
<br />
End Get<br />
Set(ByVal Value As ToolBoxItem)<br />
<br />
m_Items.Add(Value)<br />
<br />
End Set<br />
End Property<br />
<br />
Public Sub New()<br />
<br />
MyBase.New()<br />
<br />
Me.Dock = DockStyle.Right<br />
<br />
Me.BackColor = SystemColors.ControlDarkDark<br />
<br />
Me.Text = ""<br />
<br />
Me.Width = 100<br />
<br />
If m_Items Is Nothing Then<br />
<br />
m_Items = New ToolBoxItemCollection<br />
<br />
End If<br />
<br />
End Sub<br />
<br />
<br />
End Class<br />
<br />
Thanks
Qofrani
|
|
|
|
|
I've got what I'm sure is a trivial question. I want to draw a graph in Visual Basic on an object on a form.
I would like to know what is the best object to use. I tried a panel as my first choice but in the properties I couldn't find something which would override the OnDraw and let me draw my own things.
There must be a standard way to do this, but I don't use VB much so I'm not familiar with it.
Thanks,
Ilan
|
|
|
|
|
A Panel can be used, or a PictureBox. Either is good. You override the Paint method of the control you use. For example:
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
Dim g As Graphics = e.Graphics
' do your painting for the control
End Sub
Another way to do it, would be to draw to a Bitmap image and just set the PictureBox Image or Panel BackgroundImage property to the Bitmap you've drawn on.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|