|
if i make draw method of class static, it has one error like this:
(graphics g=this.CreateGraphics();)
Error 1 Keyword 'this' is not valid in a static property, static method, or static field initializer F:\Bk_University\C#\TextBox_Test\TextBox_Test\Form1.cs 26 24 TextBox_Test
On the other hand, if i use static for draw method, i can't call draw() from form2.
Thanks for replying!
|
|
|
|
|
Forms need to redraw themselves when they are uncovered, restored maximised, resized and many other reasons.
The reason you don't see the line is that your draw it before the form is shown. When the form is shown it needs to redraw itself so anything that was drawn on the form is relaced. (Well, it isn't so much replaced, but drawn because it was never displayed the first time)
You need to override the OnPaint method to draw on the form and keep it persistant. The OnPaint will be called whenever the display of the form is damaged and needs to be redrawn.
|
|
|
|
|
Thanks!
I can see the line on the new form1 but i want to see it on the original form1 (the first form1). I can't override onPaint() again because it was used for another work(for example it was used to drawing rectangel.If i call onPaint() which is overrided, it will draw line and also draw rectangel.But i want to draw line only)
So i have to write another function: draw(). Is there any way to do?
Thanks for replying!
|
|
|
|
|
i`m beginner wrote: I can't override onPaint() again because it was used for another work
The only function of OnPaint is to draw stuff. It doesn't matter what it is. It draws everything. You cannot use it for one function and something else for another. Everything goes through the OnPaint method.
Your OnPaint needs a mechansim to decide what it needs to draw. It can then call the appropriate methods. e.g. Your OnPaint method can call your Draw method.
|
|
|
|
|
Colin Angus Mackay wrote: Your OnPaint needs a mechansim to decide what it needs to draw. It can then call the appropriate methods. e.g. Your OnPaint method can call your Draw method.
i can't understand clearly, Can you give me an example about my problem?
Thanks for replying!
|
|
|
|
|
Hi,
this is a standard text on painting, I provide it as a general hint
there are several steps to draw something so it becomes visible on the screen:
1.
decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a
Form itself. I prefer to add a Panel to a Form, then draw on the Panel.
2.
create some variables (Rectangle, struct, class, whatever) that hold the parameters of
your drawing. For a rectangle that could be top and left coordinate, and width+height,
or just a Rectangle. etc.
3.
create a Paint handler for that Panel, and do all your drawing in there, using the
Graphics class and your variables.
4.
when you want to change things, modify the variables and call Panel.Invalidate() or
one of its overloads (for selective invalidation).
5.
If you want to animate things, perform the move (step 4) inside the Tick handler
of a Windows.Forms.Timer
BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them
alive in class members (hence create them only once); or create them inside the Paint
handler and don't forget to call Dispose() on them.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Thank you so much!
|
|
|
|
|
There are enough problems in this code that I'd say you need to buy a book and work through it.
As for your main question, you'd do this with delegates. Any other solution is a hack. form2 needs to be able to call a method in form1 through a delegate.
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 )
|
|
|
|
|
Thank you!
|
|
|
|
|
I put some buttons and plot some lines to connect them in a panel, if they are too big and the scrollbar appear automatically. and the size is changed then the scrollbar is changed also.
I have one question: during this, what is changed then? I found the panel size and clientsize are not changed. Because during this, the lines are not connected to the buttons anymore. I want to ajust the lines during this, but what is changed then?
thanks.
|
|
|
|
|
can you please explain in an easy to understand way!! do have a look at "How to get an answer to your question"!!
|
|
|
|
|
hi
I have two monitors and in PenTablet.cpl for a Wacom Bamboo table you can set the input to be taken across both monitors or either one simply by checking a box or two and it takes immediate effect.
I would like to write an app that allows me to change the input area from monitor 1 to monitor 2 and back, is there away to interact with a CPL programmatically or to see how they are doing it?
thanks
|
|
|
|
|
Currently I am writing a program which sends and receives messages
through serial port to a device. I am using C# and Microsoft Visual
studio 2005 for windows program. But my problem is when i try to write
in serial port from my windows a Timeoutexception is thrown. I use
SerialPort class in System.IO.Ports and for writing port i used
write() function under the built in class SerialPort. But when i use
hyperterminal , everything goes fine.
Can anyone say how can i overcome this problem?
Another thing , I have used SerialPort.GetPortNames() to get the list
of serial ports. But sometimes the list of serial ports that is
returned by this method is not same as the list of serial ports shown
in device manager.(Currently SerialPort.GetPortNames() return COM1
and COM3 but no existence of COM3 in device manager) Which method can
i use to get the actual list of serial ports?
Thanks in advance
Nasif Bin Shafi
Nasif
|
|
|
|
|
Hi,
PORT NAMES
I guess your COM3 is a real serial port that is inaccessible because it is used for
an internal modem, so it would show up elsewhere in Device Manager (try modems),
but technically it still is a serial port, so yes FetPortNames() would show it,
as it also shows serial ports that are already in use.
A terminal emulator, or some other program, may prefer to only show available ports,
hence it would enumerate SerialPort.GetPortNames and test them for availability
(probably SerialPort.IsOpen does exactly that).
SERIAL OUTPUT
The serial output is buffered, similar to serial input. So each write adds data to the
output buffer, unless the buffer is full. The buffer gets emptied automatically towards
the serial cable (and its attached device), but this may fail when some kind of dataflow
has been specified and is currently in HOLD (as in: software dataflow and CTRL/S received,
or hardware dataflow and the appropriate control input pin asserted, I believe it is
DSR, readable through SerialPort.DsrHolding ).
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hi, i have several textBoxes on a form and i want to set a Boolean to false if a key is pressed in any of them.
Is there a quick method to do that instead of "textBox1_KeyPress", "textBox2_KeyPress" ect... ?
Thanks in advance
|
|
|
|
|
You can have one KeyPress event handler for all of the textboxes
|
|
|
|
|
or you may set Form.KeyPreview true and take care of it in Form.OnKeyDown/OnKeyPress
(possibly with checking object sender is a TextBox).
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Luc Pattyn wrote: or you may set Form.KeyPreview true and take care of it in Form.OnKeyDown/OnKeyPress
(possibly with checking object sender is a TextBox).
Thx allot it works.
I am also interested in Giorgi solution as i do not know how to do what he tells.
|
|
|
|
|
If you are using Visual Studio then select the button you want to set event handler for and switch to the event list in properties window. Find the KeyPress event there and choose the event handler you want to attach from the drop-down list. For each button select the same event.
|
|
|
|
|
I am sorry if I am sounding dumb. I want to use treeview control to display certain text of object and perform certain action by fetching values of the object on selection.
Suppose I have an Employee Class which have getters setters like Name,AGE, DOJ etc. What I want to show name of employee as treenode text and then upon selection wants to execute getters like DOJ,AGE etc to fetch other values of that employee object. Tree Structure could be something like:
Engineer Dept
|_ Employee1
|_ Employee2
Finance Dept
|_ Employee3
|_ Employee4
I am not being able to figure out how to that. I can loop thru object by calling some getName() to extract the Name but not successful to fetch other values. Do I write some search function which will check required 'Keyword' by going thru all Objects and then return an OBJECT to perform task?
pls help me out
|
|
|
|
|
What about using "tag"??
e.g:
<br />
myNode.value = objEmployee.Name;<br />
myNode.tag = objEmployee;<br />
<br />
Treeview.Nodes.Add(myNode);
There is a property called "tag" in each node.. so, you can use it for storing the object...
|
|
|
|
|
Michael's solution is what your looking for.
You could improve it slightly by overriding the ToString method in the Employee class to return the Name property, so myNode.Text = objEmployee.ToString();
The benefit of this is you can manipulate the data to be displayed within the class itself. Also, if you later need to add this data to a combo box or similar, these controls automatically use this method to determine what data to display (the Items collection is a collection of objects not strings).
I'm not sure why Microsoft didn't include a treeview.Nodes.Add(object item)
|
|
|
|
|
DaveyM69 wrote: I'm not sure why Microsoft didn't include a treeview.Nodes.Add(object item)
act, i typed just like that.. so.. I hope he might get some ideas at least..
DaveyM69 wrote: You could improve it slightly by overriding the ToString method in the Employee class to return the Name property, so myNode.Text = objEmployee.ToString();
yeah.. that's good idea.
|
|
|
|
|
Hi all
I am new to C#, we are developing an application and it must support many languages including Asian and arabic. the problem is in some cultures like Arabic, the control's position is different, apart from that, there are lots of labels and buttons in this application so how can we managed all these labels to automatically change their values and positions?
I managed to define resources and assembly but in this case I have to choose label by calling some methods to retrieve the correct label for current culture and this seems really hard and very untidy code. is there a way to define resource for each form so it automatically will be used when the culture changed
any suggestion would be of great help
thank you very much
|
|
|
|
|
Set the localizable property of form to true and design the form for different languages by setting the language property.
|
|
|
|