|
teknolog123 wrote: something is missing
Is this the problem that you need help with? I'm can see a mistake in your code but before I bother typing in a full response can you let us know what issues you are having.
Alan.
|
|
|
|
|
Hi Alan,
briefly, I created a custom control and put it on the main form.During runtime the custom control is doing something and needing to write/change value of the controls on the mainform. This is where I get stuck. All I want is to access and set/get the mainform's control's values through public properties/methods without making their access modifiers public.Please see my first post for the code I use
thanks in advance
|
|
|
|
|
Ok,
I'm still guessing then. The custom control, masaCubuk, is a child of MainForm and I assume that btn1.Text on the visible instance of MainForm never changes.
If that is the issue then the reason the code does not work lies in these lines:
private void button1_Click(object sender, EventArgs e)
{
MainForm frm = new MainForm();
frm.buttontext="sample";
}
Here you intend to communicate with the existing MainForm but have created a new instance. What would be required to get this code fragment to work is a reference to the actual MainForm, i.e. the parent of masaCubuk. You could pass the reference as an argument to masaCubuk's constructor but doing so would introduce the tight coupling that you wish to avoid.
The accepted "loose coupling" technique is to have the main form access the custom control by subscribing to events exposed by the control.
MSDN's How To topic[^]
If you have never written an event this will seem like a lot of work but the code pattern is so standard that once learnt it is easily remembered.
Alan.
|
|
|
|
|
The solution to this is to raise an event in the control that the form subscribes to. This is how all the 'built in' controls work too. Example:
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class FormMain : Form
{
MyControl myControl;
Button button;
public FormMain()
{
InitializeComponent();
myControl = new MyControl();
myControl.SetTextRequest += myControl_SetTextRequest;
button = new Button();
Controls.Add(button);
button.Click += button_Click;
}
void button_Click(object sender, EventArgs e)
{
myControl.PerformSetTextRequest("Button");
}
void myControl_SetTextRequest(object sender, EventArgs<string> e)
{
button.Text = e.Value;
}
}
public class MyControl : Control
{
public event EventHandler<EventArgs<string>> SetTextRequest;
protected virtual void OnSetTestRequest(EventArgs<string> e)
{
EventHandler<EventArgs<string>> eh = SetTextRequest;
if (eh != null)
eh(this, e);
}
public void PerformSetTextRequest(string text)
{
OnSetTestRequest(new EventArgs<string>(text));
}
}
public class EventArgs<T> : EventArgs
{
public EventArgs(T value)
{
Value = value;
}
public T Value
{
get;
private set;
}
}
|
|
|
|
|
thanks so much Dave, I wasn't expecting so much code.To be honest I didn't understand because I am new to C# and other than that, if this neeeds to be done for every control I want to access, then I will be facing a big challenge Anyway, it is really nice of you to write the code in detail.I think I need to start from the beginning of event handling.
|
|
|
|
|
No problem.
Events are really simple once you grasp the concept. There are many articles/blog entries around that cover them, but most tend to get complex quickly.
I wrote an article here[^] to address this - you may find it useful. Take each example one by one and make sure you understand it thoroughly before looking at the next and you'll soon get it.
|
|
|
|
|
a much bigger thanks Dave, just as I started thinking that I was too stupid to understand the event handling issue, I red your experience about event handling and felt comfortable Finally there is a simple article + a demo project! what else a dummy wants? I will have a look at your article with an open mind, I have been struggling with the computer for 13 hours and started seeing illusions thanks again!
|
|
|
|
|
I need some ideas for my final project in c# related to database.
|
|
|
|
|
What kind of ideas you want?
|
|
|
|
|
like PIA flight Information System,or any shop or school or railway system
This is not my degrees final project,this is semester project.So it should be little smaal
|
|
|
|
|
Saba Minhas wrote: like PIA flight Information System,or any shop or school or railway system
So, you have many idea. Why you are not implementing one of them.
Saba Minhas wrote: This is not my degrees final project,this is semester project.So it should be little smaal
Don't go for develop all the things. Divide by module. Try to implement some of the main module.
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
I can't implement these ideas because my other classfellows are already implementing these ideas.
|
|
|
|
|
If you want something fairly simple, but usefull and expandable to any level of complexity you want, how about a to-do's list? Can be simple to start with, but expand to timers, reminders, assigned to user, GANT charts, etc.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
|
Hi,
Can someone please help me with this code?
10x
|
|
|
|
|
Well, first of all, the article HAS a forum for you to ask questions. Second, what does 'help me with this code' mean ? What sort of help do you need exactly ? ( I suggest whatever it is, you ask in the right forum )
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
In my windows application project ,
I have several user-forms and letters , that should stored in SQL database.
* User must be able to change contents and format of the forms and some parts of the letters.
(editing the forms and letters by user)
* Program must be able to change or fill some parts of a letter for a person and print it.
(filling and editing the forms and letters by program at runtime)
----------------------------
What is the best idea for this program ?
Should I have a builtin text editor in my program ?
How can I change or fill some parts of a letter or form at runtime ?
What about office-word to use for this program ?
----------------------------
what I should to do ?
please help me.
H.R
|
|
|
|
|
Word seems like overkill to me, esp because it means your users must have Word installed. A rich text editor, or even a textbox, sounds like it could be fine. It depends on the format of the files, what sort of stuff you need to support.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Hi!
I am trying to implement client(on my pda) server(on my laptop) application in c#. at the beginning everything works fine after a few seconds exception is occured on the server side. as the following :
"unable to write data to the transport connection an established connection was aborted by software in your host machine"
I searched solution of this exception. somebody suggested that I should turn off antivirus and firewall programs on my laptop. I did but I have still same problem. could you help me as soon as possible.
thanks....
|
|
|
|
|
|
This is truly awful code. Are you competing in an unreadable code contest ?
This is a variation on code you posted the other day. You've done nothing to improve it. The error message means what it says. It seems to mean that your data does not contain a primary key which can be used for updating. Everything about this tells me that you are writing next to no code and letting wizards and auto generating tools do it for you. This path will only lead to a big mess, because you plainly don't understand how to write readable code, what any of this code does, etc.
If you're writing code for fun, start again, buy a good book, and learn how to actually write methods, and SQL, and so on. If you're being paid for this code, you are a disgrace, and I would expect that this project will either not be delivered, or be an unmaintainable mess when it is.
I also note you ask the same basic questions over and over, but never respond when people try to help you. No-one is even capable of fixing this mess based on what you posted, so start to discuss your problems with us, or just stop posting. You won't get a solution, ever, by doing what you're doing, we just cannot help you by giving exact instructions, even if we wanted to, based on this.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
OK, maybe tune it back a little now.
|
|
|
|
|
I have tried to help this guy several times, so have other people. I'm sorry, but people who ask clueless questions, then don't answer when people try to help them, are just a waste of space. I always try to help in the first instance, even tho it's clear to me he is doing paid work and has no clue how it's done.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Christian Graus wrote: but people who ask clueless questions, then don't answer when people try to help them.
and this is increasing day by day
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
And he did the last thing we are expecting from him, deleted the post.
|
|
|
|