|
case "2":
case "3":
Dostuff();
break;
Note, you cannot do this:
case "2":
DoCase2Stuff();
case "3":
Docase2and3stuff();
break;
Because C# switch statements are dumbed down on the assumption that the user is stupid.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: Because C# switch statements are dumbed down on the assumption that the user is stupid.
Which is true, usually.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Christian Graus wrote: assumption that the user is stupid
No, that some users are much smarter than others, creating too wide a gap in developer abilities; and as they couldn't make the stupid ones smarter, their only recourse was to handicap the smart ones, thereby leveling the playing field.
|
|
|
|
|
I would like to display read-only information on a tab page by simply drawing it using GDI (TextRenderer.DrawText). The problem I'm having is that the text is not persisted by the tabPage surface itself. If I select a different tab then switch back the text is lost with the redwaw. If I use Label controls the control handles persisting the text. What do I need to do to be able to draw on a tab page and have the text stay there until I'm ready to change it? Labels aren't going to cut it.
-Roger
|
|
|
|
|
Well, if it doesn't persist, sounds like you're using CreateGraphics instead of a paint event, just like the fellow below.
You really need to post code if you want us to help you.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
The previous reply is correct, you will need to use paint event of TabPage.
Drawing a line of text should be as simple as using following code in paint event-
Graphics g = e.Graphics ;
g.DrawString("Some text",MyFontObj,Brushes.Black,10,10);
I hope this helps.
-Dave.
Dave Traister,
ComponentOne LLC.
www.componentone.com
|
|
|
|
|
I've been programming C# for a couple years now in the academic setting, but I've been curious about the recommended place to put Main() in a Windows Forms application. I know of two ways to do it. One is to simply place Main() in the same class as the primary form, as below:
using System;
using System.Windows.Forms;
namespace TestCode
{
public class MyForm : Form
{
public static void Main(String[] args)
{
Application.Run(new MyForm());
}
}
}
The other method is to create a separate class and put Main() in there, as below:
using System;
using System.Windows.Forms;
namespace TestCode
{
public class MyForm : Form
{
}
public class MyApp
{
public static void Main(String[] args)
{
Application.Run(new MyForm());
}
}
}
I've found that most people use the first method, but the second method seems better to me because it keeps the application logic and whatnot out of the GUI code. Are either of these two the "better" option? Or is it merely a matter of preference?
Thanks.
|
|
|
|
|
I've never given it a second thought.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Usually just east of New Hampshire.
|
|
|
|
|
Hi! I had created a picture box and a button. When I click on the button one circle and one rectangle will be drawn on the picture box. I would like to implement drag and drop effect on the picture box so that both circle and rectangle can be move freely in picture box area. Does anyone know how to implement it? Because I don't have any idea where to start. Thanks in advance for the help.
Here is my code on drawing that 2 shapes in picture box:-
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace test_drag
{
public partial class Window1
{
private Graphics g;
private System.Drawing.Pen pen1;
Bitmap bitmap;
public Window1()
{
this.InitializeComponent();
bitmap = new Bitmap(pictureBox.Width,pictureBox.Height);
g = Graphics.FromImage(bitmap);
// Insert code required on object creation below this point.
}
private void create_Click(object sender, RoutedEventArgs e)
{
pen1 = new System.Drawing.Pen(System.Drawing.Color.Blue, 1);
g.DrawRectangle(pen1, 10, 10, 50, 50);
g.DrawEllipse(pen1, 10, 80, 30, 30);
pen1.Dispose();
using (Graphics h = pictureBox.CreateGraphics())
{
h.DrawImageUnscaled(bitmap, 0, 0, pictureBox.Width, pictureBox.Height);
h.Dispose();
}
}
}
}
|
|
|
|
|
yuenli wrote: using (Graphics h = pictureBox.CreateGraphics())
This is a disaster, never use it. Open your app, then open calculator. Draw some stuff in your app. Now drag calculator across your app and out again. See how your drawings have disappeared ? That's because you used creategraphics.
The paint event is where you need to do all drawing. You force a paint event by calling Invalidate().
What do you mean by drag and drop ? Whatever you mean, you need to keep your shapes in an array, and iterate over that array to draw them in your paint event. Then you can make one shape your selected shape, and change it's position while forcing a paint, so the shape moves when it's redrawn.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Christian,
Thanks for the reply. Actually I am a newbie. May I know what do you mean by keep shapes in array and draw them in paint event. Do you mean that each time I change a shape location I have to recall the paint event? And How to I make selected shape by allowing user to select it? I wish to do something like Paint application where you can select the drawn shape and move it to new location inside the drawing area. Thanks.
|
|
|
|
|
It sounds to me like you need to start with something more basic.
Your shapes should be represented by class instances. Ideally they'd know how to draw themselves, just taking a graphics object. Yes, calling Invalidate to force a paint event is THE way to make a windows app redraw itself. Then you have your list of shapes and you iterate over them, getting them all to draw themselves. You'd also store the co-ordinates of each one and you'd iterate over them all when the mouse goes down, to work out which one is under the mouse, and mark it as selected.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
That means I only need to play with On mouse up and on mouse down event and everytime call the paint function to reload whole thing. Am I right?
|
|
|
|
|
No, if you're moving the object, you need to repaint in onmousemove, and also move your object by however far the mouse has moved first.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Does that mean in on mouse move function i need to get the x y position of selected shape and repaint the whole drawing? This step is also apply in on mouse up right? Thanks.
|
|
|
|
|
The drawing all happens in OnPaint. What you need to do in the mouse move, is move your shape if the mouse is down, so that it moves the amount that the mouse has moved. Then call invalidate() to force a repaint, which draws the shape in it's new position.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi!
I try to over write the onPaint event handler but it seem that got error. This is because I embeded picture box to windows host form in xaml files. Can anyone help me to solve this problem? Really thanks for the help.
XAML code:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns ="http://schemas.microsoft.com/expression/blend/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="test_drag.Window1"
x:Name="Window"
Title="Window1"
Width="516"
Height="694"
>
<Grid x:Name="LayoutRoot">
<WindowsFormsHost x:Name="host1" Background="#FFE1E0E0" AllowDrop="True" Margin="0,0,92,0" VerticalAlignment="Top" Height="312" >
<wf:PictureBox x:Name="pictureBox" Width="300" Height="300" Paint="pictureBox_Paint" />
</WindowsFormsHost>
</Grid>
</Window>
C# coding:
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Documents;
using System.Windows.Markup;
namespace test_drag
{
public partial class Window1
{
Font font;
public Window1()
{
this.InitializeComponent();
font = new Font("Arial", 10);
// Insert code required on object creation below this point.
}
private void pictureBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawString("This text is render", font, System.Drawing.Brushes.Red, 0, 0);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString("this overrride", font, System.Drawing.Brushes.Blue, 5, 5);
}
}
}
|
|
|
|
|
Hi everybody,
been struggling to understand this problem for hours, now im asking the pros...
<br />
OleDbCommand UCommand = new OleDbCommand("UPDATE UniqueDevices SET DeviceName=@'" + NewName + "' WHERE MACAddress=@'" + MACAddress + "'", Connection);<br />
<br />
adapter.UpdateCommand = UCommand;<br />
adapter.UpdateCommand.Connection = Connection;<br />
adapter.Update(ds, "UniqueDevices");<br />
<br />
Connection.Close();<br />
Can anybody please, PLEASE highlight the [possibily] obvious problem here. I live in the UK and its now 2:30am and i really want to go to bed!
On a serious note, please advise.
Thanks
Jonathan Harker
|
|
|
|
|
Omit the @ characters
or (preferably) use parameters.
And you probably don't need the DataAdapter at all; just execute the command.
|
|
|
|
|
thanks alot for your post PIEBALDconsult
your message inspired the answer:
<br />
OleDbCommand UCommand = new OleDbCommand("UPDATE UniqueDevices SET DeviceName='" + NewName + "' WHERE MACAddress='" + MACAddress + "'", Connection);<br />
UCommand.ExecuteNonQuery();<br />
cant believe i missed that one simple command!
once again thanks alot.
Jonathan Harker
|
|
|
|
|
(It's still better to use parameters.)
|
|
|
|
|
You might want to change your SQL statement because it really is subject to SQL injection.
Also I think that instead of adapter.Update that you might want to investigate the ExecuteScalar() method of the OleDbCommand class.
Microsoft provides pretty good documentation on the OleDbCommand class. http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbcommand(VS.71).aspx[^]
Just because we can; does not mean we should.
|
|
|
|
|
While trying to process multiple select statement and retreiving files from my database I get the following message:
"ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x271130
to COM context 0x2712a0 for 60 seconds. The thread that owns the destination
context/apartment is most likely either doing a non pumping wait or
processing a very long running operation without pumping Windows messages.
This situation generally has a negative performance impact and may even
lead to the application becoming non responsive or memory usage accumulating
continually over time. To avoid this problem, all single threaded apartment
(STA) threads should use pumping wait primitives (such as
CoWaitForMultipleHandles) and routinely pump messages during long running
operations."
How do I resolve this problem, I am using C# and SQL Server.
Thanks in advance,
Michael
|
|
|
|
|